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
| """ Vision-based酒精损伤检测算法 基于面部特征和眼动行为的多模态融合
论文来源: - "Estimating Blood Alcohol Level Through Facial Features" (WACV 2024) - Smart Eye Alcohol Impairment Detection (2025)
检测原理: 1. 瞳孔震颤检测(nystagmus) 2. 眼睑下垂检测(ptosis) 3. 扫视速度异常检测 4. 面部血流分析(可选) """
import torch import torch.nn as nn import torch.nn.functional as F import numpy as np from typing import Tuple, Dict, List
class PupilNystagmusDetector(nn.Module): """ 瞳孔震颤检测器 酒精损伤会导致瞳孔不规则的震颤运动(nystagmus) 检测方法: 1. 追踪瞳孔中心轨迹 2. 分析震颤频率和幅度 3. 与基线对比判定异常 论文依据: Hirsch et al., "Alcohol and eye movements" (2023) """ def __init__( self, seq_length: int = 60, feature_dim: int = 32, nystagmus_threshold: float = 0.15 ): super().__init__() self.seq_length = seq_length self.nystagmus_threshold = nystagmus_threshold self.trajectory_encoder = nn.LSTM( input_size=2, hidden_size=feature_dim, num_layers=2, batch_first=True, bidirectional=True ) self.nystagmus_head = nn.Sequential( nn.Linear(feature_dim * 2, 64), nn.ReLU(), nn.Linear(64, 2) ) def forward( self, pupil_trajectory: torch.Tensor ) -> Tuple[torch.Tensor, Dict]: """ 前向传播 Args: pupil_trajectory: [B, T, 2] 瞳孔中心轨迹 Returns: logits: [B, 2] 分类logits metrics: 震颤幅度等指标 """ B, T, _ = pupil_trajectory.shape trajectory_feat, _ = self.trajectory_encoder(pupil_trajectory) final_feat = trajectory_feat[:, -1, :] logits = self.nystagmus_head(final_feat) trajectory_std = torch.std(pupil_trajectory, dim=1) nystagmus_amplitude = torch.mean(trajectory_std, dim=1) metrics = { 'nystagmus_amplitude': nystagmus_amplitude, 'trajectory_std': trajectory_std } return logits, metrics
class SaccadeVelocityAnalyzer(nn.Module): """ 扫视速度分析器 酒精损伤会导致扫视运动(saccade)速度下降和不连续 检测方法: 1. 检测扫视事件(快速眼动) 2. 计算扫视峰值速度 3. 与正常基线对比 正常值: 峰值速度:200-700°/s 酒精损伤: 峰值速度降低20-40% """ def __init__( self, velocity_threshold: float = 150.0, fps: float = 30.0 ): super().__init__() self.velocity_threshold = velocity_threshold self.fps = fps def forward( self, gaze_sequence: torch.Tensor, return_events: bool = False ) -> Dict: """ 分析扫视速度 Args: gaze_sequence: [B, T, 2] 视线序列(归一化坐标) return_events: 是否返回扫视事件 Returns: metrics: 扫视速度指标 """ B, T, _ = gaze_sequence.shape gaze_diff = gaze_sequence[:, 1:] - gaze_sequence[:, :-1] gaze_velocity = gaze_diff * 30.0 * self.fps velocity_magnitude = torch.norm(gaze_velocity, dim=-1) saccade_mask = velocity_magnitude > self.velocity_threshold saccade_velocities = [] for i in range(B): saccade_vels = velocity_magnitude[i, saccade_mask[i]] if len(saccade_vels) > 0: avg_vel = torch.mean(saccade_vels) else: avg_vel = torch.tensor(0.0, device=gaze_sequence.device) saccade_velocities.append(avg_vel) saccade_velocities = torch.stack(saccade_velocities) saccade_count = torch.sum(saccade_mask, dim=1).float() metrics = { 'avg_saccade_velocity': saccade_velocities, 'saccade_count': saccade_count, 'velocity_magnitude': velocity_magnitude } if return_events: metrics['saccade_mask'] = saccade_mask return metrics
class AlcoholImpairmentDetector(nn.Module): """ 综合酒精损伤检测器 融合多模态特征: 1. 瞳孔震颤 2. 扫视速度 3. 眼睑开度 4. 面部血流(可选) 输出: - 损伤等级:正常/轻度/中度/重度 - BAC估计值(Blood Alcohol Concentration) """ def __init__( self, seq_length: int = 60, num_features: int = 128 ): super().__init__() self.nystagmus_detector = PupilNystagmusDetector(seq_length) self.saccade_analyzer = SaccadeVelocityAnalyzer() self.fusion = nn.Sequential( nn.Linear(32 + 2 + 1, num_features), nn.ReLU(), nn.Linear(num_features, num_features // 2), nn.ReLU() ) self.impairment_head = nn.Linear(num_features // 2, 4) self.bac_head = nn.Linear(num_features // 2, 1) def forward( self, pupil_trajectory: torch.Tensor, gaze_sequence: torch.Tensor, eyelid_openness: torch.Tensor ) -> Dict: """ 前向传播 Args: pupil_trajectory: [B, T, 2] 瞳孔轨迹 gaze_sequence: [B, T, 2] 视线序列 eyelid_openness: [B, T] 眼睑开度(0-1) Returns: result: 包含损伤等级、BAC估计等 """ B = pupil_trajectory.size(0) nystagmus_logits, nystagmus_metrics = self.nystagmus_detector(pupil_trajectory) nystagmus_feat = F.relu(nystagmus_logits) saccade_metrics = self.saccade_analyzer(gaze_sequence) saccade_feat = torch.stack([ saccade_metrics['avg_saccade_velocity'], saccade_metrics['saccade_count'] ], dim=-1) avg_eyelid = torch.mean(eyelid_openness, dim=1, keepdim=True) fused_input = torch.cat([nystagmus_feat, saccade_feat, avg_eyelid], dim=-1) fused_feat = self.fusion(fused_input) impairment_logits = self.impairment_head(fused_feat) impairment_probs = F.softmax(impairment_logits, dim=-1) impairment_level = torch.argmax(impairment_probs, dim=-1) bac_estimate = torch.sigmoid(self.bac_head(fused_feat)) * 0.20 result = { 'impairment_level': impairment_level, 'impairment_probs': impairment_probs, 'bac_estimate': bac_estimate, 'nystagmus_amplitude': nystagmus_metrics['nystagmus_amplitude'], 'avg_saccade_velocity': saccade_metrics['avg_saccade_velocity'], 'avg_eyelid_openness': avg_eyelid } return result
if __name__ == "__main__": B, T = 4, 60 pupil_normal = torch.randn(B, T, 2) * 0.02 gaze_normal = torch.randn(B, T, 2) * 0.1 eyelid_normal = torch.rand(B, T) * 0.2 + 0.7 detector = AlcoholImpairmentDetector(seq_length=T) result = detector(pupil_normal, gaze_normal, eyelid_normal) print("=" * 60) print("酒精损伤检测结果") print("=" * 60) print(f"损伤等级: {result['impairment_level']}") print(f"损伤概率: {result['impairment_probs']}") print(f"BAC估计: {result['bac_estimate'].squeeze() * 100:.2f}%") print(f"震颤幅度: {result['nystagmus_amplitude']:.4f}") print(f"平均扫视速度: {result['avg_saccade_velocity']:.1f}°/s") print(f"平均眼睑开度: {result['avg_eyelid_openness'].squeeze():.2f}") print("\n" + "=" * 60) print("模拟酒精损伤数据") print("=" * 60) pupil_impaired = torch.randn(B, T, 2) * 0.08 gaze_impaired = torch.randn(B, T, 2) * 0.05 eyelid_impaired = torch.rand(B, T) * 0.2 + 0.4 result_impaired = detector(pupil_impaired, gaze_impaired, eyelid_impaired) print(f"损伤等级: {result_impaired['impairment_level']}") print(f"BAC估计: {result_impaired['bac_estimate'].squeeze() * 100:.2f}%") print(f"震颤幅度: {result_impaired['nystagmus_amplitude']:.4f}") bac_threshold = 0.08 for i in range(B): if result_impaired['bac_estimate'][i] > bac_threshold: print(f"样本{i}: ⚠️ 超过法定醉驾标准(>{bac_threshold*100:.0f}%)") else: print(f"样本{i}: ✅ 未超过法定标准")
|