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 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
| """ 车辆乘员3D姿态估计系统
方案: 1. RGB-D相机获取深度信息 2. 2D姿态估计 3. 深度引导的3D姿态重建 4. 异常姿态判断
"""
import torch import torch.nn as nn import torch.nn.functional as F from typing import Dict, List, Tuple, Optional import numpy as np from dataclasses import dataclass from enum import Enum
class OccupantPosture(Enum): """乘员姿态""" NORMAL = "normal" LEANING_FORWARD = "leaning_forward" LEANING_SIDEWAYS = "leaning_sideways" LEANING_BACK = "leaning_back" LEGS_UP = "legs_up" LYING_DOWN = "lying_down" CHILD_SEAT_MISUSE = "child_seat_misuse"
@dataclass class Joint3D: """3D关节点""" name: str x: float y: float z: float confidence: float
class DepthEstimator(nn.Module): """ 深度估计器 从RGB图像估计深度(无深度相机时使用) """ def __init__(self): super().__init__() from torchvision.models import resnet18 resnet = resnet18(pretrained=True) self.encoder = nn.Sequential(*list(resnet.children())[:-2]) self.decoder = nn.Sequential( nn.ConvTranspose2d(512, 256, 2, stride=2), nn.BatchNorm2d(256), nn.ReLU(), nn.ConvTranspose2d(256, 128, 2, stride=2), nn.BatchNorm2d(128), nn.ReLU(), nn.ConvTranspose2d(128, 64, 2, stride=2), nn.BatchNorm2d(64), nn.ReLU(), nn.ConvTranspose2d(64, 32, 2, stride=2), nn.BatchNorm2d(32), nn.ReLU(), nn.Conv2d(32, 1, 1) ) def forward(self, image: torch.Tensor) -> torch.Tensor: """ 估计深度 Args: image: [B, 3, H, W] Returns: depth: [B, 1, H, W] 深度图(米) """ features = self.encoder(image) depth = self.decoder(features) return depth
class Pose2DEstimator(nn.Module): """ 2D姿态估计器 使用轻量级网络估计关键点 """ def __init__(self, num_joints: int = 17): super().__init__() self.backbone = nn.Sequential( nn.Conv2d(3, 32, 3, stride=2, padding=1), nn.BatchNorm2d(32), nn.ReLU(), nn.Conv2d(32, 64, 3, stride=2, padding=1), nn.BatchNorm2d(64), nn.ReLU(), nn.Conv2d(64, 128, 3, stride=2, padding=1), nn.BatchNorm2d(128), nn.ReLU(), nn.Conv2d(128, 256, 3, stride=2, padding=1), nn.BatchNorm2d(256), nn.ReLU() ) self.heatmap_head = nn.Conv2d(256, num_joints, 1) self.joint_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' ] def forward(self, image: torch.Tensor) -> torch.Tensor: """ 估计2D关键点热图 Args: image: [B, 3, H, W] Returns: heatmaps: [B, num_joints, H/16, W/16] """ features = self.backbone(image) heatmaps = self.heatmap_head(features) return heatmaps
class Pose3DReconstructor(nn.Module): """ 3D姿态重建器 从2D关键点和深度重建3D姿态 """ def __init__( self, num_joints: int = 17, hidden_dim: int = 256 ): super().__init__() self.joint_encoder = nn.Sequential( nn.Linear(num_joints * 3, hidden_dim), nn.ReLU(), nn.Linear(hidden_dim, hidden_dim) ) self.depth_encoder = nn.Sequential( nn.AdaptiveAvgPool2d(1), nn.Flatten(), nn.Linear(1, 64), nn.ReLU() ) self.reconstructor = nn.Sequential( nn.Linear(hidden_dim + 64, hidden_dim), nn.ReLU(), nn.Dropout(0.3), nn.Linear(hidden_dim, num_joints * 3) ) self.register_buffer('mean', torch.zeros(num_joints, 3)) self.register_buffer('std', torch.ones(num_joints, 3)) def forward( self, joints_2d: torch.Tensor, depth_map: torch.Tensor ) -> torch.Tensor: """ 重建3D姿态 Args: joints_2d: [B, num_joints, 3] (x, y, confidence) depth_map: [B, 1, H, W] Returns: joints_3d: [B, num_joints, 3] (x, y, z) 米 """ joint_feat = self.joint_encoder(joints_2d.view(joints_2d.size(0), -1)) depth_feat = self.depth_encoder(depth_map) fused = torch.cat([joint_feat, depth_feat], dim=-1) joints_3d = self.reconstructor(fused) joints_3d = joints_3d.view(-1, 17, 3) joints_3d = joints_3d * self.std + self.mean return joints_3d
class PostureClassifier(nn.Module): """ 姿态分类器 从3D关节点判断姿态类型 """ def __init__( self, num_joints: int = 17, num_classes: int = 7 ): super().__init__() self.encoder = nn.Sequential( nn.Linear(num_joints * 3, 256), nn.ReLU(), nn.Dropout(0.3), nn.Linear(256, 128), nn.ReLU() ) self.classifier = nn.Linear(128, num_classes) self.posture_types = list(OccupantPosture) def forward(self, joints_3d: torch.Tensor) -> torch.Tensor: """ 分类姿态 Args: joints_3d: [B, num_joints, 3] Returns: logits: [B, num_classes] """ features = self.encoder(joints_3d.view(joints_3d.size(0), -1)) logits = self.classifier(features) return logits
class Occupant3DPoseSystem(nn.Module): """ 完整的乘员3D姿态估计系统 """ def __init__(self, use_depth_camera: bool = False): super().__init__() self.use_depth_camera = use_depth_camera if not use_depth_camera: self.depth_estimator = DepthEstimator() self.pose_2d = Pose2DEstimator() self.pose_3d = Pose3DReconstructor() self.posture_classifier = PostureClassifier() def forward( self, image: torch.Tensor, depth: Optional[torch.Tensor] = None ) -> Dict[str, torch.Tensor]: """ 前向传播 Args: image: [B, 3, H, W] RGB图像 depth: [B, 1, H, W] 深度图(可选) Returns: output: { 'heatmaps_2d': 2D热图, 'joints_3d': 3D关节点, 'posture_logits': 姿态分类 } """ if depth is None and not self.use_depth_camera: depth = self.depth_estimator(image) heatmaps_2d = self.pose_2d(image) joints_2d = self._decode_heatmaps(heatmaps_2d) joints_3d = self.pose_3d(joints_2d, depth) posture_logits = self.posture_classifier(joints_3d) return { 'heatmaps_2d': heatmaps_2d, 'joints_3d': joints_3d, 'posture_logits': posture_logits } def _decode_heatmaps( self, heatmaps: torch.Tensor ) -> torch.Tensor: """解码热图为关节点坐标""" batch_size = heatmaps.size(0) num_joints = heatmaps.size(1) joints = [] for b in range(batch_size): batch_joints = [] for j in range(num_joints): heatmap = heatmaps[b, j] max_val = heatmap.max() max_idx = heatmap.argmax() h, w = heatmap.shape y = (max_idx // w).float() / h x = (max_idx % w).float() / w batch_joints.append([x.item(), y.item(), max_val.item()]) joints.append(batch_joints) return torch.tensor(joints, device=heatmaps.device)
class AbnormalPostureDetector: """异常姿态检测器""" def __init__( self, forward_threshold: float = 0.3, sideways_threshold: float = 0.2, back_threshold: float = 0.25, leg_up_threshold: float = 0.4 ): self.forward_threshold = forward_threshold self.sideways_threshold = sideways_threshold self.back_threshold = back_threshold self.leg_up_threshold = leg_up_threshold self.NOSE = 0 self.LEFT_SHOULDER = 5 self.RIGHT_SHOULDER = 6 self.LEFT_HIP = 11 self.RIGHT_HIP = 12 self.LEFT_KNEE = 13 self.RIGHT_KNEE = 14 self.LEFT_ANKLE = 15 self.RIGHT_ANKLE = 16 def detect( self, joints_3d: np.ndarray ) -> Dict: """ 检测异常姿态 Args: joints_3d: 3D关节点 Returns: result: 检测结果 """ forward_lean = self._calculate_forward_lean(joints_3d) sideways_lean = self._calculate_sideways_lean(joints_3d) back_lean = self._calculate_back_lean(joints_3d) leg_height = self._calculate_leg_height(joints_3d) posture = OccupantPosture.NORMAL abnormalities = [] if forward_lean > self.forward_threshold: posture = OccupantPosture.LEANING_FORWARD abnormalities.append('forward_lean') if abs(sideways_lean) > self.sideways_threshold: posture = OccupantPosture.LEANING_SIDEWAYS abnormalities.append('sideways_lean') if back_lean > self.back_threshold: posture = OccupantPosture.LEANING_BACK abnormalities.append('back_lean') if leg_height > self.leg_up_threshold: posture = OccupantPosture.LEGS_UP abnormalities.append('legs_up') return { 'posture': posture, 'abnormalities': abnormalities, 'metrics': { 'forward_lean': forward_lean, 'sideways_lean': sideways_lean, 'back_lean': back_lean, 'leg_height': leg_height } } def _calculate_forward_lean(self, joints: np.ndarray) -> float: """计算前倾程度""" head_z = joints[self.NOSE, 2] hip_z = (joints[self.LEFT_HIP, 2] + joints[self.RIGHT_HIP, 2]) / 2 return max(0, hip_z - head_z) def _calculate_sideways_lean(self, joints: np.ndarray) -> float: """计算侧倾程度""" shoulder_center_x = ( joints[self.LEFT_SHOULDER, 0] + joints[self.RIGHT_SHOULDER, 0] ) / 2 hip_center_x = ( joints[self.LEFT_HIP, 0] + joints[self.RIGHT_HIP, 0] ) / 2 return shoulder_center_x - hip_center_x def _calculate_back_lean(self, joints: np.ndarray) -> float: """计算后仰程度""" head_z = joints[self.NOSE, 2] hip_z = (joints[self.LEFT_HIP, 2] + joints[self.RIGHT_HIP, 2]) / 2 return max(0, head_z - hip_z) def _calculate_leg_height(self, joints: np.ndarray) -> float: """计算腿抬高程度""" hip_y = (joints[self.LEFT_HIP, 1] + joints[self.RIGHT_HIP, 1]) / 2 knee_y = (joints[self.LEFT_KNEE, 1] + joints[self.RIGHT_KNEE, 1]) / 2 return max(0, hip_y - knee_y)
if __name__ == "__main__": system = Occupant3DPoseSystem(use_depth_camera=False) print("乘员3D姿态估计系统架构:") print("- 深度估计: ResNet18 Encoder + Decoder") print("- 2D姿态: Lightweight CNN + Heatmap Head") print("- 3D重建: 2D关节 + 深度 → 3D关节") print("- 姿态分类: MLP分类器") dummy_image = torch.randn(1, 3, 224, 224) with torch.no_grad(): output = system(dummy_image) print(f"\n输出:") print(f" 2D热图: {output['heatmaps_2d'].shape}") print(f" 3D关节: {output['joints_3d'].shape}") print(f" 姿态分类: {output['posture_logits'].shape}") detector = AbnormalPostureDetector() joints_3d = output['joints_3d'][0].numpy() result = detector.detect(joints_3d) print(f"\n姿态检测结果:") print(f" 姿态类型: {result['posture'].value}") print(f" 异常指标: {result['abnormalities']}") print(f" 详细指标: {result['metrics']}")
|