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()在以上代码基础上加上关键点与相邻关键点的角度
时间: 2024-02-14 14:19:05 浏览: 122
planar_utils.py和testCases_cv2.py
要在以上代码基础上加上关键点与相邻关键点的角度,可以在循环中添加以下代码:
```
# 计算关键点的角度
angles = []
for i in range(len(results.pose_landmarks.landmark)-2):
p1 = results.pose_landmarks.landmark[i]
p2 = results.pose_landmarks.landmark[i+1]
p3 = results.pose_landmarks.landmark[i+2]
angle = mp_pose.PoseLandmark.get_angle(p1, p2, p3)
angles.append(angle)
# 显示角度信息
for i, angle in enumerate(angles):
cv2.putText(image, f"Angle {i+1}: {angle:.2f}", (20, (i+1)*20),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1)
```
这段代码会遍历每个关键点,计算它与相邻的两个关键点之间的角度,并将角度存储在一个列表中。然后,代码会将每个角度信息显示在图像中。其中,`mp_pose.PoseLandmark.get_angle(p1, p2, p3)`函数可以计算由三个关键点组成的夹角。
完整代码如下:
```
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)
# 计算关键点的角度
angles = []
for i in range(len(results.pose_landmarks.landmark)-2):
p1 = results.pose_landmarks.landmark[i]
p2 = results.pose_landmarks.landmark[i+1]
p3 = results.pose_landmarks.landmark[i+2]
angle = mp_pose.PoseLandmark.get_angle(p1, p2, p3)
angles.append(angle)
# 显示角度信息
for i, angle in enumerate(angles):
cv2.putText(image, f"Angle {i+1}: {angle:.2f}", (20, (i+1)*20),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1)
# 显示姿态估计结果
cv2.imshow('MediaPipe Pose', image)
if cv2.waitKey(5) & 0xFF == 27:
break
# 释放摄像头资源和窗口
cap.release()
cv2.destroyAllWindows()
```
阅读全文