Mediapipe Pose是一个开源的人体姿势估计库,它可以通过分析视频流或图像来检测人体的关键点,并生成人体姿势的估计结果。要更改输出视频上跟踪点和连接线的颜色,可以按照以下步骤进行操作:
import mediapipe as mp
import cv2
mp_pose = mp.solutions.pose
pose = mp_pose.Pose()
cap = cv2.VideoCapture('input_video.mp4') # 或者使用摄像头:cap = cv2.VideoCapture(0)
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
# 将图像转换为RGB格式
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# 处理图像并获取姿势估计结果
results = pose.process(frame_rgb)
# 检查是否成功检测到姿势
if results.pose_landmarks:
# 绘制跟踪点和连接线
mp_pose.draw_landmarks(frame, results.pose_landmarks, mp_pose.POSE_CONNECTIONS,
landmark_drawing_spec=mp_drawing_styles.get_default_pose_landmarks_style(),
connection_drawing_spec=mp_drawing_styles.get_default_pose_connections_style())
# 显示处理后的帧
cv2.imshow('MediaPipe Pose', frame)
# 按下'q'键退出循环
if cv2.waitKey(1) & 0xFF == ord('q'):
break
mp_drawing_styles
模块中的DrawingSpec
类来指定颜色和线宽。例如,要将跟踪点的颜色更改为红色,连接线的颜色更改为绿色,可以按照以下方式进行设置:# 创建自定义的DrawingSpec对象
landmark_drawing_spec = mp_drawing_styles.DrawingSpec(color=(255, 0, 0), thickness=2, circle_radius=2)
connection_drawing_spec = mp_drawing_styles.DrawingSpec(color=(0, 255, 0), thickness=2, circle_radius=1)
# 在绘制跟踪点和连接线时使用自定义的DrawingSpec对象
mp_pose.draw_landmarks(frame, results.pose_landmarks, mp_pose.POSE_CONNECTIONS,
landmark_drawing_spec=landmark_drawing_spec,
connection_drawing_spec=connection_drawing_spec)
import mediapipe as mp
import cv2
mp_pose = mp.solutions.pose
mp_drawing_styles = mp.solutions.drawing_styles
pose = mp_pose.Pose()
cap = cv2.VideoCapture('input_video.mp4')
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
results = pose.process(frame_rgb)
if results.pose_landmarks:
landmark_drawing_spec = mp_drawing_styles.DrawingSpec(color=(255, 0, 0), thickness=2, circle_radius=2)
connection_drawing_spec = mp_drawing_styles.DrawingSpec(color=(0, 255, 0), thickness=2, circle_radius=1)
mp_pose.draw_landmarks(frame, results.pose_landmarks, mp_pose.POSE_CONNECTIONS,
landmark_drawing_spec=landmark_drawing_spec,
connection_drawing_spec=connection_drawing_spec)
cv2.imshow('MediaPipe Pose', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
这样,你就可以使用Mediapipe Pose更改输出视频上跟踪点和连接线的颜色了。请注意,以上代码示例中的颜色设置仅为示范,你可以根据需要自行调整颜色值。