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 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
| """ 多模态融合疲劳检测系统
模态: 1. 视觉:眼动、面部表情、头部姿态 2. 生理:心率、皮肤电导(可选) 3. 行为:方向盘、踏板
融合策略: - 早期融合:特征级融合 - 晚期融合:决策级融合 - 混合融合:特征+决策
"""
import torch import torch.nn as nn import torch.nn.functional as F from typing import Dict, List, Tuple, Optional import numpy as np from dataclasses import dataclass from enum import Enum
class FatigueLevel(Enum): """疲劳等级""" ALERT = 0 SLIGHT = 1 MODERATE = 2 SEVERE = 3
@dataclass class MultimodalFeatures: """多模态特征""" perclos: float blink_rate: float avg_eye_closure: float yawn_frequency: float mouth_openness: float head_nod_count: int head_pose_std: float heart_rate: Optional[float] hrv: Optional[float] steering_entropy: float lane_deviation: float
class EyeMovementEncoder(nn.Module): """眼动特征编码器""" def __init__(self, input_dim: int = 3, hidden_dim: int = 64): super().__init__() self.lstm = nn.LSTM( input_size=input_dim, hidden_size=hidden_dim, num_layers=2, batch_first=True, bidirectional=True ) self.feature_proj = nn.Linear(hidden_dim * 2, hidden_dim) def forward( self, eye_sequence: torch.Tensor ) -> torch.Tensor: """编码眼动序列""" lstm_out, _ = self.lstm(eye_sequence) last_hidden = lstm_out[:, -1, :] return self.feature_proj(last_hidden)
class FacialExpressionEncoder(nn.Module): """面部表情编码器""" def __init__(self, hidden_dim: int = 64): super().__init__() self.backbone = nn.Sequential( nn.Conv2d(3, 32, 3, padding=1), nn.BatchNorm2d(32), nn.ReLU(), nn.MaxPool2d(2), nn.Conv2d(32, 64, 3, padding=1), nn.BatchNorm2d(64), nn.ReLU(), nn.MaxPool2d(2), nn.Conv2d(64, 128, 3, padding=1), nn.BatchNorm2d(128), nn.ReLU(), nn.AdaptiveAvgPool2d(1) ) self.proj = nn.Linear(128, hidden_dim) def forward(self, face_image: torch.Tensor) -> torch.Tensor: """编码面部图像""" features = self.backbone(face_image) features = features.view(features.size(0), -1) return self.proj(features)
class HeadPoseEncoder(nn.Module): """头部姿态编码器""" def __init__(self, input_dim: int = 6, hidden_dim: int = 32): super().__init__() self.conv = nn.Sequential( nn.Conv1d(input_dim, 32, kernel_size=5, padding=2), nn.BatchNorm1d(32), nn.ReLU(), nn.Conv1d(32, 64, kernel_size=5, padding=2), nn.BatchNorm1d(64), nn.ReLU(), nn.AdaptiveAvgPool1d(1) ) self.proj = nn.Linear(64, hidden_dim) def forward( self, head_pose_sequence: torch.Tensor ) -> torch.Tensor: """编码头部姿态序列""" x = head_pose_sequence.transpose(1, 2) features = self.conv(x) features = features.view(features.size(0), -1) return self.proj(features)
class DrivingBehaviorEncoder(nn.Module): """驾驶行为编码器""" def __init__(self, input_dim: int = 4, hidden_dim: int = 32): super().__init__() self.encoder = nn.Sequential( nn.Linear(input_dim, 32), nn.ReLU(), nn.Linear(32, hidden_dim) ) def forward( self, behavior_features: torch.Tensor ) -> torch.Tensor: """编码驾驶行为""" return self.encoder(behavior_features)
class MultimodalFusionNetwork(nn.Module): """ 多模态融合网络 融合策略: 1. 早期融合:特征拼接 2. 注意力融合:自适应权重 3. 晚期融合:决策集成 """ def __init__( self, eye_dim: int = 64, face_dim: int = 64, head_dim: int = 32, behavior_dim: int = 32, fusion_dim: int = 128, num_classes: int = 4 ): super().__init__() self.eye_encoder = EyeMovementEncoder(input_dim=3, hidden_dim=eye_dim) self.face_encoder = FacialExpressionEncoder(hidden_dim=face_dim) self.head_encoder = HeadPoseEncoder(input_dim=6, hidden_dim=head_dim) self.behavior_encoder = DrivingBehaviorEncoder(input_dim=4, hidden_dim=behavior_dim) self.attention = nn.Sequential( nn.Linear(eye_dim + face_dim + head_dim + behavior_dim, 64), nn.Tanh(), nn.Linear(64, 4), nn.Softmax(dim=-1) ) self.fusion = nn.Sequential( nn.Linear(eye_dim + face_dim + head_dim + behavior_dim, fusion_dim), nn.ReLU(), nn.Dropout(0.3), nn.Linear(fusion_dim, fusion_dim), nn.ReLU() ) self.classifier = nn.Linear(fusion_dim, num_classes) def forward( self, eye_sequence: torch.Tensor, face_image: torch.Tensor, head_pose_sequence: torch.Tensor, behavior_features: torch.Tensor ) -> Dict[str, torch.Tensor]: """ 多模态融合 Args: eye_sequence: 眼动序列 [B, T, 3] face_image: 面部图像 [B, 3, H, W] head_pose_sequence: 头部姿态 [B, T, 6] behavior_features: 驾驶行为 [B, 4] Returns: output: { 'logits': 分类logits, 'attention_weights': 注意力权重, 'modality_features': 各模态特征 } """ eye_feat = self.eye_encoder(eye_sequence) face_feat = self.face_encoder(face_image) head_feat = self.head_encoder(head_pose_sequence) behavior_feat = self.behavior_encoder(behavior_features) concat = torch.cat([eye_feat, face_feat, head_feat, behavior_feat], dim=-1) attn_weights = self.attention(concat) weighted_eye = eye_feat * attn_weights[:, 0:1] weighted_face = face_feat * attn_weights[:, 1:2] weighted_head = head_feat * attn_weights[:, 2:3] weighted_behavior = behavior_feat * attn_weights[:, 3:4] fused = torch.cat([weighted_eye, weighted_face, weighted_head, weighted_behavior], dim=-1) fused_feat = self.fusion(fused) logits = self.classifier(fused_feat) return { 'logits': logits, 'attention_weights': attn_weights, 'modality_features': { 'eye': eye_feat, 'face': face_feat, 'head': head_feat, 'behavior': behavior_feat } }
class FatigueDetectionSystem: """完整的疲劳检测系统""" def __init__(self, model_path: str = None): self.model = MultimodalFusionNetwork() if model_path: self.model.load_state_dict(torch.load(model_path)) self.model.eval() self.perclos_calculator = PERCLOSCalculator() self.stats = { 'total_frames': 0, 'fatigue_detected': 0, 'false_alarms': 0 } def detect( self, eye_data: Dict, face_image: np.ndarray, head_pose: Dict, vehicle_data: Dict ) -> Dict: """ 检测疲劳 Args: eye_data: 眼动数据 face_image: 面部图像 head_pose: 头部姿态 vehicle_data: 车辆数据 Returns: result: 检测结果 """ self.stats['total_frames'] += 1 eye_sequence = self._extract_eye_features(eye_data) face_tensor = self._preprocess_face(face_image) head_sequence = self._extract_head_features(head_pose) behavior = self._extract_behavior_features(vehicle_data) with torch.no_grad(): output = self.model(eye_sequence, face_tensor, head_sequence, behavior) probs = F.softmax(output['logits'], dim=-1) level = FatigueLevel(probs.argmax().item()) confidence = probs.max().item() return { 'fatigue_level': level, 'confidence': confidence, 'attention_weights': output['attention_weights'][0].tolist(), 'should_alert': level.value >= FatigueLevel.MODERATE.value } def _extract_eye_features(self, eye_data: Dict) -> torch.Tensor: """提取眼动特征""" return torch.randn(1, 30, 3) def _preprocess_face(self, image: np.ndarray) -> torch.Tensor: """预处理面部图像""" import cv2 img = cv2.resize(image, (64, 64)) img = img.astype(np.float32) / 255.0 img = (img - [0.485, 0.456, 0.406]) / [0.229, 0.224, 0.225] img = img.transpose(2, 0, 1) return torch.from_numpy(img).unsqueeze(0) def _extract_head_features(self, head_pose: Dict) -> torch.Tensor: """提取头部姿态特征""" return torch.randn(1, 30, 6) def _extract_behavior_features(self, vehicle_data: Dict) -> torch.Tensor: """提取驾驶行为特征""" features = [ vehicle_data.get('steering_entropy', 0.5), vehicle_data.get('lane_deviation', 0.0), vehicle_data.get('speed_variation', 0.0), vehicle_data.get('reaction_time', 0.3) ] return torch.tensor([features], dtype=torch.float32)
class PERCLOSCalculator: """PERCLOS计算器""" def __init__( self, threshold: float = 0.2, window_sec: int = 60 ): self.threshold = threshold self.window_sec = window_sec self.eye_openness_history = [] def update(self, eye_openness: float, fps: int = 30) -> float: """ 更新并计算PERCLOS Args: eye_openness: 眼睑开度 [0, 1] fps: 帧率 Returns: perclos: PERCLOS值 [%] """ self.eye_openness_history.append(eye_openness) window_frames = self.window_sec * fps if len(self.eye_openness_history) > window_frames: self.eye_openness_history.pop(0) if len(self.eye_openness_history) < fps * 10: return 0.0 closed_frames = sum(1 for e in self.eye_openness_history if e < self.threshold) perclos = closed_frames / len(self.eye_openness_history) * 100 return perclos
class MetaGatedFusion(nn.Module): """ Meta-Gated融合 自适应选择最可靠的模态 """ def __init__( self, modal_dims: List[int], hidden_dim: int = 64 ): super().__init__() self.num_modalities = len(modal_dims) self.gate_networks = nn.ModuleList([ nn.Sequential( nn.Linear(dim, hidden_dim), nn.ReLU(), nn.Linear(hidden_dim, 1), nn.Sigmoid() ) for dim in modal_dims ]) self.meta_network = nn.Sequential( nn.Linear(self.num_modalities, hidden_dim), nn.ReLU(), nn.Linear(hidden_dim, self.num_modalities), nn.Softmax(dim=-1) ) def forward( self, modality_features: List[torch.Tensor], modality_reliability: Optional[torch.Tensor] = None ) -> torch.Tensor: """ Meta-Gated融合 Args: modality_features: 各模态特征列表 modality_reliability: 各模态可靠性 [B, num_modalities] Returns: fused: 融合特征 """ batch_size = modality_features[0].size(0) gate_values = [] for i, feat in enumerate(modality_features): gate = self.gate_networks[i](feat) gate_values.append(gate) gate_values = torch.cat(gate_values, dim=-1) if modality_reliability is not None: meta_weights = self.meta_network(modality_reliability) gate_values = gate_values * meta_weights gate_values = gate_values / (gate_values.sum(dim=-1, keepdim=True) + 1e-6) fused = torch.zeros_like(modality_features[0]) for i, feat in enumerate(modality_features): fused = fused + feat * gate_values[:, i:i+1] return fused
if __name__ == "__main__": model = MultimodalFusionNetwork() print("多模态融合疲劳检测架构:") print("- 眼动编码: LSTM") print("- 面部编码: CNN") print("- 头部姿态编码: 1D-CNN") print("- 驾驶行为编码: MLP") print("- 融合策略: 注意力融合") dummy_eye = torch.randn(1, 30, 3) dummy_face = torch.randn(1, 3, 64, 64) dummy_head = torch.randn(1, 30, 6) dummy_behavior = torch.randn(1, 4) with torch.no_grad(): output = model(dummy_eye, dummy_face, dummy_head, dummy_behavior) print(f"\n输出:") print(f" 分类logits: {output['logits'].shape}") print(f" 注意力权重: {output['attention_weights']}")
|