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
| import numpy as np from typing import Tuple
class SeatbeltMisuseDetector: """ 安全带误用检测器 Euro NCAP 2026 三种误用场景: 1. Buckle only: 扣入但未绕身体 2. Lap only: 对角部分在背后 3. Fully behind back: 整条在背后 """ def __init__(self): self.misuse_types = { 'buckle_only': {'points': 2, 'description': '扣入但未绕身体'}, 'lap_only': {'points': 2, 'description': '对角部分在背后'}, 'behind_back': {'points': 1, 'description': '整条在背后'} } self.alert_duration = 0 self.alert_triggered = False def detect( self, buckle_engaged: bool, belt_visible_chest: float, belt_visible_lap: float, belt_visible_shoulder: float, person_present: bool ) -> dict: """检测安全带误用""" if not person_present: return {'status': 'EMPTY', 'alert': False} if not buckle_engaged: return {'status': 'UNBUCKLED', 'alert': True, 'alert_type': 'unbuckled'} if buckle_engaged and belt_visible_chest < 0.2 and belt_visible_lap < 0.2: return self._trigger_alert('buckle_only') if buckle_engaged and belt_visible_lap > 0.5 and belt_visible_shoulder < 0.3: return self._trigger_alert('lap_only') if buckle_engaged and belt_visible_chest < 0.1: return self._trigger_alert('behind_back') self.alert_duration = 0 self.alert_triggered = False return {'status': 'CORRECT', 'alert': False} def _trigger_alert(self, misuse_type: str) -> dict: self.alert_duration += 1 if self.alert_duration >= 30: self.alert_triggered = True return { 'status': 'MISUSE', 'misuse_type': misuse_type, 'description': self.misuse_types[misuse_type]['description'], 'points_at_risk': self.misuse_types[misuse_type]['points'], 'alert': self.alert_triggered, 'alert_duration_sec': self.alert_duration, 'visual_alert': True, 'audio_alert': self.alert_triggered }
if __name__ == "__main__": detector = SeatbeltMisuseDetector() tests = [ ("正确佩戴", True, 0.8, 0.7, 0.8, True), ("Buckle only", True, 0.1, 0.1, 0.1, True), ("Lap only", True, 0.1, 0.7, 0.2, True), ("Behind back", True, 0.05, 0.05, 0.05, True), ("未扣", False, 0, 0, 0, True), ] for name, buckle, chest, lap, shoulder, present in tests: for _ in range(31): result = detector.detect(buckle, chest, lap, shoulder, present) print(f"{name}: {result['status']}", end="") if 'misuse_type' in result: print(f" ({result['misuse_type']}, 风险{result['points_at_risk']}分)", end="") print(f" 告警: {result['alert']}")
|