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
| from enum import Enum from typing import List, Tuple import cv2 import numpy as np
class OOPType(Enum): """异常姿态类型""" NORMAL = 'normal' HEAD_CLOSE_DASHBOARD = 'head_close_dashboard' FEET_ON_DASHBOARD_INBOARD = 'feet_on_dashboard_inboard' FEET_ON_DASHBOARD_CENTER = 'feet_on_dashboard_center' FEET_ON_DASHBOARD_OUTBOARD = 'feet_on_dashboard_outboard'
class OOPDetector: """ 异常姿态检测器 检测乘员是否处于危险坐姿 """ def __init__(self, vehicle_config: dict): """ Args: vehicle_config: 车辆配置 - dashboard_y: 仪表盘 Y 坐标(图像坐标) - dashboard_x_range: 仪表盘 X 范围 - danger_distance_mm: 危险距离阈值(mm) """ self.dashboard_y = vehicle_config['dashboard_y'] self.dashboard_x_range = vehicle_config['dashboard_x_range'] self.danger_distance_mm = vehicle_config.get('danger_distance_mm', 200) self.keypoint_detector = self._load_keypoint_model() def detect( self, image: np.ndarray, depth_map: Optional[np.ndarray] = None ) -> dict: """ 检测异常姿态 Args: image: 乘员图像 depth_map: 深度图(可选) Returns: result: { 'oop_type': OOPType, 'is_dangerous': bool, 'distance_mm': float, 'alert_required': bool } """ keypoints = self._detect_keypoints(image) head_result = self._check_head_distance( keypoints, depth_map ) feet_result = self._check_feet_position(keypoints) if head_result['is_dangerous']: return { 'oop_type': OOPType.HEAD_CLOSE_DASHBOARD, 'is_dangerous': True, 'distance_mm': head_result['distance_mm'], 'alert_required': True, 'details': head_result } if feet_result['is_dangerous']: return { 'oop_type': feet_result['oop_type'], 'is_dangerous': True, 'distance_mm': 0, 'alert_required': True, 'details': feet_result } return { 'oop_type': OOPType.NORMAL, 'is_dangerous': False, 'distance_mm': head_result.get('distance_mm', 999), 'alert_required': False } def _detect_keypoints(self, image: np.ndarray) -> dict: """检测人体关键点""" return { 'nose': (500, 300, 0.95), 'left_eye': (510, 290, 0.98), 'right_eye': (490, 290, 0.98), 'left_shoulder': (540, 380, 0.92), 'right_shoulder': (460, 380, 0.92), 'left_hip': (520, 550, 0.88), 'right_hip': (480, 550, 0.88), 'left_knee': (580, 700, 0.85), 'right_knee': (420, 700, 0.85), 'left_ankle': (600, 850, 0.82), 'right_ankle': (400, 850, 0.82) } def _check_head_distance( self, keypoints: dict, depth_map: Optional[np.ndarray] ) -> dict: """检查头部距仪表盘距离""" nose = keypoints.get('nose', (0, 0, 0)) head_x, head_y = nose[0], nose[1] if depth_map is not None: dashboard_depth = depth_map[ int(self.dashboard_y), int(head_x) ] head_depth = depth_map[int(head_y), int(head_x)] distance_mm = abs(dashboard_depth - head_depth) else: distance_pixels = abs(head_y - self.dashboard_y) distance_mm = distance_pixels * 2 return { 'is_dangerous': distance_mm < self.danger_distance_mm, 'distance_mm': distance_mm, 'head_position': (head_x, head_y) } def _check_feet_position(self, keypoints: dict) -> dict: """检查脚部是否在仪表盘上""" left_ankle = keypoints.get('left_ankle', (0, 0, 0)) right_ankle = keypoints.get('right_ankle', (0, 0, 0)) dashboard_y_min = self.dashboard_y - 100 dashboard_y_max = self.dashboard_y + 50 x_min, x_max = self.dashboard_x_range result = { 'is_dangerous': False, 'oop_type': OOPType.NORMAL } for ankle, ankle_type in [ (left_ankle, 'left'), (right_ankle, 'right') ]: ax, ay, conf = ankle if conf < 0.7: continue if dashboard_y_min <= ay <= dashboard_y_max: if ax < (x_min + x_max) / 3: result['oop_type'] = OOPType.FEET_ON_DASHBOARD_INBOARD result['is_dangerous'] = True elif ax < 2 * (x_min + x_max) / 3: result['oop_type'] = OOPType.FEET_ON_DASHBOARD_CENTER result['is_dangerous'] = True else: result['oop_type'] = OOPType.FEET_ON_DASHBOARD_OUTBOARD result['is_dangerous'] = True break return result
class OOPWarningManager: """异常姿态警告管理器""" def __init__(self): self.warning_interval = 15 * 60 self.last_warning_time = 0 self.warning_duration = 30 def should_warn( self, oop_result: dict, current_time: float ) -> dict: """ 判断是否需要警告 Euro NCAP 要求: - 30 秒内开始警告 - 视觉+听觉双重警告 - 每 15 分钟重复警告 """ if not oop_result['is_dangerous']: return {'alert': False} time_since_last = current_time - self.last_warning_time if time_since_last < self.warning_interval: return { 'alert': False, 'reason': 'waiting_for_interval' } self.last_warning_time = current_time return { 'alert': True, 'warning_type': 'visual_audible', 'message': self._get_warning_message(oop_result['oop_type']), 'duration': self.warning_duration } def _get_warning_message(self, oop_type: OOPType) -> str: """获取警告消息""" messages = { OOPType.HEAD_CLOSE_DASHBOARD: "请调整坐姿,远离仪表盘", OOPType.FEET_ON_DASHBOARD_INBOARD: "请将脚从仪表盘上移开", OOPType.FEET_ON_DASHBOARD_CENTER: "请将脚从仪表盘上移开", OOPType.FEET_ON_DASHBOARD_OUTBOARD: "请将脚从仪表盘上移开" } return messages.get(oop_type, "请调整坐姿")
|