1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| """ 2D人体关键点检测 """ class HumanKeypointDetector: """ 人体关键点检测器 模型:MediaPipe Pose / BlazePose 输出:33个3D关键点(相对坐标系) """ def __init__(self, model_type='mediapipe'): if model_type == 'mediapipe': import mediapipe as mp self.pose = mp.solutions.pose.Pose( static_image_mode=False, model_complexity=1, min_detection_confidence=0.5 ) self.keypoint_names = [ 'nose', 'left_eye_inner', 'left_eye', 'left_eye_outer', 'right_eye_inner', 'right_eye', 'right_eye_outer', 'left_ear', 'right_ear', 'mouth_left', 'mouth_right', 'left_shoulder', 'right_shoulder', 'left_elbow', 'right_elbow', 'left_wrist', 'right_wrist', 'left_pinky', 'right_pinky', 'left_index', 'right_index', 'left_thumb', 'right_thumb', 'left_hip', 'right_hip', 'left_knee', 'right_knee', 'left_ankle', 'right_ankle', 'left_heel', 'right_heel', 'left_foot_index', 'right_foot_index' ] def detect(self, image): """ 检测关键点 Args: image: RGB图像 (H, W, 3) Returns: keypoints: (33, 4) [x, y, z, visibility] """ results = self.pose.process(image) if results.pose_landmarks: landmarks = results.pose_landmarks.landmark keypoints = [] for lm in landmarks: keypoints.append([lm.x, lm.y, lm.z, lm.visibility]) return np.array(keypoints) return None def visualize_keypoints(self, image, keypoints): """可视化关键点""" h, w = image.shape[:2] for i, kp in enumerate(keypoints): if kp[3] > 0.5: x, y = int(kp[0] * w), int(kp[1] * h) cv2.circle(image, (x, y), 3, (0, 255, 0), -1) cv2.putText(image, str(i), (x+5, y), cv2.FONT_HERSHEY_SIMPLEX, 0.3, (255, 0, 0), 1) return image
|