gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 18:32:36 29 4
gpt4 key购买 nike

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

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

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 参数对应于这些地标点之间的连接。颜色(B, G, R)格式

关于python - 如何使用 Mediapipe Pose 更改输出视频上的跟踪点和连接线的颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69240807/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com