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 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
| """ 3D 视觉姿态估计原理
基于深度摄像头
核心方法: - 人体关键点检测 - 3D 坐标重建 - 坐姿角度计算
参考:MediaPipe Pose + OpenPose """
import numpy as np
class OccupantPosture3DVision: """ 3D 视觉姿态估计系统 Euro NCAP OOP-01 ~ OOP-05 """ def __init__(self): self.keypoints = { 'nose': 0, 'left_eye': 1, 'right_eye': 2, 'left_ear': 3, 'right_ear': 4, 'left_shoulder': 11, 'right_shoulder': 12, 'left_elbow': 13, 'right_elbow': 14, 'left_wrist': 15, 'right_wrist': 16, 'left_hip': 23, 'right_hip': 24, 'left_knee': 25, 'right_knee': 26, 'left_ankle': 27, 'right_ankle': 28 } self.forward_lean_threshold = 45 self.lying_angle_threshold = 30 def detect_posture_3d(self, keypoints_3d: np.ndarray) -> dict: """ 检测 3D 坐姿 Args: keypoints_3d: 3D 关键点坐标, shape=(33, 3) Returns: result: {'posture_type': str, 'angles': dict, 'is_normal': bool} """ angles = self._calculate_body_angles(keypoints_3d) posture_type = self._classify_posture_by_angles(angles) return { 'posture_type': posture_type, 'is_normal': posture_type == 'normal_sitting', 'angles': angles } def detect_oop_3d(self, keypoints_3d: np.ndarray) -> dict: """ 检测 3D 异常姿态 Euro NCAP OOP 场景 OOP 特征: - 头部距离方向盘 <10 cm(OOP-01) - 身体倾斜角度 >45°(OOP-02) - 高度 >座椅高度 30 cm(OOP-03) """ posture_result = self.detect_posture_3d(keypoints_3d) head_position = self._detect_head_position(keypoints_3d) body_tilt = posture_result['angles']['forward_lean'] standing_height = self._detect_standing_height(keypoints_3d) is_oop = ( head_position['distance_to_dashboard'] < 10 or body_tilt > self.forward_lean_threshold or standing_height['is_standing'] ) oop_type = self._determine_oop_type_3d(head_position, body_tilt, standing_height) return { 'is_oop': is_oop, 'oop_type': oop_type, 'head_position': head_position, 'body_tilt': body_tilt, 'standing_height': standing_height } def detect_arm_out_window(self, keypoints_3d: np.ndarray, window_boundary: float) -> dict: """ 检测手臂伸出窗外 Euro NCAP OOP-04 检测条件:手臂超出车窗边界 """ left_wrist = keypoints_3d[self.keypoints['left_wrist']] right_wrist = keypoints_3d[self.keypoints['right_wrist']] left_arm_out = left_wrist[0] > window_boundary right_arm_out = right_wrist[0] > window_boundary is_arm_out = left_arm_out or right_arm_out return { 'is_arm_out': is_arm_out, 'left_arm_out': left_arm_out, 'right_arm_out': right_arm_out, 'intervention_required': is_arm_out } def _calculate_body_angles(self, keypoints_3d: np.ndarray) -> dict: """ 计算身体角度 关键角度: - forward_lean:前倾角度(趴在方向盘) - lying_angle:躺卧角度 - head_rotation:头部旋转角度 """ nose = keypoints_3d[self.keypoints['nose']] left_shoulder = keypoints_3d[self.keypoints['left_shoulder']] right_shoulder = keypoints_3d[self.keypoints['right_shoulder']] left_hip = keypoints_3d[self.keypoints['left_hip']] right_hip = keypoints_3d[self.keypoints['right_hip']] shoulder_center = (left_shoulder + right_shoulder) / 2 hip_center = (left_hip + right_hip) / 2 forward_lean = self._calculate_angle( shoulder_center, hip_center, np.array([hip_center[0], hip_center[1] - 1, hip_center[2]]) ) lying_angle = 90 - forward_lean return { 'forward_lean': forward_lean, 'lying_angle': lying_angle, 'head_rotation': 0 } def _calculate_angle(self, p1: np.ndarray, p2: np.ndarray, p3: np.ndarray) -> float: """ 计算三点角度 Args: p1, p2, p3: 三点坐标 Returns: angle: 角度(度) """ v1 = p1 - p2 v2 = p3 - p2 cos_angle = np.dot(v1, v2) / (np.linalg.norm(v1) * np.linalg.norm(v2)) angle = np.arccos(cos_angle) * 180 / np.pi return angle def _classify_posture_by_angles(self, angles: dict) -> str: """根据角度分类坐姿""" forward_lean = angles['forward_lean'] if forward_lean < 20: return 'normal_sitting' elif forward_lean > self.forward_lean_threshold: return 'leaning_forward' elif forward_lean < self.lying_angle_threshold: return 'lying_down' else: return 'unknown' def _detect_head_position(self, keypoints_3d: np.ndarray) -> dict: """ 检测头部位置 OOP-01:头部距离方向盘 <10 cm """ nose = keypoints_3d[self.keypoints['nose']] dashboard_position = np.array([0.3, 0.5, 0.8]) distance_to_dashboard = np.linalg.norm(nose - dashboard_position) return { 'nose_position': nose, 'distance_to_dashboard': distance_to_dashboard, 'is_close_to_dashboard': distance_to_dashboard < 10 } def _detect_standing_height(self, keypoints_3d: np.ndarray) -> dict: """ 检测站立高度 OOP-03:高度 >座椅高度 30 cm """ nose = keypoints_3d[self.keypoints['nose']] seat_height = 0.5 standing_height = nose[1] - seat_height return { 'standing_height': standing_height, 'is_standing': standing_height > 30 } def _determine_oop_type_3d(self, head_position: dict, body_tilt: float, standing_height: dict) -> str: """判定 3D OOP 类型""" if head_position['is_close_to_dashboard']: return 'OOP-01' elif body_tilt > self.forward_lean_threshold: return 'OOP-02' elif standing_height['is_standing']: return 'OOP-03' else: return 'normal'
if __name__ == "__main__": vision = OccupantPosture3DVision() np.random.seed(42) normal_keypoints = np.random.randn(33, 3) * 0.1 + np.array([0.5, 0.5, 0.5]) leaning_keypoints = normal_keypoints.copy() leaning_keypoints[0] = np.array([0.3, 0.4, 0.8]) normal_result = vision.detect_posture_3d(normal_keypoints) print(f"正常坐姿检测:") print(f" 坐姿类型: {normal_result['posture_type']}") print(f" 前倾角度: {normal_result['angles']['forward_lean']:.1f}°") leaning_result = vision.detect_oop_3d(leaning_keypoints) print(f"\n趴在方向盘检测:") print(f" OOP 类型: {leaning_result['oop_type']}") print(f" 头部距离: {leaning_result['head_position']['distance_to_dashboard']:.2f} cm") arm_keypoints = normal_keypoints.copy() arm_keypoints[15] = np.array([0.8, 0.5, 0.5]) arm_result = vision.detect_arm_out_window(arm_keypoints, 0.7) print(f"\n手臂伸出窗外检测:") print(f" 手臂伸出: {arm_result['is_arm_out']}")
|