运用mediapipe和cv2写一个人体姿态检测的代码将各个人体关键的都标注出来
时间: 2023-08-23 13:03:54 浏览: 82
基于C++实现的人体姿势检测
好的,以下是一个使用mediapipe和OpenCV进行人体姿态估计的示例代码,可以检测到人体的各个关键点,并用数字标注出来:
```python
import cv2
import mediapipe as mp
# 初始化mediapipe
mp_drawing = mp.solutions.drawing_utils
mp_pose = mp.solutions.pose
# 初始化摄像头
cap = cv2.VideoCapture(0)
with mp_pose.Pose(min_detection_confidence=0.5, min_tracking_confidence=0.5) as pose:
while cap.isOpened():
success, image = cap.read()
if not success:
print("Ignoring empty camera frame.")
continue
# 转换图像颜色空间
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# 执行姿态估计
results = pose.process(image)
# 绘制姿态估计结果
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
mp_drawing.draw_landmarks(image, results.pose_landmarks, mp_pose.POSE_CONNECTIONS,
mp_drawing.DrawingSpec(color=(0, 255, 0), thickness=2, circle_radius=2),
mp_drawing.DrawingSpec(color=(0, 0, 255), thickness=2, circle_radius=2))
# 标注关键点编号
for index, landmark in enumerate(results.pose_landmarks.landmark):
height, width, _ = image.shape
cx, cy = int(landmark.x * width), int(landmark.y * height)
cv2.putText(image, str(index), (cx, cy), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 1)
# 显示姿态估计结果
cv2.imshow('MediaPipe Pose', image)
if cv2.waitKey(5) & 0xFF == 27:
break
# 释放摄像头资源和窗口
cap.release()
cv2.destroyAllWindows()
```
运行这个代码后,将会打开摄像头窗口并实时显示姿态估计结果,每个关键点周围都会有一个数字标注。注意,这个代码仅仅是为了演示如何标注关键点,如果想要实际应用的话,需要根据具体需求进行修改。
阅读全文