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
| import numpy as np from dataclasses import dataclass from typing import Tuple, Optional import torch import torch.nn as nn
@dataclass class OccupantMetrics: """乘员测量指标""" estimated_height_cm: float estimated_weight_kg: float shoulder_width_cm: float hip_width_cm: float head_position: Tuple[float, float, float] confidence: float
class StatureClassifier(nn.Module): """ 乘员身材分类器 基于 3D 深度信息估计身高和体重 """ def __init__(self, input_channels: int = 1): super().__init__() self.depth_encoder = nn.Sequential( nn.Conv2d(input_channels, 32, 5, stride=2, padding=2), 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.AdaptiveAvgPool2d((8, 8)) ) self.height_regressor = nn.Sequential( nn.Linear(128 * 8 * 8, 256), nn.ReLU(), nn.Dropout(0.3), nn.Linear(256, 1), nn.ReLU() ) self.weight_regressor = nn.Sequential( nn.Linear(128 * 8 * 8, 256), nn.ReLU(), nn.Dropout(0.3), nn.Linear(256, 1), nn.ReLU() ) self.stature_classifier = nn.Sequential( nn.Linear(2, 64), nn.ReLU(), nn.Linear(64, 3) ) def forward(self, depth_map: torch.Tensor) -> dict: """ 前向传播 Args: depth_map: 深度图, shape=(B, 1, H, W) Returns: height: 估计身高 (cm) weight: 估计体重 (kg) stature_class: 身材分类 (0=5th, 1=50th, 2=95th) """ features = self.depth_encoder(depth_map) features = features.view(features.size(0), -1) height = self.height_regressor(features) weight = self.weight_regressor(features) hw_features = torch.cat([height, weight], dim=1) stature_logits = self.stature_classifier(hw_features) return { 'height_cm': height.squeeze(-1), 'weight_kg': weight.squeeze(-1), 'stature_logits': stature_logits }
class OccupantClassificationSystem: """ 乘员分类系统 整合多种传感器数据进行分类 """ def __init__(self): self.stature_classifier = StatureClassifier() self.stature_classifier.eval() self.seat_pressure_thresholds = { 'empty': 5.0, 'child': 30.0, 'adult': 50.0 } def classify( self, depth_map: np.ndarray, seat_pressure: float, seat_track_position: int, seat_back_angle: float ) -> dict: """ 综合分类 Args: depth_map: 深度图 seat_pressure: 座椅压力传感器读数 (kg) seat_track_position: 座椅滑轨位置 (0-100) seat_back_angle: 靠背角度 (度) Returns: classification: { 'stature_class': str, 'estimated_height': float, 'estimated_weight': float, 'airbag_strategy': str, 'restraint_strategy': dict } """ depth_tensor = torch.tensor(depth_map).float().unsqueeze(0).unsqueeze(0) with torch.no_grad(): outputs = self.stature_classifier(depth_tensor) estimated_height = outputs['height_cm'].item() estimated_weight = outputs['weight_kg'].item() stature_class = torch.argmax(outputs['stature_logits'], dim=1).item() if seat_pressure > 0: weight_correction_factor = seat_pressure / max(estimated_weight, 30) if 0.8 < weight_correction_factor < 1.2: pass else: estimated_weight = seat_pressure class_map = { 0: '5th_percentile', 1: '50th_percentile', 2: '95th_percentile' } stature_name = class_map[stature_class] airbag_strategy = self._get_airbag_strategy( stature_name, seat_track_position, seat_back_angle ) restraint_strategy = self._get_restraint_strategy( stature_name, estimated_height, estimated_weight ) return { 'stature_class': stature_name, 'estimated_height_cm': estimated_height, 'estimated_weight_kg': estimated_weight, 'airbag_strategy': airbag_strategy, 'restraint_strategy': restraint_strategy, 'confidence': 0.85 } def _get_airbag_strategy( self, stature_class: str, seat_position: int, back_angle: float ) -> str: """确定安全气囊策略""" if stature_class == '5th_percentile': if seat_position < 30: return 'low_power' return 'standard' if stature_class == '50th_percentile': return 'standard' if stature_class == '95th_percentile': return 'high_power' return 'standard' def _get_restraint_strategy( self, stature_class: str, height: float, weight: float ) -> dict: """确定约束系统策略""" pretensioner_force = 'standard' if weight < 55: pretensioner_force = 'low' elif weight > 85: pretensioner_force = 'high' load_limiter = 'standard' if stature_class == '5th_percentile': load_limiter = 'reduced' elif stature_class == '95th_percentile': load_limiter = 'increased' return { 'pretensioner_force': pretensioner_force, 'load_limiter_level': load_limiter, 'airbag_deployment_level': self._get_airbag_strategy(stature_class, 50, 25) }
|