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
| import numpy as np from dataclasses import dataclass
@dataclass class ImpairmentSignals: """酒驾损伤检测多信号输入""" gaze_deviation: float blink_irregularity: float head_sway: float facial_slackness: float response_delay: float steering_corrections: int lane_departures: int speed_variability: float cabin_alcohol_ppm: float
class AlcoholImpairmentDetector: """ Smart Eye风格的多信号融合酒驾检测 融合:行为AI + 车辆CAN + 空气质量 """ def __init__(self): self.weights = { 'gaze': 0.20, 'blink': 0.10, 'head': 0.15, 'facial': 0.10, 'response': 0.15, 'steering': 0.10, 'lane': 0.10, 'alcohol': 0.10, } self.thresholds = { 'caution': 0.35, 'warning': 0.55, 'impaired': 0.75, } def assess(self, signals: ImpairmentSignals) -> dict: """ 综合损伤评估 Returns: {'score': 0-1, 'level': str, 'action': str} """ gaze_score = min(signals.gaze_deviation / 30.0, 1.0) blink_score = min(abs(signals.blink_irregularity - 0.5) * 2, 1.0) head_score = min(signals.head_sway / 8.0, 1.0) facial_score = signals.facial_slackness response_score = min(signals.response_delay / 2.0, 1.0) steering_score = min(signals.steering_corrections / 20.0, 1.0) lane_score = min(signals.lane_departures / 10.0, 1.0) alcohol_score = min(signals.cabin_alcohol_ppm / 50.0, 1.0) score = ( self.weights['gaze'] * gaze_score + self.weights['blink'] * blink_score + self.weights['head'] * head_score + self.weights['facial'] * facial_score + self.weights['response'] * response_score + self.weights['steering'] * steering_score + self.weights['lane'] * lane_score + self.weights['alcohol'] * alcohol_score ) if score < self.thresholds['caution']: level, action = '正常', '无' elif score < self.thresholds['warning']: level, action = '注意', '声音提醒' elif score < self.thresholds['impaired']: level, action = '警告', '强烈提醒+限速' else: level, action = '损伤', '阻止启动/自动靠边' return { 'score': score, 'level': level, 'action': action, 'details': { 'gaze': gaze_score, 'blink': blink_score, 'head': head_score, 'facial': facial_score, 'response': response_score, 'steering': steering_score, 'lane': lane_score, 'alcohol': alcohol_score, } }
if __name__ == "__main__": detector = AlcoholImpairmentDetector() alert = ImpairmentSignals( gaze_deviation=8, blink_irregularity=0.5, head_sway=1, facial_slackness=0.1, response_delay=0.3, steering_corrections=3, lane_departures=0, speed_variability=0.05, cabin_alcohol_ppm=0 ) r1 = detector.assess(alert) print(f"清醒: score={r1['score']:.2f}, level={r1['level']}") drunk = ImpairmentSignals( gaze_deviation=25, blink_irregularity=0.8, head_sway=6, facial_slackness=0.7, response_delay=1.5, steering_corrections=15, lane_departures=5, speed_variability=0.25, cabin_alcohol_ppm=35 ) r2 = detector.assess(drunk) print(f"酒驾: score={r2['score']:.2f}, level={r2['level']}, action={r2['action']}") print(f" 各项: {r2['details']}")
|