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
| import torch import torch.nn as nn
class AlcoholImpairmentDetector(nn.Module): """ 酒精损伤检测器 """ def __init__(self): super().__init__() self.eye_encoder = nn.Sequential( nn.Linear(10, 32), nn.ReLU(), nn.Dropout(0.3) ) self.head_encoder = nn.Sequential( nn.Linear(6, 32), nn.ReLU(), nn.Dropout(0.3) ) self.behavior_encoder = nn.Sequential( nn.Linear(8, 32), nn.ReLU(), nn.Dropout(0.3) ) self.fusion = nn.Sequential( nn.Linear(96, 64), nn.ReLU(), nn.Dropout(0.3) ) self.lstm = nn.LSTM( input_size=64, hidden_size=32, num_layers=2, batch_first=True ) self.classifier = nn.Sequential( nn.Linear(32, 16), nn.ReLU(), nn.Linear(16, 2) ) def forward(self, eye_features, head_features, behavior_features): """ eye_features: [B, T, 10] head_features: [B, T, 6] behavior_features: [B, T, 8] """ eye_h = self.eye_encoder(eye_features) head_h = self.head_encoder(head_features) behavior_h = self.behavior_encoder(behavior_features) fused = torch.cat([eye_h, head_h, behavior_h], dim=-1) fused = self.fusion(fused) lstm_out, _ = self.lstm(fused) last_hidden = lstm_out[:, -1, :] logits = self.classifier(last_hidden) return logits
detector = AlcoholImpairmentDetector()
eye_features = extract_eye_features(video_stream) head_features = extract_head_features(video_stream) behavior_features = extract_behavior_features(vehicle_data)
logits = detector(eye_features, head_features, behavior_features) prob = torch.softmax(logits, dim=-1)
if prob[0, 1] > 0.7: print("⚠️ 酒驾风险高!")
|