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
| """ 域随机化策略 缩小合成数据与真实数据的域差距 """
import numpy as np from typing import Dict, List, Tuple import json
class DomainRandomization: """ 域随机化配置 在合成数据生成时应用随机扰动,提升模型泛化能力 """ def __init__( self, config: Dict = None ): if config is None: config = self._default_config() self.config = config def _default_config(self) -> Dict: """默认随机化配置""" return { "lighting": { "intensity_range": [0.5, 1.5], "color_temp_range": [3000, 7000], "direction_noise": 0.2 }, "camera": { "position_noise": [0.02, 0.02, 0.02], "rotation_noise": [5, 5, 5], "fov_range": [85, 95] }, "texture": { "color_jitter": { "brightness": 0.2, "contrast": 0.2, "saturation": 0.2, "hue": 0.1 } }, "geometry": { "scale_range": [0.9, 1.1], "rotation_range": [-10, 10] }, "environment": { "fog_density_range": [0, 0.01], "rain_intensity_range": [0, 0.5] } } def apply_randomization( self, base_params: Dict ) -> Dict: """ 应用域随机化 Args: base_params: 基础参数 Returns: randomized_params: 随机化后的参数 """ randomized = base_params.copy() lighting = self.config["lighting"] randomized["lighting"] = { "intensity": base_params.get("lighting_intensity", 1.0) * \ np.random.uniform(*lighting["intensity_range"]), "color_temp": np.random.uniform(*lighting["color_temp_range"]) } camera = self.config["camera"] pos_noise = np.random.uniform(-1, 1, 3) * camera["position_noise"] rot_noise = np.random.uniform(-1, 1, 3) * camera["rotation_noise"] randomized["camera"] = { "position": np.array(base_params["camera_position"]) + pos_noise, "rotation": np.array(base_params.get("camera_rotation", [0, 0, 0])) + rot_noise, "fov": np.random.uniform(*camera["fov_range"]) } texture = self.config["texture"]["color_jitter"] randomized["texture"] = { "brightness": np.random.uniform(1 - texture["brightness"], 1 + texture["brightness"]), "contrast": np.random.uniform(1 - texture["contrast"], 1 + texture["contrast"]), "saturation": np.random.uniform(1 - texture["saturation"], 1 + texture["saturation"]) } return randomized def generate_randomized_batch( self, base_params: Dict, batch_size: int ) -> List[Dict]: """生成随机化批次""" return [ self.apply_randomization(base_params) for _ in range(batch_size) ]
class EuroNCAPScenarioGenerator: """ Euro NCAP场景生成器 根据Euro NCAP DSM/OMS协议生成对应测试场景 """ DSM_FATIGUE_SCENARIOS = { "F-01": { "name": "PERCLOS检测", "description": "PERCLOS ≥ 30%持续检测", "animation": "slow_blink", "duration_sec": 60, "threshold_perclos": 30 }, "F-02": { "name": "微睡眠", "description": "闭眼1.5秒以上", "animation": "eyes_closed_1_5s", "duration_sec": 5, "trigger_time_sec": 3 }, "F-04": { "name": "眼睑下垂", "description": "眼睛半闭状态", "animation": "eyelids_drooping", "duration_sec": 30, "eye_open_ratio": 0.3 }, "F-05": { "name": "打哈欠", "description": "打哈欠检测", "animation": "yawning", "duration_sec": 5, "mouth_open_ratio": 0.7 } } DSM_DISTRACTION_SCENARIOS = { "D-01": { "name": "长时间视线偏离", "description": "视线偏离道路≥3秒", "animation": "looking_away_3s", "duration_sec": 5, "gaze_offset_deg": 30 }, "D-02": { "name": "手机通话", "description": "手持手机通话", "animation": "phone_call_left", "duration_sec": 10, "hand_position": "left_ear" }, "D-03": { "name": "手机打字", "description": "手持手机打字", "animation": "phone_texting", "duration_sec": 10, "hand_position": "steering_wheel_level" }, "D-05": { "name": "低头", "description": "低头看中控", "animation": "looking_down", "duration_sec": 5, "head_pitch_deg": 45 } } CPD_SCENARIOS = { "CPD-01": { "name": "婴儿遗留", "description": "婴儿座椅中的婴儿", "occupant_type": "infant", "location": "rear_seat", "duration_min": 5 }, "CPD-02": { "name": "儿童遗留", "description": "后座儿童", "occupant_type": "child", "location": "rear_seat", "duration_min": 10 }, "CPD-03": { "name": "宠物遗留", "description": "车内宠物", "occupant_type": "pet", "location": "any_seat", "duration_min": 15 } } def generate_dsm_training_set( self, output_path: str, samples_per_scenario: int = 100 ) -> Dict: """ 生成DSM训练数据集 Args: output_path: 输出路径 samples_per_scenario: 每个场景样本数 Returns: stats: 生成统计 """ all_scenarios = [] for scenario_id, scenario in self.DSM_FATIGUE_SCENARIOS.items(): for i in range(samples_per_scenario): all_scenarios.append({ "scenario_id": scenario_id, "type": "fatigue", "name": scenario["name"], "animation": scenario["animation"], "duration_sec": scenario["duration_sec"], "metadata": { "perclos_threshold": scenario.get("threshold_perclos"), "eye_open_ratio": scenario.get("eye_open_ratio") } }) for scenario_id, scenario in self.DSM_DISTRACTION_SCENARIOS.items(): for i in range(samples_per_scenario): all_scenarios.append({ "scenario_id": scenario_id, "type": "distraction", "name": scenario["name"], "animation": scenario["animation"], "duration_sec": scenario["duration_sec"], "metadata": { "gaze_offset_deg": scenario.get("gaze_offset_deg"), "hand_position": scenario.get("hand_position") } }) output_file = f"{output_path}/dsm_scenarios.json" with open(output_file, 'w') as f: json.dump(all_scenarios, f, indent=2) return { "total_scenarios": len(all_scenarios), "fatigue_scenarios": len(self.DSM_FATIGUE_SCENARIOS) * samples_per_scenario, "distraction_scenarios": len(self.DSM_DISTRACTION_SCENARIOS) * samples_per_scenario, "output_file": output_file } def generate_cpd_training_set( self, output_path: str, samples_per_scenario: int = 100 ) -> Dict: """生成CPD训练数据集""" all_scenarios = [] for scenario_id, scenario in self.CPD_SCENARIOS.items(): for i in range(samples_per_scenario): age_variations = ["infant", "toddler", "child"] if scenario["occupant_type"] == "child" else [scenario["occupant_type"]] for age in age_variations: all_scenarios.append({ "scenario_id": scenario_id, "type": "cpd", "name": scenario["name"], "occupant_type": age, "location": scenario["location"], "duration_min": scenario["duration_min"], "metadata": { "covered": np.random.choice([True, False], p=[0.3, 0.7]), "motion": np.random.choice(["still", "slight"], p=[0.6, 0.4]) } }) output_file = f"{output_path}/cpd_scenarios.json" with open(output_file, 'w') as f: json.dump(all_scenarios, f, indent=2) return { "total_scenarios": len(all_scenarios), "output_file": output_file }
if __name__ == "__main__": dr = DomainRandomization() base_params = { "camera_position": [0, 1.5, 0.8], "camera_rotation": [0, 0, 0], "lighting_intensity": 1.0 } randomized = dr.apply_randomization(base_params) print("=== 域随机化结果 ===") print(f"原始相机位置: {base_params['camera_position']}") print(f"随机化后: {randomized['camera']['position']}") print(f"光照强度: {randomized['lighting']['intensity']:.2f}") generator = EuroNCAPScenarioGenerator() dsm_stats = generator.generate_dsm_training_set("./output", samples_per_scenario=10) cpd_stats = generator.generate_cpd_training_set("./output", samples_per_scenario=10) print("\n=== 场景生成统计 ===") print(f"DSM场景: {dsm_stats['total_scenarios']}") print(f"CPD场景: {cpd_stats['total_scenarios']}")
|