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
| import numpy as np
class OmniverseSDGPipeline: """ NVIDIA Omniverse合成数据生成管道 组件: - Isaac Sim:物理仿真 - Omniverse Replicator:数据生成 - Metahuman:虚拟人物 - OpenUSD:场景描述 支持生成: - DMS数据:疲劳、分心、损伤场景 - OMS数据:乘员姿态、儿童场景 - OOP数据:异常姿态场景 - CPD数据:儿童存在场景 """ def __init__(self, config): self.simulation_app = self.start_simulation(config) self.cabin = CabinEnvironment() self.driver = VirtualDriver() self.occupants = VirtualOccupants() self.cameras = self.setup_cameras() self.sensors = self.setup_sensors() def start_simulation(self, config): """ 启动Isaac Sim仿真 配置: - 渲染:RTX光线追踪 - 物理率:120Hz - 输出:RGB、深度、分割、关键点 """ from omni.isaac.kit import SimulationApp config = { 'headless': True, 'width': 1920, 'height': 1080, 'renderer': 'RayTraced', 'physics_dt': 1/120 } sim_app = SimulationApp(config) return sim_app def setup_cameras(self): """ 配置虚拟相机 典型配置: - DMS相机:仪表台,看向驾驶员 - OMS相机:顶棚,看向后排 - CPD相机:后排座椅上方 """ cameras = {} cameras['dms'] = { 'position': (0.3, -0.4, 1.2), 'rotation': (0, -15, 0), 'fov': 60, 'resolution': (1920, 1080) } cameras['oms'] = { 'position': (0.0, 0.0, 1.5), 'rotation': (0, -30, 0), 'fov': 120, 'resolution': (1920, 1080) } return cameras def setup_sensors(self): """ 配置虚拟传感器 支持类型: - RGB相机 - 深度相机 - 红外相机(模拟) - mmWave雷达(点云) """ sensors = {} sensors['rgb'] = {'type': 'RGB', 'format': 'png'} sensors['depth'] = {'type': 'Depth', 'format': 'tiff'} sensors['segmentation'] = {'type': 'Semantic', 'format': 'png'} sensors['keypoints'] = {'type': 'Keypoint2D', 'format': 'json'} return sensors def generate_scene(self, scenario_config): """ 生成场景 Args: scenario_config: dict - 'scenario_type': 'fatigue', 'distraction', 'oop', etc. - 'driver_state': driver configuration - 'environment': lighting, weather, etc. Returns: data: dict with images, annotations """ self.cabin.load('sedan_cabin.usd') self.driver.configure(scenario_config['driver_state']) self.configure_environment(scenario_config['environment']) data = self.render_frame() return data def render_frame(self): """ 渲染单帧 输出: - RGB图像 - 深度图 - 分割图 - 关键点标注 - 元数据 """ data = {} for cam_name, cam_config in self.cameras.items(): self.set_camera(cam_config) rgb = self.render_rgb() depth = self.render_depth() segmentation = self.render_segmentation() keypoints = self.extract_keypoints() data[cam_name] = { 'rgb': rgb, 'depth': depth, 'segmentation': segmentation, 'keypoints': keypoints } return data def extract_keypoints(self): """ 提取关键点 支持: - 面部关键点(68点) - 身体关键点(17点) - 手部关键点(21点) """ face_keypoints = self.driver.get_face_keypoints() body_keypoints = self.driver.get_body_keypoints() hand_keypoints = self.driver.get_hand_keypoints() keypoints = { 'face': face_keypoints, 'body': body_keypoints, 'hands': hand_keypoints } return keypoints
class FatigueScenarioGenerator: """ 疲劳场景生成器 场景类型: - F-01:PERCLOS≥30%(眼睑下垂) - F-02:微睡眠(闭眼1-2秒) - F-03:打哈欠 - F-04:头部下垂 """ def __init__(self, pipeline): self.pipeline = pipeline def generate_fatigue_dataset(self, num_samples=1000): """ 生成疲劳数据集 变量: - 疲劳程度:轻度/中度/重度 - 光照:白天/黄昏/夜晚 - 遮挡:无/墨镜/口罩 - 头部姿态:正面/侧面/低头 """ dataset = [] for i in range(num_samples): fatigue_level = np.random.choice(['light', 'moderate', 'severe']) lighting = np.random.choice(['day', 'dusk', 'night']) occlusion = np.random.choice(['none', 'sunglasses', 'mask']) scenario_config = { 'scenario_type': 'fatigue', 'driver_state': { 'fatigue_level': fatigue_level, 'eye_openness': self.compute_eye_openness(fatigue_level), 'blink_rate': self.compute_blink_rate(fatigue_level), 'head_pose': self.compute_head_pose(fatigue_level) }, 'environment': { 'lighting': lighting, 'occlusion': occlusion } } data = self.pipeline.generate_scene(scenario_config) annotation = self.annotate_fatigue(data, fatigue_level) dataset.append({ 'data': data, 'annotation': annotation }) return dataset def compute_eye_openness(self, fatigue_level): """ 计算眼睑开度 正常:0.8-1.0 轻度疲劳:0.6-0.8 中度疲劳:0.4-0.6 重度疲劳:0.2-0.4 """ if fatigue_level == 'light': return np.random.uniform(0.6, 0.8) elif fatigue_level == 'moderate': return np.random.uniform(0.4, 0.6) else: return np.random.uniform(0.2, 0.4) def compute_blink_rate(self, fatigue_level): """ 计算眨眼频率 正常:15-20次/分钟 疲劳:增加 """ if fatigue_level == 'light': return np.random.uniform(20, 30) elif fatigue_level == 'moderate': return np.random.uniform(25, 35) else: return np.random.uniform(30, 40) def compute_head_pose(self, fatigue_level): """ 计算头部姿态 疲劳:头部下垂 """ if fatigue_level == 'severe': return {'pitch': np.random.uniform(-20, -30)} else: return {'pitch': np.random.uniform(-5, 5)} def annotate_fatigue(self, data, fatigue_level): """ 标注疲劳程度 """ label_map = { 'light': 1, 'moderate': 2, 'severe': 3 } annotation = { 'fatigue_label': label_map[fatigue_level], 'eye_openness': data['dms']['keypoints']['face']['eye_openness'], 'blink_events': [], 'yawn_events': [] } return annotation
class DistractionScenarioGenerator: """ 分心场景生成器 场景类型: - D-01:手机使用 - D-02:视线偏离 - D-03:调整设备 - D-04:认知分心 """ def __init__(self, pipeline): self.pipeline = pipeline def generate_distraction_dataset(self, num_samples=1000): """ 生成分心数据集 """ dataset = [] distraction_types = ['phone', 'gaze_away', 'device', 'cognitive'] for i in range(num_samples): distraction_type = np.random.choice(distraction_types) scenario_config = { 'scenario_type': 'distraction', 'driver_state': { 'distraction_type': distraction_type, 'gaze_target': self.compute_gaze_target(distraction_type), 'hand_position': self.compute_hand_position(distraction_type) }, 'environment': { 'lighting': np.random.choice(['day', 'night']) } } data = self.pipeline.generate_scene(scenario_config) annotation = self.annotate_distraction(data, distraction_type) dataset.append({'data': data, 'annotation': annotation}) return dataset def compute_gaze_target(self, distraction_type): """ 计算视线目标 正常:道路前方 手机:手部位置 设备:仪表台 认知:前方但无焦点 """ if distraction_type == 'phone': return 'hand' elif distraction_type == 'gaze_away': return np.random.choice(['left', 'right', 'down']) elif distraction_type == 'device': return 'dashboard' else: return 'road_unfocused' def compute_hand_position(self, distraction_type): """ 计算手部位置 """ if distraction_type == 'phone': return {'left': 'steering', 'right': 'phone_ear'} else: return {'left': 'steering', 'right': 'steering'} def annotate_distraction(self, data, distraction_type): """ 标注分心类型 """ label_map = { 'phone': 1, 'gaze_away': 2, 'device': 3, 'cognitive': 4 } annotation = { 'distraction_label': label_map[distraction_type], 'gaze_direction': data['dms']['keypoints']['face']['gaze_direction'], 'hand_objects': [] } return annotation
def generate_ims_dataset(): """ 生成完整IMS数据集 数据量: - 疲劳场景:10000张 - 分心场景:10000张 - OOP场景:5000张 - CPD场景:5000张 - 正常驾驶:20000张 总计:50000张 """ pipeline = OmniverseSDGPipeline(config={}) fatigue_gen = FatigueScenarioGenerator(pipeline) fatigue_dataset = fatigue_gen.generate_fatigue_dataset(num_samples=10000) distraction_gen = DistractionScenarioGenerator(pipeline) distraction_dataset = distraction_gen.generate_distraction_dataset(num_samples=10000) full_dataset = fatigue_dataset + distraction_dataset save_dataset(full_dataset, 'ims_synthetic_dataset_v1.h5') print(f"生成数据集大小: {len(full_dataset)}") return full_dataset
def save_dataset(dataset, filename): """ 保存数据集 格式:HDF5(高效存储大图像数据) """ import h5py with h5py.File(filename, 'w') as f: for i, sample in enumerate(dataset): group = f.create_group(f'sample_{i}') group.create_dataset('rgb', data=sample['data']['dms']['rgb']) group.create_dataset('depth', data=sample['data']['dms']['depth']) for key, value in sample['annotation'].items(): group.attrs[key] = value
if __name__ == "__main__": dataset = generate_ims_dataset()
|