You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用MediaPipe Pose修改输出视频中33个人体追踪点及连接线的颜色

Customizing MediaPipe Pose Landmark & Connection Colors

Got it, let's fix that default white connection line and tweak your landmark colors too! MediaPipe lets you define custom drawing styles using DrawingSpec from mp.solutions.drawing_utils—this is exactly what you need to override the default look.

Here's the modified version of your poseDetector class with custom colors:

import cv2
import mediapipe as mp

class poseDetector():
    def __init__(self, mode=False, complex=1, smooth_landmarks=True, segmentation=True, smooth_segmentation=True, detectionCon=0.5, trackCon=0.5):
        self.mode = mode
        self.complex = complex
        self.smooth_landmarks = smooth_landmarks
        self.segmentation = segmentation
        self.smooth_segmentation = smooth_segmentation
        self.detectionCon = detectionCon
        self.trackCon = trackCon
        self.mpDraw = mp.solutions.drawing_utils
        self.mpDrawStyle = mp.solutions.drawing_styles
        self.mpPose = mp.solutions.pose
        self.pose = self.mpPose.Pose(self.mode, self.complex, self.smooth_landmarks, self.segmentation, self.smooth_segmentation, self.detectionCon, self.trackCon)
        
        # Define custom drawing styles here
        # Colors use OpenCV's BGR format (so (255,0,0) = blue, (0,255,0) = green)
        self.custom_landmark_spec = self.mpDraw.DrawingSpec(
            color=(0, 255, 0),  # Bright green landmarks
            thickness=2,
            circle_radius=3
        )
        self.custom_connection_spec = self.mpDraw.DrawingSpec(
            color=(255, 0, 0),  # Bold blue connections (no more white!)
            thickness=3
        )

    def findPose(self, img, draw=True):
        imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        self.results = self.pose.process(imgRGB)
        if self.results.pose_landmarks:
            if draw:
                # Pass our custom styles to the drawing function
                self.mpDraw.draw_landmarks(
                    img, 
                    self.results.pose_landmarks, 
                    self.mpPose.POSE_CONNECTIONS,
                    landmark_drawing_spec=self.custom_landmark_spec,
                    connection_drawing_spec=self.custom_connection_spec
                )
        return img

def main():
    cap = cv2.VideoCapture("../assets/videos/v4.mp4")
    detector = poseDetector()
    while True:
        success, img = cap.read()
        if not success:
            break  # Exit loop when video ends
        img = detector.findPose(img)
        cv2.imshow("Image", img)
        if cv2.waitKey(1) & 0xFF == ord('q'):  # Press 'q' to quit early
            break
    cap.release()
    cv2.destroyAllWindows()

if __name__ == "__main__":
    main()

Key changes explained:

  1. Custom DrawingSpec Objects: We added two style definitions in the class constructor:

    • custom_landmark_spec: Controls the 33 pose landmarks' color, border thickness, and circle size
    • custom_connection_spec: Overrides the default white lines with your preferred color and thickness
    • Pro tip: Swap the BGR values to get any color you want—(0,0,255) for red, (255,255,0) for cyan, etc.
  2. Updated draw_landmarks Call: We passed our custom specs as parameters to replace MediaPipe's default styling, ensuring your connections and landmarks match your desired look.

  3. Small Quality-of-Life Fixes: Added checks to exit gracefully when the video ends or you press 'q', preventing infinite loops or stuck windows.

Feel free to adjust the color values, thickness, and radius numbers to match whatever aesthetic you're going for!

内容的提问来源于stack exchange,提问作者Atharva Katre

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 13:02:34