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
| """ 智能座舱座椅人体姿态识别 基于 Wiley 2026 论文
核心: 2D姿态→3D重建→姿态分类→座椅参数优化 """
import torch import torch.nn as nn import torch.nn.functional as F import numpy as np from typing import Dict, List, Tuple, Optional
class PoseEstimator2D(nn.Module): """ 2D人体姿态估计器 从单张图像估计17个关节点的2D坐标 基于HRNet架构(高分辨率保持) """ KEYPOINT_NAMES = [ 'nose', 'left_eye', 'right_eye', 'left_ear', 'right_ear', 'left_shoulder', 'right_shoulder', 'left_elbow', 'right_elbow', 'left_wrist', 'right_wrist', 'left_hip', 'right_hip', 'left_knee', 'right_knee', 'left_ankle', 'right_ankle' ] SKELETON = [ (5, 7), (7, 9), (6, 8), (8, 10), (5, 6), (5, 11), (6, 12), (11, 12), (11, 13), (13, 15), (12, 14), (14, 16), (0, 5), (0, 6), ] def __init__(self, num_keypoints: int = 17): super().__init__() self.num_kp = num_keypoints self.stem = nn.Sequential( nn.Conv2d(3, 32, 3, 2, 1), nn.BatchNorm2d(32), nn.ReLU(), nn.Conv2d(32, 64, 3, 2, 1), nn.BatchNorm2d(64), nn.ReLU(), ) self.branch1 = self._make_branch(64, 32) self.branch2 = self._make_branch(64, 32, stride=2) self.branch3 = self._make_branch(64, 32, stride=4) self.fusion = nn.Conv2d(96, 64, 1) self.heatmap_head = nn.Sequential( nn.Conv2d(64, 64, 3, 1, 1), nn.ReLU(), nn.Conv2d(64, num_keypoints, 1), ) def _make_branch(self, in_ch, out_ch, stride=1): layers = [] s = stride for _ in range(4): layers.extend([ nn.Conv2d(in_ch, out_ch, 3, s, 1), nn.BatchNorm2d(out_ch), nn.ReLU(), ]) in_ch = out_ch s = 1 return nn.Sequential(*layers) def forward(self, image: torch.Tensor) -> Dict: """ Args: image: (B, 3, H, W) Returns: heatmaps: (B, 17, H/4, W/4) keypoints: (B, 17, 2) 坐标 confidence: (B, 17) """ x = self.stem(image) b1 = self.branch1(x) b2 = self.branch2(x) b3 = self.branch3(x) b2_up = F.interpolate(b2, size=b1.shape[2:], mode='bilinear') b3_up = F.interpolate(b3, size=b1.shape[2:], mode='bilinear') fused = self.fusion(torch.cat([b1, b2_up, b3_up], dim=1)) heatmaps = self.heatmap_head(fused) B, K, H, W = heatmaps.shape heatmaps_flat = heatmaps.view(B, K, -1) max_idx = heatmaps_flat.argmax(dim=-1) x_coord = (max_idx % W).float() / W y_coord = (max_idx // W).float() / H keypoints = torch.stack([x_coord, y_coord], dim=-1) confidence = heatmaps_flat.max(dim=-1).values return { 'heatmaps': heatmaps, 'keypoints': keypoints, 'confidence': confidence, }
class Pose3DReconstructor(nn.Module): """ 3D姿态重建器 从2D关节推断3D位置 用于OOP检测和座椅参数优化 """ def __init__(self, num_joints: int = 17, hidden_dim: int = 256): super().__init__() self.lift = nn.Sequential( nn.Linear(num_joints * 2, hidden_dim), nn.ReLU(), nn.Dropout(0.2), nn.Linear(hidden_dim, hidden_dim), nn.ReLU(), nn.Dropout(0.2), nn.Linear(hidden_dim, num_joints * 3), ) def forward(self, keypoints_2d: torch.Tensor) -> torch.Tensor: """ Args: keypoints_2d: (B, 17, 2) Returns: keypoints_3d: (B, 17, 3) 相对骨盆的3D坐标 """ B, K, _ = keypoints_2d.shape x = keypoints_2d.reshape(B, -1) pred_3d = self.lift(x).reshape(B, K, 3) pelvis = (pred_3d[:, 11] + pred_3d[:, 12]) / 2 keypoints_3d = pred_3d - pelvis.unsqueeze(1) return keypoints_3d
class PostureClassifier(nn.Module): """ 姿态分类器 将3D姿态分类为标准/异常姿态 用于OOP检测 """ POSTURE_CLASSES = [ 'normal_driving', 'leaning_left', 'leaning_right', 'leaning_forward', 'leaning_back', 'reclined', 'reaching_dashboard', 'picking_floor', 'sleeping', 'on_phone', 'twisting', ] def __init__(self, num_joints: int = 17, num_classes: int = 11): super().__init__() self.classifier = nn.Sequential( nn.Linear(num_joints * 3, 512), nn.ReLU(), nn.Dropout(0.3), nn.Linear(512, 256), nn.ReLU(), nn.Dropout(0.3), nn.Linear(256, num_classes), ) def forward(self, keypoints_3d: torch.Tensor) -> Dict: """ Args: keypoints_3d: (B, 17, 3) Returns: class_logits: (B, num_classes) posture: str confidence: float """ B = keypoints_3d.shape[0] x = keypoints_3d.reshape(B, -1) logits = self.classifier(x) probs = F.softmax(logits, dim=-1) idx = probs.argmax(dim=-1) results = [] for i in range(B): results.append({ 'posture': self.POSTURE_CLASSES[idx[i].item()], 'confidence': probs[i, idx[i]].item(), }) return {'results': results, 'logits': logits}
class SeatAdjustmentOptimizer(nn.Module): """ 座椅参数优化器 根据姿态自动调节座椅参数 控制参数: - seat_position: 前后(x)、高低(z) 0-1 - backrest_angle: 靠背角度 0-1 (0=垂直, 1=平躺) - lumbar_support: 腰托高度/凸度 0-1 - headrest_height: 头枕高度 0-1 - side_bolster: 侧支撑 0-1 """ SEAT_PARAMS = { 'seat_x': {'range': (0.0, 1.0), 'default': 0.5}, 'seat_z': {'range': (0.0, 1.0), 'default': 0.5}, 'backrest_angle': {'range': (0.0, 0.7), 'default': 0.25}, 'lumbar_height': {'range': (0.0, 1.0), 'default': 0.5}, 'lumbar_convexity': {'range': (0.0, 1.0), 'default': 0.4}, 'headrest_height': {'range': (0.0, 1.0), 'default': 0.5}, 'side_bolster': {'range': (0.0, 1.0), 'default': 0.3}, } def __init__(self, num_joints: int = 17): super().__init__() num_params = len(self.SEAT_PARAMS) self.optimizer = nn.Sequential( nn.Linear(num_joints * 3, 512), nn.ReLU(), nn.Linear(512, 256), nn.ReLU(), nn.Linear(256, num_params), nn.Sigmoid(), ) self.body_scale = nn.Sequential( nn.Linear(num_joints * 3, 64), nn.ReLU(), nn.Linear(64, 3), ) def forward(self, keypoints_3d: torch.Tensor) -> Dict: """ 根据姿态生成座椅参数 Args: keypoints_3d: (B, 17, 3) Returns: params: 座椅参数字典 body_estimate: 身体尺寸估计 """ B = keypoints_3d.shape[0] x = keypoints_3d.reshape(B, -1) raw_params = self.optimizer(x) body = self.body_scale(x) results = [] param_names = list(self.SEAT_PARAMS.keys()) for i in range(B): params = {} for j, name in enumerate(param_names): min_val = self.SEAT_PARAMS[name]['range'][0] max_val = self.SEAT_PARAMS[name]['range'][1] value = raw_params[i, j].item() * (max_val - min_val) + min_val params[name] = round(value, 3) results.append(params) body_estimates = [] for i in range(B): body_estimates.append({ 'estimated_height_cm': 150 + body[i, 0].item() * 50, 'sitting_ratio': body[i, 1].item(), 'shoulder_width_cm': 35 + body[i, 2].item() * 15, }) return { 'seat_params': results, 'body_estimate': body_estimates, }
class IntelligentSeatSystem(nn.Module): """ 智能座椅系统 集成: 2D姿态估计 → 3D重建 → 姿态分类 → 座椅优化 同时: OOP异常检测 """ def __init__(self): super().__init__() self.pose2d = PoseEstimator2D() self.pose3d = Pose3DReconstructor() self.posture_cls = PostureClassifier() self.seat_opt = SeatAdjustmentOptimizer() def forward(self, image: torch.Tensor) -> Dict: """ 完整推理流程 Args: image: (B, 3, H, W) 座舱侧摄像头图像 Returns: result: 姿态、姿态分类、座椅参数、OOP状态 """ pose2d_out = self.pose2d(image) kpts_3d = self.pose3d(pose2d_out['keypoints']) posture_out = self.posture_cls(kpts_3d) seat_out = self.seat_opt(kpts_3d) oop_results = [] for r in posture_out['results']: is_oop = r['posture'] not in [ 'normal_driving', 'leaning_left', 'leaning_right' ] oop_results.append({ 'is_oop': is_oop, 'posture': r['posture'], 'confidence': r['confidence'], }) return { 'keypoints_2d': pose2d_out['keypoints'], 'keypoints_3d': kpts_3d, 'confidence': pose2d_out['confidence'], 'posture': posture_out['results'], 'seat_params': seat_out['seat_params'], 'body_estimate': seat_out['body_estimate'], 'oop_status': oop_results, }
if __name__ == "__main__": torch.manual_seed(42) system = IntelligentSeatSystem() image = torch.randn(2, 3, 256, 192) result = system(image) for i in range(2): print(f"\n{'='*60}") print(f"乘员 {i+1}:") print(f" 2D关键点: {result['keypoints_2d'][i].shape}") print(f" 3D关键点: {result['keypoints_3d'][i].shape}") print(f" 平均置信度: {result['confidence'][i].mean():.3f}") print(f" 姿态: {result['posture'][i]['posture']} " f"({result['posture'][i]['confidence']:.3f})") print(f" OOP异常: {result['oop_status'][i]['is_oop']}") params = result['seat_params'][i] print(f" 座椅参数:") for k, v in params.items(): print(f" {k}: {v}") body = result['body_estimate'][i] print(f" 身体估计: 身高{body['estimated_height_cm']:.0f}cm, " f"肩宽{body['shoulder_width_cm']:.0f}cm") print(f"\n✅ 测试通过")
|