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
| """ Minieye DFR 风格的酒驾损伤视觉检测框架 基于多特征融合的驾驶员酒精损伤评估 """
import numpy as np from dataclasses import dataclass from typing import List, Optional from enum import Enum
class ImpairmentLevel(Enum): NORMAL = 0 MILD_FATIGUE = 1 MODERATE_FATIGUE = 2 SEVERE_FATIGUE = 3 ALCOHOL_MILD = 4 ALCOHOL_MODERATE = 5 ALCOHOL_SEVERE = 6 UNKNOWN = -1
@dataclass class EyeFeatures: """眼动特征""" perclos: float blink_rate: float blink_interval_std: float saccade_freq: float saccade_velocity_mean: float gaze_dispersion: float
@dataclass class HeadFeatures: """头部特征""" pitch: float yaw: float roll: float nod_freq: float sway_amplitude: float movement_smoothness: float
@dataclass class FacialFeatures: """面部特征""" mouth_open: float facial_symmetry: float muscle_tone: float micro_expression_count: int
@dataclass class BehaviorFeatures: """驾驶行为特征""" steering_reversals: float lane_deviation: float speed_variability: float reaction_time: Optional[float]
@dataclass class DFRInput: """DFR 模型输入""" eye: EyeFeatures head: HeadFeatures facial: FacialFeatures behavior: BehaviorFeatures timestamp: float
class DFRModel: """ Driver Functional Readiness 综合评估模型 融合多模态特征评估驾驶员功能准备度 """ def __init__(self): self.weights = { 'eye': 0.25, 'head': 0.20, 'facial': 0.15, 'behavior': 0.40 } self.thresholds = { 'perclos_fatigue': 0.15, 'perclos_alcohol': 0.08, 'blink_rate_normal': (10, 20), 'blink_rate_alcohol': (25, 40), 'saccade_freq_normal': 2.0, 'saccade_freq_alcohol': 3.5, 'gaze_dispersion_alcohol': 0.6, 'sway_amplitude_alcohol': 5.0, 'steering_reversals_alcohol': 12.0, 'facial_symmetry_alcohol': 0.85, 'muscle_tone_alcohol': 0.6 } def assess(self, inputs: List[DFRInput]) -> tuple: """ 综合评估驾驶员功能准备度 Args: inputs: 最近 N 秒的特征序列 Returns: (ImpairmentLevel, confidence, scores_dict) """ if len(inputs) < 10: return (ImpairmentLevel.UNKNOWN, 0.0, {}) recent = inputs[-30:] fatigue_score = self._compute_fatigue_score(recent) alcohol_score = self._compute_alcohol_score(recent) distraction_score = self._compute_distraction_score(recent) scores = { 'fatigue': fatigue_score, 'alcohol': alcohol_score, 'distraction': distraction_score } max_score = max(fatigue_score, alcohol_score, distraction_score) if max_score < 0.3: level = ImpairmentLevel.NORMAL elif alcohol_score == max_score and alcohol_score > 0.5: if alcohol_score > 0.7: level = ImpairmentLevel.ALCOHOL_SEVERE elif alcohol_score > 0.6: level = ImpairmentLevel.ALCOHOL_MODERATE else: level = ImpairmentLevel.ALCOHOL_MILD elif fatigue_score == max_score and fatigue_score > 0.5: if fatigue_score > 0.7: level = ImpairmentLevel.SEVERE_FATIGUE elif fatigue_score > 0.6: level = ImpairmentLevel.MODERATE_FATIGUE else: level = ImpairmentLevel.MILD_FATIGUE else: level = ImpairmentLevel.NORMAL confidence = min(max_score * 0.9 + 0.1, 1.0) return (level, confidence, scores) def _compute_fatigue_score(self, inputs: List[DFRInput]) -> float: """计算疲劳分数 (0-1)""" perclos = np.mean([i.eye.perclos for i in inputs]) blink_rate = np.mean([i.eye.blink_rate for i in inputs]) nod_freq = np.mean([i.head.nod_freq for i in inputs]) score = 0.0 if perclos > self.thresholds['perclos_fatigue']: score += 0.4 * min(perclos / 0.3, 1.0) if blink_rate > 20: score += 0.3 * min((blink_rate - 20) / 20, 1.0) if nod_freq > 0.5: score += 0.3 * min(nod_freq / 2.0, 1.0) return min(score, 1.0) def _compute_alcohol_score(self, inputs: List[DFRInput]) -> float: """ 计算酒精损伤分数 (0-1) 关键区分指标: - PERCLOS 中等(不如疲劳高) - 眨眼频率高且不规律 - 扫视频繁但精度差 - 头部摇摆幅度大 - 方向盘微修正多 - 面部对称性和张力下降 """ perclos = np.mean([i.eye.perclos for i in inputs]) blink_rate = np.mean([i.eye.blink_rate for i in inputs]) blink_irregularity = np.mean([i.eye.blink_interval_std for i in inputs]) saccade_freq = np.mean([i.eye.saccade_freq for i in inputs]) gaze_disp = np.mean([i.eye.gaze_dispersion for i in inputs]) sway = np.mean([abs(i.head.sway_amplitude) for i in inputs]) steering_rev = np.mean([i.behavior.steering_reversals for i in inputs]) symmetry = np.mean([i.facial.facial_symmetry for i in inputs]) muscle = np.mean([i.facial.muscle_tone for i in inputs]) score = 0.0 if blink_rate > 25: score += 0.15 * min((blink_rate - 25) / 15, 1.0) if blink_irregularity > 0.5: score += 0.10 * min(blink_irregularity, 1.0) if saccade_freq > 3.0: score += 0.15 * min((saccade_freq - 3.0) / 2.0, 1.0) if gaze_disp > 0.5: score += 0.10 * min(gaze_disp, 1.0) if sway > 3.0: score += 0.15 * min(sway / 10.0, 1.0) if steering_rev > 8.0: score += 0.20 * min((steering_rev - 8.0) / 10.0, 1.0) if symmetry < 0.85: score += 0.10 * (1.0 - symmetry) if muscle < 0.7: score += 0.05 * (1.0 - muscle) return min(score, 1.0) def _compute_distraction_score(self, inputs: List[DFRInput]) -> float: """计算分心分数 (0-1)""" gaze_away = np.mean([i.eye.gaze_dispersion for i in inputs]) head_yaw = np.mean([abs(i.head.yaw) for i in inputs]) score = 0.0 if gaze_away > 0.4: score += 0.5 * min(gaze_away, 1.0) if head_yaw > 15: score += 0.5 * min(head_yaw / 30, 1.0) return min(score, 1.0)
if __name__ == "__main__": model = DFRModel() normal_inputs = [ DFRInput( eye=EyeFeatures(perclos=0.05, blink_rate=15, blink_interval_std=0.2, saccade_freq=1.5, saccade_velocity_mean=200, gaze_dispersion=0.3), head=HeadFeatures(pitch=0, yaw=0, roll=0, nod_freq=0.1, sway_amplitude=1.0, movement_smoothness=0.9), facial=FacialFeatures(mouth_open=0.1, facial_symmetry=0.95, muscle_tone=0.85, micro_expression_count=0), behavior=BehaviorFeatures(steering_reversals=5, lane_deviation=0.1, speed_variability=0.05, reaction_time=None), timestamp=i ) for i in range(30) ] level, conf, scores = model.assess(normal_inputs) print(f"正常驾驶: {level.name}, 置信度={conf:.2f}, 分数={scores}") alcohol_inputs = [ DFRInput( eye=EyeFeatures(perclos=0.10, blink_rate=32, blink_interval_std=0.8, saccade_freq=4.0, saccade_velocity_mean=150, gaze_dispersion=0.7), head=HeadFeatures(pitch=5, yaw=3, roll=2, nod_freq=0.3, sway_amplitude=6.0, movement_smoothness=0.5), facial=FacialFeatures(mouth_open=0.15, facial_symmetry=0.80, muscle_tone=0.55, micro_expression_count=3), behavior=BehaviorFeatures(steering_reversals=14, lane_deviation=0.3, speed_variability=0.15, reaction_time=None), timestamp=i ) for i in range(30) ] level, conf, scores = model.assess(alcohol_inputs) print(f"酒精损伤: {level.name}, 置信度={conf:.2f}, 分数={scores}")
|