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
| """ Smart Eye酒精损伤检测算法 基于眼动和面部表情的多模态分析
关键技术: 1. 眼动特征:扫视速度、注视稳定性、瞳孔直径 2. 面部特征:面部松弛度、表情异常、头部姿态 3. 融合模型:多模态BAC估计 """
import numpy as np import torch import torch.nn as nn from typing import Dict, Tuple
class AlcoholImpairmentDetector(nn.Module): """ 酒精损伤检测器 输入: - 眼动特征:扫视速度、注视稳定性、瞳孔直径、眨眼频率 - 面部特征:面部关键点位置、表情分类、头部姿态 输出: - BAC估计值:0-0.15% - 损伤概率:0-1 - 警告等级:0-3 """ def __init__(self, config: Dict = None): super().__init__() config = config or {} self.gaze_encoder = nn.Sequential( nn.Linear(8, 64), nn.ReLU(), nn.Linear(64, 128), nn.ReLU() ) self.face_encoder = nn.Sequential( nn.Linear(68 * 2, 256), nn.ReLU(), nn.Linear(256, 128), nn.ReLU() ) self.fusion = nn.Sequential( nn.Linear(256, 128), nn.ReLU(), nn.Dropout(0.3), nn.Linear(128, 64), nn.ReLU() ) self.bac_regressor = nn.Linear(64, 1) self.impairment_classifier = nn.Linear(64, 4) def forward(self, gaze_features: torch.Tensor, face_landmarks: torch.Tensor) -> Dict[str, torch.Tensor]: """ 前向传播 Args: gaze_features: (B, 8) 眼动特征 [saccade_velocity, fixation_stability, pupil_diameter, blink_rate, gaze_variance, saccade_latency, smooth_pursuit_gain, vergence_error] face_landmarks: (B, 68, 2) 面部关键点 Returns: outputs: { 'bac_estimate': (B,) BAC估计值, 'impairment_level': (B,) 损伤等级, 'impairment_prob': (B, 4) 各等级概率 } """ gaze_feat = self.gaze_encoder(gaze_features) face_feat = self.face_encoder(face_landmarks.view(gaze_features.size(0), -1)) fused_feat = torch.cat([gaze_feat, face_feat], dim=1) fused_feat = self.fusion(fused_feat) bac_estimate = self.bac_regressor(fused_feat).squeeze(-1) impairment_logits = self.impairment_classifier(fused_feat) impairment_prob = torch.softmax(impairment_logits, dim=1) impairment_level = torch.argmax(impairment_prob, dim=1) return { 'bac_estimate': bac_estimate, 'impairment_level': impairment_level, 'impairment_prob': impairment_prob }
class GazeFeatureExtractor: """ 眼动特征提取器 关键特征: 1. 扫视速度(Saccade Velocity):酒精导致扫视变慢 2. 注视稳定性(Fixation Stability):酒精导致注视不稳 3. 瞳孔直径(Pupil Diameter):酒精导致瞳孔扩大 4. 眨眼频率(Blink Rate):酒精改变眨眼模式 """ def __init__(self, window_sec: float = 10.0, fs: int = 60): """ Args: window_sec: 分析窗口长度(秒) fs: 眼动采样率(Hz) """ self.window_sec = window_sec self.fs = fs self.window_samples = int(window_sec * fs) def extract(self, gaze_sequence: np.ndarray) -> np.ndarray: """ 提取眼动特征 Args: gaze_sequence: (T, 4) 眼动数据序列 [:, 0] = 时间戳 [:, 1] = 注视点X [:, 2] = 注视点Y [:, 3] = 瞳孔直径 Returns: features: (8,) 特征向量 """ if len(gaze_sequence) < self.window_samples: gaze_window = gaze_sequence else: gaze_window = gaze_sequence[-self.window_samples:] features = np.zeros(8) features[0] = self._compute_saccade_velocity(gaze_window) features[1] = self._compute_fixation_stability(gaze_window) features[2] = self._compute_pupil_diameter(gaze_window) features[3] = self._compute_blink_rate(gaze_window) features[4] = self._compute_gaze_variance(gaze_window) features[5] = self._compute_saccade_latency(gaze_window) features[6] = self._compute_smooth_pursuit_gain(gaze_window) features[7] = self._compute_vergence_error(gaze_window) return features def _compute_saccade_velocity(self, gaze: np.ndarray) -> float: """ 计算扫视速度 酒精效应:扫视速度降低,峰值速度下降 """ x, y = gaze[:, 1], gaze[:, 2] t = gaze[:, 0] dx = np.diff(x) dy = np.diff(y) dt = np.diff(t) velocity = np.sqrt(dx**2 + dy**2) / (dt + 1e-6) peak_velocity = np.percentile(velocity, 95) return peak_velocity def _compute_fixation_stability(self, gaze: np.ndarray) -> float: """ 计算注视稳定性(离散度) 酒精效应:注视不稳,方差增大 """ x, y = gaze[:, 1], gaze[:, 2] velocity = np.sqrt(np.diff(x)**2 + np.diff(y)**2) * self.fs fixation_mask = velocity < 30 if fixation_mask.sum() < 10: return 1.0 fixation_x = x[:-1][fixation_mask] fixation_y = y[:-1][fixation_mask] stability = np.var(fixation_x) + np.var(fixation_y) return np.sqrt(stability) def _compute_pupil_diameter(self, gaze: np.ndarray) -> float: """ 计算瞳孔直径(归一化) 酒精效应:瞳孔扩大 """ pupil = gaze[:, 3] pupil_clean = pupil[pupil > pupil.mean() - 2 * pupil.std()] baseline = pupil_clean[0] if len(pupil_clean) > 0 else 1.0 return np.mean(pupil_clean) / baseline def _compute_blink_rate(self, gaze: np.ndarray) -> float: """ 计算眨眼频率(次/分钟) 酒精效应:眨眼频率改变 """ pupil = gaze[:, 3] t = gaze[:, 0] pupil_diff = np.diff(pupil) blink_threshold = -pupil.std() * 2 blinks = (pupil_diff < blink_threshold).sum() duration_min = (t[-1] - t[0]) / 60.0 return blinks / duration_min if duration_min > 0 else 0 def _compute_gaze_variance(self, gaze: np.ndarray) -> float: """注视点总方差""" x, y = gaze[:, 1], gaze[:, 2] return np.var(x) + np.var(y) def _compute_saccade_latency(self, gaze: np.ndarray) -> float: """扫视潜伏期(简化计算)""" return 150.0 def _compute_smooth_pursuit_gain(self, gaze: np.ndarray) -> float: """平滑追踪增益(简化计算)""" return 0.95 def _compute_vergence_error(self, gaze: np.ndarray) -> float: """辐辏误差(需要双眼数据)""" return 0.5
if __name__ == "__main__": """ 模拟酒精损伤检测 """ device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') model = AlcoholImpairmentDetector().to(device) print(f"模型参数量: {sum(p.numel() for p in model.parameters()) / 1e6:.2f}M") batch_size = 4 gaze_normal = torch.tensor([ [200.0, 0.5, 1.0, 15.0, 100.0, 150.0, 0.95, 0.5], [180.0, 0.6, 1.0, 16.0, 120.0, 155.0, 0.94, 0.6], [190.0, 0.55, 1.0, 14.0, 110.0, 152.0, 0.96, 0.5], [195.0, 0.52, 1.0, 15.5, 105.0, 148.0, 0.95, 0.5] ]).to(device) gaze_alcohol = torch.tensor([ [120.0, 2.5, 1.3, 25.0, 500.0, 200.0, 0.70, 2.0], [100.0, 3.0, 1.4, 28.0, 600.0, 220.0, 0.65, 2.5], [110.0, 2.8, 1.35, 26.0, 550.0, 210.0, 0.68, 2.2], [115.0, 2.7, 1.38, 27.0, 580.0, 215.0, 0.67, 2.3] ]).to(device) face_normal = torch.randn(batch_size, 68, 2).to(device) face_alcohol = torch.randn(batch_size, 68, 2).to(device) model.eval() with torch.no_grad(): result_normal = model(gaze_normal, face_normal) result_alcohol = model(gaze_alcohol, face_alcohol) print("\n" + "=" * 60) print("正常驾驶员检测结果") print("=" * 60) for i in range(batch_size): bac = result_normal['bac_estimate'][i].item() level = result_normal['impairment_level'][i].item() prob = result_normal['impairment_prob'][i].cpu().numpy() print(f"\n样本{i+1}:") print(f" BAC估计: {bac:.3f}%") print(f" 损伤等级: {level} (0=正常, 1=轻度, 2=中度, 3=重度)") print(f" 各等级概率: {prob}") print("\n" + "=" * 60) print("酒精损伤驾驶员检测结果") print("=" * 60) for i in range(batch_size): bac = result_alcohol['bac_estimate'][i].item() level = result_alcohol['impairment_level'][i].item() prob = result_alcohol['impairment_prob'][i].cpu().numpy() print(f"\n样本{i+1}:") print(f" BAC估计: {bac:.3f}%") print(f" 损伤等级: {level} (0=正常, 1=轻度, 2=中度, 3=重度)") print(f" 各等级概率: {prob}") print("\n" + "=" * 60) print("警告等级判定") print("=" * 60) for i in range(batch_size): bac = result_alcohol['bac_estimate'][i].item() if bac > 0.08: alert = "三级警告:建议停车(BAC > 0.08%)" elif bac > 0.05: alert = "二级警告:振动提醒(BAC > 0.05%)" elif bac > 0.02: alert = "一级警告:声音提示(BAC > 0.02%)" else: alert = "正常" print(f"样本{i+1}: {alert}")
|