关键点说明

mediapipe姿态估计可以识别出身体的33个关键点
pose_tracking_full_body_landmarks.png

获取关键点坐标

模型给出的结果是归一化的坐标比例,要获取在图像中的坐标需要乘以图像的宽高

  1. x = results.pose_landmarks.landmark[mp_pose.PoseLandmark.NOSE].x * image_width
  2. y = results.pose_landmarks.landmark[mp_pose.PoseLandmark.NOSE].y * image_height

示例代码

  1. import cv2
  2. import mediapipe as mp
  3. import numpy as np
  4. Key_Esc = 27
  5. mp_drawing = mp.solutions.drawing_utils
  6. mp_drawing_styles = mp.solutions.drawing_styles
  7. mp_pose = mp.solutions.pose
  8. # For webcam input:
  9. cap = cv2.VideoCapture(0)
  10. with mp_pose.Pose(
  11. min_detection_confidence=0.5,
  12. min_tracking_confidence=0.5) as pose:
  13. while cap.isOpened():
  14. success, image = cap.read()
  15. if not success:
  16. print("Ignoring empty camera frame.")
  17. # If loading a video, use 'break' instead of 'continue'.
  18. continue
  19. # To improve performance, optionally mark the image as not writeable to
  20. # pass by reference.
  21. image.flags.writeable = False
  22. image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
  23. results = pose.process(image)
  24. # Draw the pose annotation on the image.
  25. image.flags.writeable = True
  26. image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
  27. mp_drawing.draw_landmarks(
  28. image,
  29. results.pose_landmarks,
  30. mp_pose.POSE_CONNECTIONS,
  31. landmark_drawing_spec=mp_drawing_styles.get_default_pose_landmarks_style())
  32. # Flip the image horizontally for a selfie-view display.
  33. cv2.imshow('MediaPipe Pose', np.rot90(
  34. cv2.resize(cv2.flip(image, 1), (320, 240))))
  35. if cv2.waitKey(5) == Key_Esc:
  36. break
  37. cap.release()