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
| import numpy as np import torch import torch.nn as nn from typing import List, Dict, Tuple
class Occupant3DPostureEstimator: """ 乘员3D姿态估计器 基于深度图像估计乘员骨骼关键点 """ def __init__(self, config: dict): super().__init__() self.depth_encoder = DepthEncoder( input_channels=1, feature_dim=config.get('feature_dim', 256) ) self.rgb_encoder = RGBEncoder( input_channels=3, feature_dim=config.get('feature_dim', 256) ) self.fusion = FeatureFusion( modalities=['depth', 'rgb'], fusion_dim=config.get('fusion_dim', 512) ) self.keypoint_head = nn.Sequential( nn.Linear(512, 256), nn.ReLU(), nn.Linear(256, 17 * 3) ) self.posture_head = nn.Sequential( nn.Linear(512, 128), nn.ReLU(), nn.Linear(128, 5) ) self.oop_detector = OOPDetector( keypoint_threshold=config.get('keypoint_threshold', 50) ) self.camera_intrinsics = config.get('camera_intrinsics') def estimate(self, depth_image: np.ndarray, rgb_image: np.ndarray = None) -> Dict: """ 估计乘员3D姿态 Args: depth_image: 深度图像 (H, W), 单位mm rgb_image: RGB图像 (H, W, 3), 可选 Returns: result: { 'keypoints_3d': np.ndarray, # (17, 3) 'keypoints_2d': np.ndarray, # (17, 2) 'posture_class': str, 'is_oop': bool, 'oop_type': str, 'confidence': float } """ depth_feat = self.depth_encoder(depth_image) if rgb_image is not None: rgb_feat = self.rgb_encoder(rgb_image) fused_feat = self.fusion([depth_feat, rgb_feat]) else: fused_feat = depth_feat keypoints_flat = self.keypoint_head(fused_feat) keypoints_3d = keypoints_flat.view(-1, 17, 3) keypoints_2d = self._project_3d_to_2d(keypoints_3d) posture_logits = self.posture_head(fused_feat) posture_class = self._classify_posture(posture_logits) oop_result = self.oop_detector.detect(keypoints_3d, posture_class) return { 'keypoints_3d': keypoints_3d[0].cpu().numpy(), 'keypoints_2d': keypoints_2d[0].cpu().numpy(), 'posture_class': posture_class, 'is_oop': oop_result['is_oop'], 'oop_type': oop_result['oop_type'], 'confidence': oop_result['confidence'] } def _project_3d_to_2d(self, keypoints_3d: torch.Tensor) -> torch.Tensor: """ 将3D关键点投影到2D图像平面 Args: keypoints_3d: (B, 17, 3) Returns: keypoints_2d: (B, 17, 2) """ fx = self.camera_intrinsics['fx'] fy = self.camera_intrinsics['fy'] cx = self.camera_intrinsics['cx'] cy = self.camera_intrinsics['cy'] x = keypoints_3d[:, :, 0] y = keypoints_3d[:, :, 1] z = keypoints_3d[:, :, 2] u = fx * x / z + cx v = fy * y / z + cy keypoints_2d = torch.stack([u, v], dim=-1) return keypoints_2d def _classify_posture(self, logits: torch.Tensor) -> str: """分类姿态""" classes = ['NORMAL', 'FORWARD', 'BACKWARD', 'SIDEWAY', 'LYING'] idx = torch.argmax(logits, dim=1).item() return classes[idx]
class DepthEncoder(nn.Module): """ 深度图像编码器 """ def __init__(self, input_channels: int = 1, feature_dim: int = 256): super().__init__() self.conv_layers = nn.Sequential( nn.Conv2d(input_channels, 32, kernel_size=7, stride=2, padding=3), nn.BatchNorm2d(32), nn.ReLU(), nn.Conv2d(32, 64, kernel_size=3, stride=2, padding=1), nn.BatchNorm2d(64), nn.ReLU(), nn.Conv2d(64, 128, kernel_size=3, stride=2, padding=1), nn.BatchNorm2d(128), nn.ReLU(), nn.Conv2d(128, 256, kernel_size=3, stride=2, padding=1), nn.BatchNorm2d(256), nn.ReLU(), nn.AdaptiveAvgPool2d((1, 1)), nn.Flatten(), nn.Linear(256, feature_dim) ) def forward(self, x: torch.Tensor) -> torch.Tensor: """ Args: x: 深度图像 (B, H, W) 或 (B, 1, H, W) Returns: features: (B, feature_dim) """ if x.dim() == 3: x = x.unsqueeze(1) x = (x - 1500) / 1500.0 return self.conv_layers(x)
class RGBEncoder(nn.Module): """ RGB图像编码器 """ def __init__(self, input_channels: int = 3, feature_dim: int = 256): super().__init__() from torchvision.models import resnet18 resnet = resnet18(pretrained=True) self.features = nn.Sequential(*list(resnet.children())[:-1]) self.fc = nn.Linear(512, feature_dim) def forward(self, x: torch.Tensor) -> torch.Tensor: """ Args: x: RGB图像 (B, 3, H, W) Returns: features: (B, feature_dim) """ x = self.features(x) x = x.view(x.size(0), -1) x = self.fc(x) return x
class FeatureFusion(nn.Module): """ 多模态特征融合 """ def __init__(self, modalities: List[str], fusion_dim: int = 512): super().__init__() self.fusion = nn.Linear(len(modalities) * 256, fusion_dim) def forward(self, features_list: List[torch.Tensor]) -> torch.Tensor: """ Args: features_list: 各模态特征列表 Returns: fused: (B, fusion_dim) """ concatenated = torch.cat(features_list, dim=-1) return self.fusion(concatenated)
class OOPDetector: """ Out-of-Position检测器 """ def __init__(self, keypoint_threshold: float = 50): self.threshold = keypoint_threshold self.KEYPOINTS = { 'nose': 0, 'left_eye': 1, 'right_eye': 2, 'left_ear': 3, 'right_ear': 4, 'left_shoulder': 5, 'right_shoulder': 6, 'left_elbow': 7, 'right_elbow': 8, 'left_wrist': 9, 'right_wrist': 10, 'left_hip': 11, 'right_hip': 12, 'left_knee': 13, 'right_knee': 14, 'left_ankle': 15, 'right_ankle': 16 } def detect(self, keypoints_3d: torch.Tensor, posture_class: str) -> Dict: """ 检测OOP状态 Args: keypoints_3d: (B, 17, 3) posture_class: 姿态分类 Returns: result: OOP检测结果 """ kp = keypoints_3d[0].cpu().numpy() is_oop = False oop_type = 'NORMAL' confidence = 0.9 if self._check_forward_lean(kp): is_oop = True oop_type = 'FORWARD_LEAN' confidence = 0.85 elif self._check_side_lean(kp): is_oop = True oop_type = 'SIDE_LEAN' confidence = 0.8 elif self._check_hands_near_airbag(kp): is_oop = True oop_type = 'HANDS_NEAR_AIRBAG' confidence = 0.9 if posture_class in ['FORWARD', 'SIDEWAY', 'LYING']: is_oop = True oop_type = posture_class return { 'is_oop': is_oop, 'oop_type': oop_type, 'confidence': confidence } def _check_forward_lean(self, keypoints: np.ndarray) -> bool: """ 检测前倾 判断标准: - 鼻子到髋部的Z轴距离减小 - 肩膀Z坐标小于髋部Z坐标 """ nose = keypoints[self.KEYPOINTS['nose']] left_shoulder = keypoints[self.KEYPOINTS['left_shoulder']] right_shoulder = keypoints[self.KEYPOINTS['right_shoulder']] left_hip = keypoints[self.KEYPOINTS['left_hip']] right_hip = keypoints[self.KEYPOINTS['right_hip']] shoulder_z = (left_shoulder[2] + right_shoulder[2]) / 2 hip_z = (left_hip[2] + right_hip[2]) / 2 return (hip_z - shoulder_z) > 100 def _check_side_lean(self, keypoints: np.ndarray) -> bool: """ 检测侧倾 判断标准: - 左右肩高度差 > 阈值 - 头部偏离中心 """ left_shoulder = keypoints[self.KEYPOINTS['left_shoulder']] right_shoulder = keypoints[self.KEYPOINTS['right_shoulder']] height_diff = abs(left_shoulder[1] - right_shoulder[1]) return height_diff > 80 def _check_hands_near_airbag(self, keypoints: np.ndarray) -> bool: """ 检测手部靠近气囊区域 判断标准: - 手腕位置低于方向盘中心 - 手腕Z坐标靠近仪表盘 """ left_wrist = keypoints[self.KEYPOINTS['left_wrist']] right_wrist = keypoints[self.KEYPOINTS['right_wrist']] nose = keypoints[self.KEYPOINTS['nose']] left_wrist_low = left_wrist[1] > nose[1] right_wrist_low = right_wrist[1] > nose[1] left_wrist_forward = left_wrist[2] < nose[2] - 200 right_wrist_forward = right_wrist[2] < nose[2] - 200 return (left_wrist_low and left_wrist_forward) or \ (right_wrist_low and right_wrist_forward)
if __name__ == "__main__": config = { 'feature_dim': 256, 'fusion_dim': 512, 'keypoint_threshold': 50, 'camera_intrinsics': { 'fx': 500.0, 'fy': 500.0, 'cx': 320.0, 'cy': 240.0 } } model = Occupant3DPostureEstimator(config) depth_image = np.random.rand(480, 640).astype(np.float32) * 2000 + 500 rgb_image = np.random.rand(480, 640, 3).astype(np.float32) * 255 result = model.estimate(depth_image, rgb_image) print("3D姿态估计结果:") print(f" 关键点数量: {result['keypoints_3d'].shape[0]}") print(f" 姿态分类: {result['posture_class']}") print(f" 是否OOP: {result['is_oop']}") print(f" OOP类型: {result['oop_type']}") print(f" 置信度: {result['confidence']:.2f}")
|