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 283 284 285 286 287 288 289 290 291
| """ Euro NCAP OOP-01~06 场景:异常姿态检测 协议要求:检测驾驶员异常坐姿,调整气囊部署策略 """
import numpy as np from typing import List, Tuple, Optional from dataclasses import dataclass from enum import Enum
class PostureType(Enum): """姿态类型""" NORMAL = "normal" FORWARD_SEVERE = "forward_severe" FORWARD_MODERATE = "forward_moderate" LEANING_DOOR = "leaning_door" RECLINED = "reclined" FEET_DASHBOARD = "feet_dashboard" CROUCHED = "crouched"
@dataclass class BodyKeypoint: """身体关键点(3D)""" name: str x: float y: float z: float confidence: float
@dataclass class PoseEstimate: """姿态估计结果""" timestamp: float keypoints: List[BodyKeypoint] def get_keypoint(self, name: str) -> Optional[BodyKeypoint]: """获取指定关键点""" for kp in self.keypoints: if kp.name == name: return kp return None
class OOPDetector: """ Euro NCAP 标准异常姿态检测器 实现场景: - OOP-01: 前倾严重(距仪表台 < 20cm) - OOP-02: 前倾中度(距仪表台 20-30cm) - OOP-03: 侧倾靠门 - OOP-04: 后仰(座椅角度 > 35°) 硬件要求: - 深度摄像头(如 Intel RealSense D455) - 或 3D ToF 传感器 - 分辨率 ≥ 640x480,深度精度 ≤ 2cm """ KEYPOINT_NAMES = [ 'nose', 'left_eye', 'right_eye', 'left_shoulder', 'right_shoulder', 'left_elbow', 'right_elbow', 'left_wrist', 'right_wrist', 'left_hip', 'right_hip', 'left_knee', 'right_knee', 'left_ankle', 'right_ankle' ] def __init__( self, depth_camera_height: float = 0.3, dashboard_distance: float = 0.6, seat_angle_threshold: float = 35.0 ): """ 初始化 OOP 检测器 Args: depth_camera_height: 深度摄像头高度 dashboard_distance: 正常坐姿距仪表台距离 seat_angle_threshold: 后仰角度阈值 """ self.camera_height = depth_camera_height self.dashboard_distance = dashboard_distance self.seat_angle_threshold = seat_angle_threshold def detect(self, pose: PoseEstimate) -> Tuple[PostureType, dict]: """ 检测异常姿态 Args: pose: 姿态估计结果 Returns: posture_type: 姿态类型 metrics: 检测指标 """ nose = pose.get_keypoint('nose') left_shoulder = pose.get_keypoint('left_shoulder') right_shoulder = pose.get_keypoint('right_shoulder') left_hip = pose.get_keypoint('left_hip') right_hip = pose.get_keypoint('right_hip') left_knee = pose.get_keypoint('left_knee') right_knee = pose.get_keypoint('right_knee') if not all([nose, left_shoulder, right_shoulder, left_hip, right_hip]): return PostureType.NORMAL, {'error': 'Missing keypoints'} shoulder_center = BodyKeypoint( name='shoulder_center', x=(left_shoulder.x + right_shoulder.x) / 2, y=(left_shoulder.y + right_shoulder.y) / 2, z=(left_shoulder.z + right_shoulder.z) / 2, confidence=min(left_shoulder.confidence, right_shoulder.confidence) ) hip_center = BodyKeypoint( name='hip_center', x=(left_hip.x + right_hip.x) / 2, y=(left_hip.y + right_hip.y) / 2, z=(left_hip.z + right_hip.z) / 2, confidence=min(left_hip.confidence, right_hip.confidence) ) metrics = { 'nose_distance_to_dashboard': nose.z, 'shoulder_distance_to_dashboard': shoulder_center.z, 'lateral_offset': shoulder_center.x, } if nose.z < 0.2: return PostureType.FORWARD_SEVERE, { **metrics, 'trigger': 'OOP-01', 'message': f'Severe forward lean: {nose.z:.2f}m to dashboard' } if nose.z < 0.3: return PostureType.FORWARD_MODERATE, { **metrics, 'trigger': 'OOP-02', 'message': f'Moderate forward lean: {nose.z:.2f}m to dashboard' } lateral_offset = abs(shoulder_center.x) if lateral_offset > 0.2: return PostureType.LEANING_DOOR, { **metrics, 'trigger': 'OOP-03', 'message': f'Leaning to side: {lateral_offset:.2f}m offset' } torso_vector = np.array([ shoulder_center.x - hip_center.x, shoulder_center.y - hip_center.y, shoulder_center.z - hip_center.z ]) vertical = np.array([0, -1, 0]) angle = np.degrees(np.arccos( np.dot(torso_vector, vertical) / (np.linalg.norm(torso_vector) * np.linalg.norm(vertical)) )) metrics['torso_angle'] = angle if angle > self.seat_angle_threshold: return PostureType.RECLINED, { **metrics, 'trigger': 'OOP-04', 'message': f'Reclined posture: {angle:.1f}°' } if left_knee and right_knee: if left_knee.z < 0.3 or right_knee.z < 0.3: return PostureType.FEET_DASHBOARD, { **metrics, 'trigger': 'OOP-05', 'message': 'Feet on dashboard detected' } return PostureType.NORMAL, metrics def get_airbag_strategy(self, posture: PostureType) -> str: """ 根据姿态确定气囊部署策略 Args: posture: 姿态类型 Returns: strategy: 部署策略 """ strategies = { PostureType.NORMAL: "NORMAL_DEPLOYMENT", PostureType.FORWARD_SEVERE: "SUPPRESS_AIRBAG", PostureType.FORWARD_MODERATE: "LOW_RISK_DEPLOYMENT", PostureType.LEANING_DOOR: "DISABLE_SIDE_AIRBAG", PostureType.RECLINED: "LOW_RISK_DEPLOYMENT", PostureType.FEET_DASHBOARD: "SUPPRESS_AIRBAG", PostureType.CROUCHED: "SUPPRESS_AIRBAG" } return strategies.get(posture, "NORMAL_DEPLOYMENT")
if __name__ == "__main__": detector = OOPDetector() print("=== Euro NCAP OOP 测试:异常姿态检测 ===") print() test_cases = [ { 'name': '正常坐姿', 'keypoints': { 'nose': (0, 0.1, 0.5), 'left_shoulder': (-0.2, 0, 0.55), 'right_shoulder': (0.2, 0, 0.55), 'left_hip': (-0.15, -0.4, 0.6), 'right_hip': (0.15, -0.4, 0.6), 'left_knee': (-0.1, -0.6, 0.8), 'right_knee': (0.1, -0.6, 0.8) } }, { 'name': '严重前倾(OOP-01)', 'keypoints': { 'nose': (0, 0.1, 0.15), 'left_shoulder': (-0.2, 0, 0.2), 'right_shoulder': (0.2, 0, 0.2), 'left_hip': (-0.15, -0.4, 0.6), 'right_hip': (0.15, -0.4, 0.6), 'left_knee': (-0.1, -0.6, 0.8), 'right_knee': (0.1, -0.6, 0.8) } }, { 'name': '侧倾靠门(OOP-03)', 'keypoints': { 'nose': (0.25, 0.1, 0.5), 'left_shoulder': (0.05, 0, 0.55), 'right_shoulder': (0.45, 0, 0.55), 'left_hip': (-0.15, -0.4, 0.6), 'right_hip': (0.15, -0.4, 0.6), 'left_knee': (-0.1, -0.6, 0.8), 'right_knee': (0.1, -0.6, 0.8) } } ] for test_case in test_cases: print(f"测试场景: {test_case['name']}") keypoints = [] for name, (x, y, z) in test_case['keypoints'].items(): keypoints.append(BodyKeypoint( name=name, x=x, y=y, z=z, confidence=0.95 )) pose = PoseEstimate(timestamp=0, keypoints=keypoints) posture, metrics = detector.detect(pose) strategy = detector.get_airbag_strategy(posture) print(f" 检测结果: {posture.value}") if 'trigger' in metrics: print(f" 触发场景: {metrics['trigger']}") print(f" 消息: {metrics['message']}") print(f" 气囊策略: {strategy}") print() print("=== 测试完成 ===")
|