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
| """ 视觉行为分析酒驾检测框架
基于DMS摄像头,无需额外硬件 """ import numpy as np import torch import torch.nn as nn
class VisualImpairmentDetector(nn.Module): """ 视觉酒驾损伤检测模型 多模态输入: 1. 面部关键点序列(48点 × T帧) 2. 头部姿态序列(pitch/yaw/roll × T帧) 3. 眼动特征序列(PERCLOS/gaze_x/gaze_y × T帧) 4. 面部外观特征(肤色/对称性) 输出:impairment概率 (0-1) """ def __init__(self, seq_len=150, n_face_pts=48): super().__init__() self.face_branch = nn.Sequential( nn.Linear(n_face_pts * 2, 128), nn.ReLU(), nn.LSTM(128, 64, batch_first=True), ) self.head_branch = nn.Sequential( nn.Linear(3, 32), nn.ReLU(), nn.LSTM(32, 32, batch_first=True), ) self.eye_branch = nn.Sequential( nn.Linear(3, 32), nn.ReLU(), nn.LSTM(32, 32, batch_first=True), ) self.appearance_branch = nn.Sequential( nn.Conv2d(3, 16, 3, stride=2), nn.ReLU(), nn.Conv2d(16, 32, 3, stride=2), nn.ReLU(), nn.AdaptiveAvgPool2d(1), nn.Flatten(), nn.Linear(32, 32), ) self.fusion = nn.Sequential( nn.Linear(64 + 32 + 32 + 32, 64), nn.ReLU(), nn.Dropout(0.2), nn.Linear(64, 1), nn.Sigmoid() ) def forward(self, face_pts, head_pose, eye_features, face_img): """前向推理""" face_feat, _ = self.face_branch(face_pts) face_feat = face_feat[:, -1, :] head_feat, _ = self.head_branch(head_pose) head_feat = head_feat[:, -1, :] eye_feat, _ = self.eye_branch(eye_features) eye_feat = eye_feat[:, -1, :] appearance_feat = self.appearance_branch(face_img) fused = torch.cat([face_feat, head_feat, eye_feat, appearance_feat], dim=1) impairment_prob = self.fusion(fused) return impairment_prob
if __name__ == "__main__": model = VisualImpairmentDetector() batch = 1 face_pts = torch.randn(batch, 150, 48, 2) head_pose = torch.randn(batch, 150, 3) eye_features = torch.randn(batch, 150, 3) face_img = torch.randn(batch, 3, 64, 64) prob = model(face_pts, head_pose, eye_features, face_img) print(f"损伤概率: {prob.item():.3f}") print(f"判断: {'疑似酒驾' if prob > 0.5 else '正常'}")
|