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
| import torch import torch.nn as nn import numpy as np
class OOPDetectionFromRadar(nn.Module): """ 基于 mmWave 雷达 HMR 的 OOP 异常姿态检测器 设计思路: 1. 使用 M4Human 预训练 HMR 模型提取 3D 人体姿态 2. 结合座椅参考几何,判断姿态是否异常 3. 输出 OOP 类别和置信度 硬件配置: | 组件 | 型号 | 参数 | |------|------|------| | mmWave 雷达 | Vayyar VSWR-10 或 TI IWR6843AOP | 60-64 GHz, 4Tx/4Rx | | 处理器 | Qualcomm QCS8255 | Hexagon NPU, 26 TOPS | | 更新频率 | 30 fps | 端到端推理 | """ OOP_CLASSES = { 0: "normal_posture", 1: "forward_lean", 2: "sideways_lean", 3: "fetal_position", 4: "feet_on_dashboard", 5: "kneeling", 6: "lying_down", 7: "backward_facing", } def __init__(self, hmr_checkpoint: str = None, num_classes: int = 8): super().__init__() self.hmr = RadarHMRBaseline(modality="rt") if hmr_checkpoint: self.hmr.load_state_dict(torch.load(hmr_checkpoint)) for param in self.hmr.parameters(): param.requires_grad = False self.classifier = nn.Sequential( nn.Linear(156 + 10 + 3, 256), nn.ReLU(), nn.Dropout(0.3), nn.Linear(256, 128), nn.ReLU(), nn.Dropout(0.2), nn.Linear(128, num_classes) ) self.register_buffer( 'seat_reference', torch.tensor([ [0.0, 0.0, 0.5], [0.0, 0.0, 0.2], [0.0, -0.3, 0.1], [0.0, -0.5, 0.0], [0.0, -0.7, -0.1], [0.0, -0.9, -0.2], ]) ) def forward(self, radar_input: torch.Tensor) -> dict: """ Args: radar_input: (B, 1, A, D, R) Raw Tensor Returns: output: { pose: (B, 156) 3D 关节位置, shape: (B, 10) 体型参数, trans: (B, 3) 全局平移, oop_logits: (B, 8) OOP 分类, oop_class: (B,) 预测类别 } """ with torch.no_grad(): hmr_out = self.hmr(radar_input) feat = torch.cat([ hmr_out["pose"], hmr_out["shape"], hmr_out["trans"] ], dim=-1) oop_logits = self.classifier(feat) oop_class = oop_logits.argmax(dim=-1) return { **hmr_out, "oop_logits": oop_logits, "oop_class": oop_class } def post_to_oop_score(self, pose_3d: torch.Tensor) -> dict: """ 基于关节 3D 位置直接计算 OOP 风险分数 Args: pose_3d: (B, 52, 3) 52 个关节的 3D 坐标 Returns: scores: 各 OOP 风险分数 """ HEAD = 12 SHOULDER_L = 2 SHOULDER_R = 5 HIP_L = 12 HIP_R = 15 KNEE_L = 18 KNEE_R = 21 ANKLE_L = 19 ANKLE_R = 22 torso_vec = pose_3d[:, HIP_L] - pose_3d[:, SHOULDER_L] vertical = torch.tensor([0, 0, 1], device=pose_3d.device) forward_lean_angle = torch.acos( torch.clamp( torch.sum(torso_vec * vertical, dim=-1) / (torch.norm(torso_vec, dim=-1) + 1e-8), -1, 1 ) ) shoulder_vec = pose_3d[:, SHOULDER_R] - pose_3d[:, SHOULDER_L] sideways_angle = torch.atan2( shoulder_vec[..., 2], torch.norm(shoulder_vec[..., :2], dim=-1) ) knee_hip_dist = torch.norm( pose_3d[:, KNEE_L] - pose_3d[:, HIP_L], dim=-1 ) fetal_score = torch.exp(-knee_hip_dist / 0.2) feet_height = (pose_3d[:, ANKLE_L, 2] + pose_3d[:, ANKLE_R, 2]) / 2 return { "forward_lean_deg": torch.degrees(forward_lean_angle), "sideways_lean_deg": torch.degrees(sideways_angle), "fetal_score": fetal_score, "feet_height_m": feet_height, }
if __name__ == "__main__": model = OOPDetectionFromRadar() radar_input = torch.randn(2, 1, 32, 64, 128) with torch.no_grad(): output = model(radar_input) print("=== OOP 检测结果 ===") print(f"OOP 类别: {output['oop_class']}") for cls_idx, cls_name in model.OOP_CLASSES.items(): prob = torch.softmax(output['oop_logits'], dim=-1)[0, cls_idx] print(f" {cls_name}: {prob:.4f}")
|