如何使用 Mediapipe Pose 更改输出视频上的跟踪点和连接线的颜色?

How to change colors of the tracking points and connector lines on the output video using Mediapipe Pose?

我指的是 33 个 body 点和它们之间的连接线。我想更改它们的颜色,尤其是连接线的白色默认颜色。

这是我的代码,我已经为 mediapipe 创建了一个 class 模块,我可以将其导入并在我的其他程序中使用

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)

    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:
                self.mpDraw.draw_landmarks(img, self.results.pose_landmarks,
                                           self.mpPose.POSE_CONNECTIONS)
        return img

def main():
    cap = cv2.VideoCapture("..//assets//videos//v4.mp4")
    detector = poseDetector()
    while True:
        success, img = cap.read()
        img = detector.findPose(img)

        cv2.imshow("Image", img)
        cv2.waitKey(1)


if __name__ == "__main__":
    main()

根据文档,这是 draw_landmarks

的代码
mp_drawing.draw_landmarks(
image: numpy.ndarray,
landmark_list: mediapipe.framework.formats.landmark_pb2.NormalizedLandmarkList,
connections: Optional[List[Tuple[int, int]]] = None,
landmark_drawing_spec: mediapipe.python.solutions.drawing_utils.DrawingSpec = DrawingSpec(color=(0, 0, 255), thickness=2, circle_radius=2),
connection_drawing_spec: mediapipe.python.solutions.drawing_utils.DrawingSpec = DrawingSpec(color=(0, 255, 0), thickness=2, circle_radius=2),
)

所以在你的findPose函数中你只需要更新一行代码

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:
            self.mpDraw.draw_landmarks(img, self.results.pose_landmarks,
                                       self.mpPose.POSE_CONNECTIONS,
            self.mpDraw.DrawingSpec(color=(245,117,66), thickness=2, circle_radius=2),
            self.mpDraw.DrawingSpec(color=(245,66,230), thickness=2, circle_radius=2))
    return img

第一个self.mpDraw.DrawingSpec参数对应地标点。第二个 self.mpDraw.DrawingSpec 参数对应于那些地标点之间的连接。 color 的格式是 (B, G, R)