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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
| """ 3D深度图像姿态估计
步骤: 1. 深度图像预处理 2. 人体分割 3. 关键点检测 4. 姿态分类(正常/OOP) """
import numpy as np from typing import Dict, List, Tuple
class OOPDetector: """ OOP异常姿态检测器 输入:3D深度图像 输出:姿态分类、关键点坐标、异常类型 """ def __init__(self): self.keypoints = [ 'nose', 'left_eye', 'right_eye', 'left_ear', 'right_ear', 'left_shoulder', 'right_shoulder', 'left_elbow', 'right_elbow', 'left_wrist', 'right_wrist', 'left_hip', 'right_hip', 'left_knee', 'right_knee', 'left_ankle', 'right_ankle' ] self.oop_thresholds = { 'standing_height': 1.2, 'kneeling_height': 0.6, 'forward_lean': 0.3, 'dashboard_feet': 0.7 } def detect(self, depth_image: np.ndarray) -> Dict: """ 检测OOP姿态 Args: depth_image: (H, W) 深度图像(米) Returns: result: { 'is_oop': bool, 'oop_type': str, # 'standing', 'kneeling', 'forward_lean', 'dashboard_feet' 'keypoints_3d': (17, 3), # 3D关键点坐标 'confidence': float } """ person_mask = self._segment_person(depth_image) keypoints_3d = self._detect_keypoints(depth_image, person_mask) oop_analysis = self._analyze_posture(keypoints_3d) is_oop, oop_type, confidence = self._determine_oop(oop_analysis) return { 'is_oop': is_oop, 'oop_type': oop_type, 'keypoints_3d': keypoints_3d, 'confidence': confidence } def _segment_person(self, depth_image: np.ndarray) -> np.ndarray: """ 人体分割 简化实现:基于深度阈值 """ person_mask = (depth_image > 0.3) & (depth_image < 1.5) return person_mask.astype(np.uint8) def _detect_keypoints(self, depth_image: np.ndarray, person_mask: np.ndarray) -> np.ndarray: """ 3D关键点检测 实际实现使用深度学习模型(如OpenPose、MediaPipe) 这里简化为示例 """ H, W = depth_image.shape keypoints_3d = np.zeros((17, 3)) center_x, center_y = W // 2, H // 2 keypoints_3d[0] = [center_x, center_y - 100, 0.8] keypoints_3d[1] = [center_x - 20, center_y - 110, 0.8] keypoints_3d[2] = [center_x + 20, center_y - 110, 0.8] keypoints_3d[5] = [center_x - 80, center_y, 0.9] keypoints_3d[6] = [center_x + 80, center_y, 0.9] keypoints_3d[11] = [center_x - 50, center_y + 150, 1.0] keypoints_3d[12] = [center_x + 50, center_y + 150, 1.0] keypoints_3d[13] = [center_x - 60, center_y + 250, 1.0] keypoints_3d[14] = [center_x + 60, center_y + 250, 1.0] keypoints_3d[15] = [center_x - 70, center_y + 350, 0.9] keypoints_3d[16] = [center_x + 70, center_y + 350, 0.9] return keypoints_3d def _analyze_posture(self, keypoints_3d: np.ndarray) -> Dict: """ 姿态分析 计算高度、角度等特征 """ head_height = keypoints_3d[0, 1] / 400.0 hip_height = np.mean([keypoints_3d[11, 1], keypoints_3d[12, 1]]) / 400.0 knee_height = np.mean([keypoints_3d[13, 1], keypoints_3d[14, 1]]) / 400.0 shoulder_z = np.mean([keypoints_3d[5, 2], keypoints_3d[6, 2]]) hip_z = np.mean([keypoints_3d[11, 2], keypoints_3d[12, 2]]) forward_lean = (shoulder_z - hip_z) / 0.5 ankle_height = np.mean([keypoints_3d[15, 1], keypoints_3d[16, 1]]) / 400.0 return { 'head_height': head_height, 'hip_height': hip_height, 'knee_height': knee_height, 'forward_lean': forward_lean, 'ankle_height': ankle_height } def _determine_oop(self, analysis: Dict) -> Tuple[bool, str, float]: """ OOP判定 Returns: is_oop: 是否异常姿态 oop_type: 异常类型 confidence: 置信度 """ if analysis['head_height'] < self.oop_thresholds['standing_height']: return True, 'standing', 0.85 if analysis['knee_height'] > self.oop_thresholds['kneeling_height']: return True, 'kneeling', 0.80 if analysis['forward_lean'] > self.oop_thresholds['forward_lean']: return True, 'forward_lean', 0.75 if analysis['ankle_height'] > self.oop_thresholds['dashboard_feet']: return True, 'dashboard_feet', 0.70 return False, 'normal', 0.90
if __name__ == "__main__": detector = OOPDetector() depth_normal = np.ones((480, 640)) * 1.0 depth_standing = np.ones((480, 640)) * 1.0 depth_standing[100:200, :] = 0.5 result_normal = detector.detect(depth_normal) result_standing = detector.detect(depth_standing) print("=" * 60) print("OOP异常姿态检测") print("=" * 60) print(f"\n正常坐姿:") print(f" 异常姿态: {result_normal['is_oop']}") print(f" 姿态类型: {result_normal['oop_type']}") print(f" 置信度: {result_normal['confidence']:.2f}") print(f"\n站立姿态:") print(f" 异常姿态: {result_standing['is_oop']}") print(f" 姿态类型: {result_standing['oop_type']}") print(f" 置信度: {result_standing['confidence']:.2f}")
|