mediapipe 检测敬礼是否标准的代码
时间: 2023-09-24 07:13:51 浏览: 83
PyPlagia:粗心学生的Python代码中的抄袭检测
以下是使用mediapipe进行敬礼检测的示例代码:
```python
import cv2
import mediapipe as mp
mp_drawing = mp.solutions.drawing_utils
mp_pose = mp.solutions.pose
# 初始化Pose模型
pose = mp_pose.Pose(min_detection_confidence=0.5, min_tracking_confidence=0.5)
# 加载测试图像
image = cv2.imread('test_image.jpg')
image_height, image_width, _ = image.shape
# 将图像从BGR转换为RGB
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# 运行Pose模型
results = pose.process(image_rgb)
# 检测敬礼动作
if results.pose_landmarks is not None:
# 获取关键点
landmarks = results.pose_landmarks.landmark
# 检测右臂是否向上伸直
right_shoulder_x, right_shoulder_y = landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER.value].x, \
landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER.value].y
right_elbow_x, right_elbow_y = landmarks[mp_pose.PoseLandmark.RIGHT_ELBOW.value].x, \
landmarks[mp_pose.PoseLandmark.RIGHT_ELBOW.value].y
right_wrist_x, right_wrist_y = landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value].x, \
landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value].y
right_arm_angle = abs((right_elbow_y - right_shoulder_y) / (right_wrist_y - right_elbow_y))
# 检测左臂是否向上伸直
left_shoulder_x, left_shoulder_y = landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value].x, \
landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value].y
left_elbow_x, left_elbow_y = landmarks[mp_pose.PoseLandmark.LEFT_ELBOW.value].x, \
landmarks[mp_pose.PoseLandmark.LEFT_ELBOW.value].y
left_wrist_x, left_wrist_y = landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value].x, \
landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value].y
left_arm_angle = abs((left_elbow_y - left_shoulder_y) / (left_wrist_y - left_elbow_y))
# 判断是否为敬礼动作
if right_arm_angle > 0.9 and left_arm_angle > 0.9:
print('敬礼动作标准')
else:
print('敬礼动作不标准')
```
该代码使用mediapipe库中的Pose模型检测人体姿势,并根据姿势关键点判断是否为敬礼动作。具体来说,该代码检测右臂和左臂是否向上伸直,如果两臂都向上伸直,则判断为敬礼动作。
阅读全文