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
| """ 多模态酒精损伤检测融合算法
数据源: 1. 眼动追踪(DMS摄像头) 2. 面部温度(红外传感器) 3. 转向行为(CAN总线) 4. 握持检测(方向盘传感器) """
import numpy as np from typing import Dict, List, Tuple from dataclasses import dataclass from enum import Enum
class ImpairmentLevel(Enum): """损伤等级""" NORMAL = 0 MILD = 1 MODERATE = 2 SEVERE = 3
@dataclass class EyeMetrics: """眼动指标""" blink_rate: float blink_duration: float gaze_deviation: float reaction_time: float perclos: float
@dataclass class VehicleMetrics: """车辆行为指标""" steering_corrections: int lane_keeping_error: float speed_variability: float brake_reaction_time: float
@dataclass class Biometrics: """生物指标""" facial_temp: float grip_strength: float grip_pattern: str
class MultimodalImpairmentDetector: """ 多模态酒精损伤检测器 融合策略: 1. 单模态阈值检测 2. 多模态加权融合 3. 时序一致性校验 """ THRESHOLDS = { 'blink_rate_low': 12.0, 'blink_duration_high': 0.20, 'reaction_time_high': 0.80, 'perclos_high': 30.0, 'steering_corrections_high': 6, } WEIGHTS = { 'eye_metrics': 0.40, 'vehicle_metrics': 0.35, 'biometrics': 0.25 } def __init__(self): self.history = [] def detect( self, eye: EyeMetrics, vehicle: VehicleMetrics, bio: Biometrics, window_sec: int = 60 ) -> Tuple[ImpairmentLevel, Dict]: """ 检测酒精损伤 Args: eye: 眼动指标 vehicle: 车辆行为指标 bio: 生物指标 window_sec: 分析窗口(秒) Returns: level: 损伤等级 details: 检测详情 """ eye_score = self._score_eye(eye) vehicle_score = self._score_vehicle(vehicle) bio_score = self._score_biometrics(bio) fusion_score = ( self.WEIGHTS['eye_metrics'] * eye_score + self.WEIGHTS['vehicle_metrics'] * vehicle_score + self.WEIGHTS['biometrics'] * bio_score ) self.history.append(fusion_score) if len(self.history) > 10: self.history.pop(0) smooth_score = np.mean(self.history) level = self._classify_level(smooth_score) details = { 'eye_score': eye_score, 'vehicle_score': vehicle_score, 'bio_score': bio_score, 'fusion_score': fusion_score, 'smooth_score': smooth_score, 'contributions': { 'eye': eye_score * self.WEIGHTS['eye_metrics'], 'vehicle': vehicle_score * self.WEIGHTS['vehicle_metrics'], 'bio': bio_score * self.WEIGHTS['biometrics'] } } return level, details def _score_eye(self, eye: EyeMetrics) -> float: """ 眼动指标评分 Returns: score: 0-1,越高表示损伤越严重 """ score = 0.0 if eye.blink_rate < self.THRESHOLDS['blink_rate_low']: score += 0.2 * (self.THRESHOLDS['blink_rate_low'] - eye.blink_rate) / self.THRESHOLDS['blink_rate_low'] if eye.blink_duration > self.THRESHOLDS['blink_duration_high']: score += 0.3 * (eye.blink_duration - self.THRESHOLDS['blink_duration_high']) / 0.1 if eye.reaction_time > self.THRESHOLDS['reaction_time_high']: score += 0.3 * (eye.reaction_time - self.THRESHOLDS['reaction_time_high']) / 0.5 if eye.perclos > self.THRESHOLDS['perclos_high']: score += 0.2 * (eye.perclos - self.THRESHOLDS['perclos_high']) / 20.0 return min(score, 1.0) def _score_vehicle(self, vehicle: VehicleMetrics) -> float: """ 车辆行为指标评分 """ score = 0.0 if vehicle.steering_corrections > self.THRESHOLDS['steering_corrections_high']: score += 0.4 * (vehicle.steering_corrections - self.THRESHOLDS['steering_corrections_high']) / 5 if vehicle.lane_keeping_error > 0.3: score += 0.3 * vehicle.lane_keeping_error if vehicle.speed_variability > 15: score += 0.3 * vehicle.speed_variability / 30 return min(score, 1.0) def _score_biometrics(self, bio: Biometrics) -> float: """ 生物指标评分 """ score = 0.0 if bio.facial_temp > 37.0: score += 0.4 * (bio.facial_temp - 37.0) / 2.0 if bio.grip_strength < 0.3: score += 0.3 * (0.3 - bio.grip_strength) / 0.3 return min(score, 1.0) def _classify_level(self, score: float) -> ImpairmentLevel: """ 等级分类 """ if score < 0.3: return ImpairmentLevel.NORMAL elif score < 0.5: return ImpairmentLevel.MILD elif score < 0.7: return ImpairmentLevel.MODERATE else: return ImpairmentLevel.SEVERE
if __name__ == "__main__": detector = MultimodalImpairmentDetector() eye_data = EyeMetrics( blink_rate=10.0, blink_duration=0.25, gaze_deviation=20.0, reaction_time=1.0, perclos=35.0 ) vehicle_data = VehicleMetrics( steering_corrections=8, lane_keeping_error=0.4, speed_variability=20.0, brake_reaction_time=1.2 ) bio_data = Biometrics( facial_temp=37.5, grip_strength=0.2, grip_pattern='weak' ) level, details = detector.detect(eye_data, vehicle_data, bio_data) print(f"检测等级: {level.name}") print(f"融合得分: {details['fusion_score']:.2f}") print(f"平滑得分: {details['smooth_score']:.2f}") print(f"贡献分析: 眼动 {details['contributions']['eye']:.2f}, " f"车辆 {details['contributions']['vehicle']:.2f}, " f"生物 {details['contributions']['bio']:.2f}")
|