MediaPipe Pose Estimation
Advanced pose detection and visualization system
import cv2 import numpy as np import gradio as gr import mediapipe as mp from mediapipe.tasks.python import vision from mediapipe.tasks.python import BaseOptions from mediapipe.tasks.python.vision import PoseLandmarker, PoseLandmarkerOptions, RunningMode MODEL_PATH = "pose_landmarker.task" # Color palette (BGR format) COLORS = { "White": (255, 255, 255), "Red": (0, 0, 255), "Green": (0, 255, 0), "Blue": (255, 0, 0), "Yellow": (0, 255, 255), "Cyan": (255, 255, 0), "Magenta": (255, 0, 255), "Orange": (0, 165, 255), "Purple": (255, 0, 128), "Pink": (203, 192, 255), } MULTICOLOR_SCHEME = { "face": (255, 255, 0), # Cyan "torso": (0, 255, 255), # Yellow "right_arm": (0, 0, 255), # Red "left_arm": (255, 0, 0), # Blue "right_leg": (255, 0, 255), # Magenta "left_leg": (0, 255, 0), # Green } def get_body_part_connections(): """Define which connections belong to which body part""" connections = { "face": [ (0, 1), (1, 2), (2, 3), (3, 7), # Right eye region (0, 4), (4, 5), (5, 6), (6, 8), # Left eye region (9, 10), # Mouth ], "torso": [ (11, 12), # Shoulders (11, 23), (12, 24), # Shoulder to hip (23, 24), # Hips ], "right_arm": [ (11, 13), (13, 15), # Shoulder to elbow to wrist (15, 17), (15, 19), (15, 21), # Wrist connections (17, 19), # Hand ], "left_arm": [ (12, 14), (14, 16), # Shoulder to elbow to wrist (16, 18), (16, 20), (16, 22), # Wrist connections (18, 20), # Hand ], "right_leg": [ (23, 25), (25, 27), # Hip to knee to ankle (27, 29), (27, 31), # Ankle connections (29, 31), # Foot ], "left_leg": [ (24, 26), (26, 28), # Hip to knee to ankle (28, 30), (28, 32), # Ankle connections (30, 32), # Foot ], } return connections def draw_pose( video_path, detection_confidence, tracking_confidence, background_type, color_mode, line_color, joint_color ): output_path = "output.mp4" options = PoseLandmarkerOptions( base_options=BaseOptions(model_asset_path=MODEL_PATH), running_mode=RunningMode.VIDEO, num_poses=1, min_pose_detection_confidence=detection_confidence, min_tracking_confidence=tracking_confidence, ) landmarker = PoseLandmarker.create_from_options(options) cap = cv2.VideoCapture(video_path) width = int(cap.get(3)) height = int(cap.get(4)) fps = cap.get(5) or 24 fourcc = cv2.VideoWriter_fourcc(*'mp4v') out = cv2.VideoWriter(output_path, fourcc, fps, (width, height)) timestamp = 0 line_bgr = COLORS[line_color] joint_bgr = COLORS[joint_color] body_parts = get_body_part_connections() while cap.isOpened(): ret, frame = cap.read() if not ret: break rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) mp_image = mp.Image( image_format=mp.ImageFormat.SRGB, data=rgb_frame ) result = landmarker.detect_for_video(mp_image, timestamp) timestamp += int(1000 / fps) if background_type == "Black Background": canvas = np.zeros((height, width, 3), dtype=np.uint8) else: # Original Video canvas = frame.copy() if result.pose_landmarks: for pose_landmarks in result.pose_landmarks: points = [] for lm in pose_landmarks: x = int(lm.x * width) y = int(lm.y * height) points.append((x, y)) if color_mode == "Single Color": # Original behavior - single color for all connections connections = mp.solutions.pose.POSE_CONNECTIONS for c in connections: cv2.line( canvas, points[c[0]], points[c[1]], line_bgr, 3 ) else: # Multicolor for part_name, part_connections in body_parts.items(): part_color = MULTICOLOR_SCHEME[part_name] for c in part_connections: if c[0] < len(points) and c[1] < len(points): cv2.line( canvas, points[c[0]], points[c[1]], part_color, 3 ) for p in points: cv2.circle(canvas, p, 5, joint_bgr, -1) out.write(canvas) cap.release() out.release() return output_path custom_css = """ * { font-family: 'Inter', 'Segoe UI', system-ui, -apple-system, sans-serif; } .main-header { text-align: center; padding: 30px 20px; border-bottom: 1px solid #404040; margin-bottom: 30px; } .main-header h1 { font-size: 28px; font-weight: 600; color: #ffffff; margin: 0 0 8px 0; letter-spacing: -0.5px; } .main-header p { font-size: 15px; color: #b0b0b0; margin: 0; font-weight: 400; } .section-header { font-size: 13px; font-weight: 600; color: #ffffff; text-transform: uppercase; letter-spacing: 0.5px; margin-bottom: 16px; padding-bottom: 8px; border-bottom: 2px solid #ffffff; } .subsection-title { font-size: 12px; font-weight: 600; color: #b0b0b0; text-transform: uppercase; letter-spacing: 0.3px; margin: 20px 0 12px 0; } .warning-box { background: #1a1a1a; border-left: 3px solid #888; padding: 16px 20px; margin: 20px 0; font-size: 14px; color: #d0d0d0; line-height: 1.6; } .warning-box strong { font-weight: 600; color: #ffffff; } .info-box { background: #1a1a1a; border: 1px solid #404040; padding: 16px; border-radius: 4px; margin-top: 16px; } .info-box p { margin: 6px 0; font-size: 13px; color: #b0b0b0; line-height: 1.5; } .info-box strong { color: #ffffff; } .footer { text-align: center; padding: 30px 20px; border-top: 1px solid #404040; margin-top: 40px; color: #888; font-size: 13px; } .footer h3 { font-size: 14px; font-weight: 600; color: #ffffff; margin-bottom: 12px; } .footer p { color: #b0b0b0; } #submit-btn { margin-top: 24px; background: #ffffff; color: #000000; border: none; font-weight: 500; letter-spacing: 0.3px; } #submit-btn:hover { background: #e0e0e0; } .gr-box { border-radius: 4px; } .gr-input, .gr-dropdown, .gr-radio { border-radius: 4px; } .gr-accordion { border: 1px solid #404040; border-radius: 4px; } label { color: #d0d0d0 !important; } .gr-text-input, .gr-dropdown { background: #1a1a1a; border: 1px solid #404040; color: #ffffff; } .color-legend { background: #1a1a1a; border: 1px solid #404040; padding: 12px; border-radius: 4px; margin-top: 12px; font-size: 12px; } .color-legend p { margin: 4px 0; color: #b0b0b0; } .color-item { display: inline-block; width: 12px; height: 12px; margin-right: 6px; border-radius: 2px; } """ with gr.Blocks(title="MediaPipe Pose Estimation", theme=gr.themes.Default(), css=custom_css) as demo: # Header gr.HTML( """
Advanced pose detection and visualization system
Multicolor Legend:
Torso: Yellow
Right Arm: Red
Left Arm: Blue
Right Leg: Magenta
Left Leg: Green
Output Specifications:
Format: MP4 (H.264 encoding)
Resolution: Matches input resolution
Frame Rate: Matches input frame rate
Keypoints: 33 body landmarks tracked per frame