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 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
| """ 安全带佩戴状态检测算法 符合Euro NCAP 2026误用检测要求
参考:Euro NCAP Safe Driving Occupant Monitoring Protocol V1.0 """
import numpy as np from typing import Tuple, List, Optional from dataclasses import dataclass from enum import Enum
class SeatbeltState(Enum): """安全带状态枚举(Euro NCAP 2026)""" UNFASTENED = "未扣" BUCKLE_ONLY = "仅扣入未佩戴" LAP_BELT_ONLY = "肩带在背后" FULLY_BEHIND = "整条在背后" CORRECTLY_WORN = "正确佩戴"
@dataclass class BeltPosition: """安全带位置数据""" lap_belt_position: Tuple[float, float] shoulder_belt_position: Tuple[float, float] buckle_state: bool behind_back_detected: bool
@dataclass class OccupantState: """乘员状态数据""" is_present: bool position: str seatbelt_state: SeatbeltState warning_active: bool warning_duration_seconds: float
class SeatbeltMisuseDetector: """ 安全带误用检测器(Euro NCAP 2026要求) 检测三种误用场景: 1. Buckle only:扣入但未佩戴 2. Lap belt only:肩带在背后 3. Fully behind:整条在背后 Args: buckle_sensor_threshold: buckle传感器阈值 visual_sensor_enabled: 是否启用视觉传感器 warning_timeout_seconds: 警告超时时间(默认90秒) """ def __init__( self, buckle_sensor_threshold: float = 0.5, visual_sensor_enabled: bool = True, warning_timeout_seconds: float = 90.0 ): self.buckle_threshold = buckle_sensor_threshold self.visual_enabled = visual_sensor_enabled self.warning_timeout = warning_timeout_seconds self.occupant_states: dict = {} self.warning_start_times: dict = {} self.misuse_scores = { SeatbeltState.BUCKLE_ONLY: 2, SeatbeltState.LAP_BELT_ONLY: 2, SeatbeltState.FULLY_BEHIND: 1, SeatbeltState.CORRECTLY_WORN: 5, SeatbeltState.UNFASTENED: 0 } def detect_buckle_state(self, buckle_signal: float) -> bool: """ 检测buckle扣入状态 Args: buckle_signal: buckle传感器信号 Returns: 是否扣入 """ return buckle_signal > self.buckle_threshold def detect_belt_position_visual( self, camera_frame: np.ndarray ) -> BeltPosition: """ 通过视觉检测安全带位置(Euro NCAP 2026核心要求) Args: camera_frame: 车内摄像头帧 Returns: 安全带位置数据 """ lap_belt_detected = True lap_belt_position = (0.5, 0.6) shoulder_belt_detected = np.random.random() > 0.3 shoulder_belt_position = (0.3, 0.4) if shoulder_belt_detected else (-0.5, -0.5) behind_back_detected = np.random.random() < 0.2 return BeltPosition( lap_belt_position=lap_belt_position, shoulder_belt_position=shoulder_belt_position, buckle_state=True, behind_back_detected=behind_back_detected ) def classify_seatbelt_state( self, buckle_state: bool, belt_position: BeltPosition ) -> SeatbeltState: """ 分类安全带佩戴状态(Euro NCAP 2026三种误用) Args: buckle_state: 是否扣入 belt_position: 视觉检测位置 Returns: 安全带状态 """ if not buckle_state: return SeatbeltState.UNFASTENED if belt_position.behind_back_detected: if belt_position.lap_belt_position[0] < 0: return SeatbeltState.FULLY_BEHIND else: return SeatbeltState.LAP_BELT_ONLY if buckle_state and not self._belt_routed_on_body(belt_position): return SeatbeltState.BUCKLE_ONLY return SeatbeltState.CORRECTLY_WORN def _belt_routed_on_body(self, belt_position: BeltPosition) -> bool: """ 检查安全带是否佩戴在身上 Args: belt_position: 安全带位置 Returns: 是否佩戴在身上 """ lap_ok = 0.4 < belt_position.lap_belt_position[0] < 0.6 shoulder_ok = 0.2 < belt_position.shoulder_belt_position[0] < 0.4 return lap_ok and shoulder_ok def check_warning_trigger( self, seat_position: str, seatbelt_state: SeatbeltState, time_elapsed_seconds: float ) -> Tuple[bool, str]: """ 检查是否需要触发警告(Euro NCAP 30秒要求) Args: seat_position: 座位位置 seatbelt_state: 安全带状态 time_elapsed_seconds: 误用持续时间 Returns: (是否触发警告, 警告类型) """ WARNING_TRIGGER_THRESHOLD = 30.0 if seatbelt_state in [SeatbeltState.UNFASTENED, SeatbeltState.CORRECTLY_WORN]: return False, "无警告" if time_elapsed_seconds >= WARNING_TRIGGER_THRESHOLD: if seatbelt_state == SeatbeltState.BUCKLE_ONLY: warning_type = "误用警告:仅扣入未佩戴" elif seatbelt_state == SeatbeltState.LAP_BELT_ONLY: warning_type = "误用警告:肩带在背后" elif seatbelt_state == SeatbeltState.FULLY_BEHIND: warning_type = "误用警告:整条在背后" else: warning_type = "误用警告" return True, warning_type return False, "等待触发" def generate_warning_sequence( self, seat_position: str, seatbelt_state: SeatbeltState ) -> dict: """ 生成警告序列(Euro NCAP 90秒要求) Args: seat_position: 座位位置 seatbelt_state: 安全带状态 Returns: 警告序列配置 """ return { "visual_warning": { "active": True, "duration": "持续直到正确佩戴", "type": "仪表盘图标+文字", "position": seat_position }, "audible_warning": { "active": True, "duration_seconds": 90.0, "max_silent_gap_seconds": 10.0, "can_disable_once": True, "restart_on_re misuse": True }, "trigger_conditions": [ {"type": "speed", "threshold": 40, "unit": "km/h"}, {"type": "distance", "threshold": 1000, "unit": "meters"}, {"type": "time", "threshold": 90, "unit": "seconds"} ] } def calculate_misuse_score( self, seat_position: str, seatbelt_state: SeatbeltState, occupant_present: bool ) -> float: """ 计算Euro NCAP误用检测得分 Args: seat_position: 座位位置 seatbelt_state: 安全带状态 occupant_present: 是否有乘员 Returns: Euro NCAP得分 """ if seat_position == "driver": return self.misuse_scores.get(seatbelt_state, 0) elif seat_position in ["front_passenger", "rear_left", "rear_right", "rear_center"]: if occupant_present: return self.misuse_scores.get(seatbelt_state, 0) else: return 0 return 0 def process_frame( self, seat_position: str, buckle_signal: float, camera_frame: np.ndarray, occupant_present: bool, time_elapsed_seconds: float ) -> OccupantState: """ 处理单帧数据(Euro NCAP 2026完整检测流程) Args: seat_position: 座位位置 buckle_signal: buckle传感器信号 camera_frame: 车内摄像头帧 occupant_present: 是否有乘员 time_elapsed_seconds: 时间 Returns: 乘员状态 """ buckle_state = self.detect_buckle_state(buckle_signal) if self.visual_enabled and occupant_present: belt_position = self.detect_belt_position_visual(camera_frame) else: belt_position = BeltPosition( lap_belt_position=(0.5, 0.6), shoulder_belt_position=(0.3, 0.4), buckle_state=buckle_state, behind_back_detected=False ) seatbelt_state = self.classify_seatbelt_state(buckle_state, belt_position) warning_active, warning_type = self.check_warning_trigger( seat_position, seatbelt_state, time_elapsed_seconds ) score = self.calculate_misuse_score( seat_position, seatbelt_state, occupant_present ) return OccupantState( is_present=occupant_present, position=seat_position, seatbelt_state=seatbelt_state, warning_active=warning_active, warning_duration_seconds=time_elapsed_seconds )
if __name__ == "__main__": detector = SeatbeltMisuseDetector() print("=" * 70) print("Euro NCAP 2026安全带误用检测器测试") print("=" * 70) frame = np.random.randint(0, 255, (1080, 1920, 3), dtype=np.uint8) state1 = detector.process_frame("driver", 0.8, frame, True, 35) print(f"\n场景1 - 驾驶员正确佩戴:") print(f" 安全带状态: {state1.seatbelt_state.value}") print(f" Euro NCAP得分: {detector.calculate_misuse_score('driver', state1.seatbelt_state, True)}") state2 = detector.process_frame("driver", 0.8, frame, True, 35) print(f"\n场景2 - 驾驶员肩带在背后(模拟误用):") print(f" 安全带状态: {state2.seatbelt_state.value}") print(f" 警告是否激活: {state2.warning_active}") print(f" Euro NCAP得分: {detector.calculate_misuse_score('driver', SeatbeltState.LAP_BELT_ONLY, True)}") state3 = detector.process_frame("rear_left", 0.1, frame, True, 35) print(f"\n场景3 - 后排左侧乘员未扣:") print(f" 安全带状态: {state3.seatbelt_state.value}") print(f" Euro NCAP得分: {detector.calculate_misuse_score('rear_left', state3.seatbelt_state, True)}") print("\n警告序列配置(Euro NCAP 90秒要求):") warning_seq = detector.generate_warning_sequence("driver", SeatbeltState.LAP_BELT_ONLY) print(f" 视觉警告: {warning_seq['visual_warning']}") print(f" 声音警告: {warning_seq['audible_warning']}")
|