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
| import torch import torch.nn as nn
class DualHeadYOLOv5(nn.Module): """ 双头YOLOv5手势识别框架 论文: Zhao et al., "A Multi-Scale Dual-Head YOLOv5 Framework for Hand Gesture Recognition via Spatial Relationship Modeling" Information, 2026 """ def __init__(self, n_gestures: int = 10, n_anchors: int = 3): super().__init__() self.n_gestures = n_gestures self.backbone = self._build_backbone() self.neck = self._build_neck() self.det_head = self._build_det_head(n_anchors) self.cls_head = self._build_cls_head(n_gestures) self.spatial_module = SpatialRelationModule() def _build_backbone(self) -> nn.Module: """CSPDarknet backbone""" return nn.Sequential( nn.Conv2d(3, 64, 6, 2, 2), self._csp_block(64, 128, 3), self._csp_block(128, 256, 9), self._csp_block(256, 512, 9), self._csp_block(512, 1024, 3), ) def _csp_block(self, in_c, out_c, n): """CSP (Cross Stage Partial) block""" layers = [] for _ in range(n): layers.append(nn.Sequential( nn.Conv2d(in_c, out_c, 1), nn.BatchNorm2d(out_c), nn.SiLU(), nn.Conv2d(out_c, out_c, 3, 1, 1), nn.BatchNorm2d(out_c), nn.SiLU(), )) in_c = out_c return nn.Sequential(*layers) def _build_neck(self) -> nn.Module: """PANet neck for multi-scale fusion""" return nn.Sequential( nn.Conv2d(1024, 512, 1), nn.Upsample(scale_factor=2), nn.Conv2d(512, 256, 1), nn.Upsample(scale_factor=2), ) def _build_det_head(self, n_anchors): """检测头: Bounding Box + Objectness""" return nn.Sequential( nn.Conv2d(256, 256, 3, 1, 1), nn.Conv2d(256, n_anchors * (4 + 1), 1), ) def _build_cls_head(self, n_gestures): """手势分类头""" return nn.Sequential( nn.AdaptiveAvgPool2d((1, 1)), nn.Flatten(), nn.Linear(256, 128), nn.SiLU(), nn.Linear(128, n_gestures), ) def forward(self, x): """前向传播""" backbone_feat = self.backbone(x) neck_feat = self.neck(backbone_feat) det_out = self.det_head(neck_feat) cls_out = self.cls_head(neck_feat) spatial_out = self.spatial_module(det_out, cls_out) return { 'detection': det_out, 'classification': cls_out, 'spatial': spatial_out }
class SpatialRelationModule(nn.Module): """ 空间关系建模模块 利用手部与方向盘/面部的空间关系增强手势识别 """ def __init__(self): super().__init__() self.steering_wheel_roi = [(0.3, 0.5), (0.7, 0.8)] self.face_roi = [(0.3, 0.1), (0.7, 0.4)] def forward(self, det_out, cls_out): """ 计算空间关系特征 Args: det_out: 检测输出 [B, A, 5, H, W] cls_out: 分类输出 [B, n_gestures] Returns: 增强后的分类结果 """ spatial_features = torch.ones_like(cls_out) * 0.1 enhanced = cls_out + spatial_features * 0.3 return torch.softmax(enhanced, dim=-1)
CABIN_GESTURES = { 0: 'fist', 1: 'palm', 2: 'point_up', 3: 'point_left', 4: 'point_right', 5: 'swipe_left', 6: 'swipe_right', 7: 'pinch', 8: 'rotate', 9: 'thumbs_up', }
if __name__ == "__main__": model = DualHeadYOLOv5(n_gestures=10) x = torch.randn(1, 3, 640, 640) out = model(x) print("=== 双头YOLOv5手势识别 ===") print(f"检测输出: {out['detection'].shape}") print(f"分类输出: {out['classification'].shape}") print(f"空间增强: {out['spatial'].shape}") print(f"\n座舱手势类别: {len(CABIN_GESTURES)}种") for idx, name in CABIN_GESTURES.items(): print(f" [{idx}] {name}")
|