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
| """ In-vehicle 3D Driver Pose Estimation using ToF Depth Camera Based on: Scientific Reports (2026)
核心: 从深度图估计 16 个关键关节 3D 坐标 轻量化: 比基准模型小 70%, 精度 96.02% """
import torch import torch.nn as nn import torch.nn.functional as F import numpy as np from typing import Tuple, List
class DepthPoseEstimator(nn.Module): """ 轻量级 3D 姿态估计器 (深度图输入) 输入: 深度图 (1, H, W) + IR 图 (1, H, W) 输出: 16 个关节 3D 坐标 (16, 3) 架构: 轻量 Hourglass Network 参数量: ~0.8M (基准 2.7M, 减少 70%) """ JOINT_NAMES = [ '头部', '颈部', '左肩', '右肩', '左肘', '右肘', '左腕', '右腕', '胸腔', '骨盆', '左髋', '右髋', '左膝', '右膝', '脊柱上段', '脊柱下段' ] def __init__( self, num_joints: int = 16, depth_channels: int = 1, ir_channels: int = 1, hidden_dim: int = 64 ): super().__init__() self.num_joints = num_joints self.depth_encoder = self._make_encoder( depth_channels, hidden_dim ) self.ir_encoder = self._make_encoder( ir_channels, hidden_dim ) self.fusion = nn.Sequential( nn.Conv2d(hidden_dim * 2, hidden_dim, 1, bias=False), nn.BatchNorm2d(hidden_dim), nn.ReLU(inplace=True) ) self.hourglass = LightweightHourglass( hidden_dim, num_joints ) self.pose_head = nn.Sequential( nn.AdaptiveAvgPool2d(1), nn.Flatten(), nn.Linear(hidden_dim, 256), nn.ReLU(inplace=True), nn.Dropout(0.2), nn.Linear(256, num_joints * 3) ) def _make_encoder(self, in_ch, out_ch): """轻量编码器""" return nn.Sequential( nn.Conv2d(in_ch, out_ch // 2, 3, stride=2, padding=1), nn.BatchNorm2d(out_ch // 2), nn.ReLU(inplace=True), nn.Conv2d(out_ch // 2, out_ch, 3, stride=2, padding=1), nn.BatchNorm2d(out_ch), nn.ReLU(inplace=True), nn.MaxPool2d(2), ) def forward( self, depth: torch.Tensor, ir: torch.Tensor ) -> Tuple[torch.Tensor, torch.Tensor]: """ Returns: pose_3d: (B, 16, 3) 关节 3D 坐标 heatmaps: (B, 16, H//4, W//4) 关节热力图 """ depth_feat = self.depth_encoder(depth) ir_feat = self.ir_encoder(ir) fused = self.fusion( torch.cat([depth_feat, ir_feat], dim=1) ) heatmaps = self.hourglass(fused) pose_3d = self.pose_head(fused) pose_3d = pose_3d.view(-1, self.num_joints, 3) return pose_3d, heatmaps
class LightweightHourglass(nn.Module): """轻量 Hourglass 网络""" def __init__(self, dim, num_joints): super().__init__() self.down1 = self._make_res_block(dim, dim * 2, stride=2) self.down2 = self._make_res_block(dim * 2, dim * 4, stride=2) self.up1 = nn.ConvTranspose2d( dim * 4, dim * 2, 2, stride=2 ) self.up2 = nn.ConvTranspose2d( dim * 2, dim, 2, stride=2 ) self.head = nn.Conv2d(dim, num_joints, 1) def _make_res_block(self, in_ch, out_ch, stride=1): return nn.Sequential( nn.Conv2d(in_ch, out_ch, 3, stride=stride, padding=1, bias=False), nn.BatchNorm2d(out_ch), nn.ReLU(inplace=True), nn.Conv2d(out_ch, out_ch, 3, padding=1, bias=False), nn.BatchNorm2d(out_ch), nn.ReLU(inplace=True), ) def forward(self, x): skip1 = x x = self.down1(x) skip2 = x x = self.down2(x) x = self.up1(x) + skip2 x = self.up2(x) + skip1 return self.head(x)
class STGCNPlusPlus(nn.Module): """ ST-GCN++: 骨架序列行为识别 输入: 3D 关节序列 (B, T, V, C) T=帧数, V=关节数, C=坐标维度 输出: 行为类别 (B, num_classes) 基于: Spatial-Temporal Graph Convolutional Network 优化: ++ 版本增加多尺度时间卷积 """ def __init__( self, num_joints: int = 16, num_classes: int = 10, in_channels: int = 3, hidden_dim: int = 64, num_layers: int = 4 ): super().__init__() self.num_joints = num_joints self.input_proj = nn.Linear(in_channels, hidden_dim) self.adj = self._build_skeleton_adj(num_joints) self.gcn_layers = nn.ModuleList([ STGCNLayer(hidden_dim, self.adj) for _ in range(num_layers) ]) self.temporal_conv = MultiScaleTemporalConv( hidden_dim, kernel_sizes=[1, 3, 5] ) self.classifier = nn.Sequential( nn.AdaptiveAvgPool2d(1), nn.Flatten(), nn.Linear(hidden_dim, num_classes) ) def _build_skeleton_adj(self, n): """构建骨架邻接矩阵""" adj = torch.zeros(n, n) connections = [ (0, 1), (1, 2), (1, 3), (2, 4), (3, 5), (4, 6), (5, 7), (1, 8), (8, 9), (9, 10), (9, 11), (10, 12), (11, 13), (8, 14), (14, 15) ] for i, j in connections: if i < n and j < n: adj[i, j] = 1 adj[j, i] = 1 adj += torch.eye(n) D = adj.sum(dim=1, keepdim=True) adj = adj / D.clamp(min=1) return nn.Parameter(adj, requires_grad=False) def forward(self, x): """ Args: x: (B, T, V, C) 骨架序列 """ B, T, V, C = x.shape x = self.input_proj(x) for layer in self.gcn_layers: x = layer(x) x = x.permute(0, 2, 3, 1) x = self.temporal_conv(x) x = x.permute(0, 2, 1, 3) out = self.classifier(x) return out
class STGCNLayer(nn.Module): """ST-GCN 单层""" def __init__(self, dim, adj): super().__init__() self.adj = adj self.theta = nn.Parameter(torch.randn(dim, dim) * 0.01) self.gcn_bias = nn.Parameter(torch.zeros(1)) def forward(self, x): """x: (B, T, V, D)""" x = torch.einsum('btvd,de,vw->btwe', x, self.theta, self.adj) x = x + self.gcn_bias return F.relu(x)
class MultiScaleTemporalConv(nn.Module): """多尺度时间卷积""" def __init__(self, dim, kernel_sizes=[1, 3, 5]): super().__init__() self.convs = nn.ModuleList([ nn.Conv2d(dim, dim // len(kernel_sizes), kernel_size=(1, k), padding=(0, k//2)) for k in kernel_sizes ]) self.fuse = nn.Conv2d(dim, dim, 1) def forward(self, x): """x: (B, C, V, T)""" outs = [conv(x) for conv in self.convs] out = torch.cat(outs, dim=1) return F.relu(self.fuse(out))
class HierarchicalSafetyControl: """ 三级层次化安全控制系统 一级: 主动警告 (声光提醒) 二级: ADAS 干预 (减速/转向) 三级: 被动安全准备 (安全带预紧/气囊调整) """ DANGER_LEVELS = { 'normal_driving': 0, 'phone_use': 1, 'smoking': 1, 'eating': 1, 'turning_around': 2, 'leaning_forward': 2, 'leaning_side': 2, 'drowsy_posture': 2, 'reaching_back': 3, 'abnormal_recline': 3, } def __init__(self): self.current_level = 0 def assess(self, behavior: str, confidence: float, duration: float) -> dict: """评估危险等级并生成控制指令""" danger_level = self.DANGER_LEVELS.get(behavior, 0) if duration > 3.0 and danger_level > 0: danger_level = min(danger_level + 1, 3) actions = self._get_actions(danger_level) return { 'behavior': behavior, 'danger_level': danger_level, 'confidence': confidence, 'duration': duration, 'actions': actions } def _get_actions(self, level: int) -> List[str]: """根据危险等级获取控制动作""" actions = [] if level >= 1: actions.extend([ '声光警告', 'HUD 提示', '方向盘振动' ]) if level >= 2: actions.extend([ 'ADAS 减速准备', '车距自动增大', '警告升级' ]) if level >= 3: actions.extend([ '安全带预紧', '气囊部署角度调整', '紧急减速', 'eCall 预警' ]) return actions
if __name__ == "__main__": print("=" * 60) print("3D 姿态估计模型测试") print("=" * 60) model = DepthPoseEstimator( num_joints=16, hidden_dim=64 ) depth = torch.randn(2, 1, 240, 320) ir = torch.randn(2, 1, 240, 320) pose_3d, heatmaps = model(depth, ir) print(f"输入: depth={depth.shape}, ir={ir.shape}") print(f"3D姿态: {pose_3d.shape}") print(f"热力图: {heatmaps.shape}") print(f"关节名称: {DepthPoseEstimator.JOINT_NAMES}") total = sum(p.numel() for p in model.parameters()) print(f"\n参数量: {total:,} (基准: 2,700,000)") print(f"压缩率: {(1 - total/2700000)*100:.1f}%") print("\n" + "=" * 60) print("ST-GCN++ 行为识别测试") print("=" * 60) stgcn = STGCNPlusPlus( num_joints=16, num_classes=10, in_channels=3, hidden_dim=64 ) skeleton_seq = torch.randn(2, 30, 16, 3) behavior_logits = stgcn(skeleton_seq) print(f"输入: {skeleton_seq.shape}") print(f"输出: {behavior_logits.shape}") print(f"预测类别: {behavior_logits.argmax(dim=-1)}") print("\n" + "=" * 60) print("三级安全控制测试") print("=" * 60) control = HierarchicalSafetyControl() test_cases = [ ('phone_use', 0.92, 1.5), ('turning_around', 0.88, 2.0), ('reaching_back', 0.95, 4.0), ] for behavior, conf, dur in test_cases: result = control.assess(behavior, conf, dur) print(f"\n行为: {result['behavior']}") print(f"危险等级: {result['danger_level']}") print(f"控制动作: {', '.join(result['actions'])}")
|