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
| """ 车载感知合成数据生成器 模拟Anyverse核心功能 """
import numpy as np from typing import Dict, List, Tuple, Optional from dataclasses import dataclass import json
@dataclass class DriverState: """驾驶员状态""" state_name: str eye_openness: float mouth_openness: float head_pitch: float head_yaw: float head_roll: float gaze_direction: Tuple[float, float, float] blink_rate: float perclos: float
@dataclass class LightingCondition: """光照条件""" name: str ambient_intensity: float sun_intensity: float sun_angle: Tuple[float, float] color_temperature: int ir_intensity: float
@dataclass class CameraConfig: """相机配置""" name: str sensor_type: str resolution: Tuple[int, int] fov: float position: Tuple[float, float, float] rotation: Tuple[float, float, float] exposure_time: float gain: float
class SyntheticDataGenerator: """ 合成数据生成器 模拟Anyverse的核心功能 """ EURO_NCAP_SCENARIOS = { "F-01_PERCLOS": { "description": "PERCLOS阈值检测", "trigger_condition": "PERCLOS >= 30% over 60s", "label": "fatigue_level_2" }, "F-02_microsleep": { "description": "微睡眠检测", "trigger_condition": "eyes_closed >= 1.5s", "label": "microsleep" }, "F-04_eyelid_droop": { "description": "眼睑下垂", "trigger_condition": "eye_openness <= 50%", "label": "eyelid_droop" }, "F-05_yawning": { "description": "打哈欠", "trigger_condition": "mouth_open > 80% for >= 3s", "label": "yawning" }, "D-01_long_distraction": { "description": "长时间分心", "trigger_condition": "gaze_off_road >= 3s", "label": "distraction" }, "D-02_phone_handheld": { "description": "手持电话", "trigger_condition": "hand_near_ear AND phone_detected", "label": "phone_use" } } def __init__( self, output_dir: str = "./synthetic_data" ): """ Args: output_dir: 输出目录 """ self.output_dir = output_dir self.generated_samples = [] def generate_driver_states( self, num_samples: int = 1000, include_variations: bool = True ) -> List[DriverState]: """ 生成多样化的驾驶员状态 Args: num_samples: 样本数 include_variations: 是否包含变体 Returns: states: 驾驶员状态列表 """ states = [] base_states = [ DriverState( state_name="normal", eye_openness=0.85, mouth_openness=0.1, head_pitch=0.0, head_yaw=0.0, head_roll=0.0, gaze_direction=(0.0, 0.0, 1.0), blink_rate=15.0, perclos=5.0 ), DriverState( state_name="drowsy_mild", eye_openness=0.6, mouth_openness=0.15, head_pitch=5.0, head_yaw=0.0, head_roll=0.0, gaze_direction=(0.0, -0.1, 1.0), blink_rate=25.0, perclos=20.0 ), DriverState( state_name="drowsy_severe", eye_openness=0.3, mouth_openness=0.2, head_pitch=10.0, head_yaw=0.0, head_roll=2.0, gaze_direction=(0.0, -0.2, 0.98), blink_rate=35.0, perclos=45.0 ), DriverState( state_name="microsleep", eye_openness=0.0, mouth_openness=0.1, head_pitch=15.0, head_yaw=0.0, head_roll=0.0, gaze_direction=(0.0, 0.0, 1.0), blink_rate=0.0, perclos=100.0 ), DriverState( state_name="yawning", eye_openness=0.2, mouth_openness=0.85, head_pitch=5.0, head_yaw=0.0, head_roll=0.0, gaze_direction=(0.0, 0.0, 1.0), blink_rate=20.0, perclos=40.0 ), DriverState( state_name="looking_left", eye_openness=0.85, mouth_openness=0.1, head_pitch=0.0, head_yaw=-35.0, head_roll=0.0, gaze_direction=(-0.6, 0.0, 0.8), blink_rate=15.0, perclos=5.0 ), DriverState( state_name="looking_down_phone", eye_openness=0.85, mouth_openness=0.1, head_pitch=25.0, head_yaw=0.0, head_roll=0.0, gaze_direction=(0.0, -0.5, 0.87), blink_rate=10.0, perclos=5.0 ), DriverState( state_name="phone_call", eye_openness=0.7, mouth_openness=0.4, head_pitch=5.0, head_yaw=-15.0, head_roll=10.0, gaze_direction=(-0.3, 0.0, 0.95), blink_rate=12.0, perclos=10.0 ) ] samples_per_state = num_samples // len(base_states) for base in base_states: for i in range(samples_per_state): if include_variations: state = DriverState( state_name=base.state_name, eye_openness=np.clip(base.eye_openness + np.random.normal(0, 0.05), 0, 1), mouth_openness=np.clip(base.mouth_openness + np.random.normal(0, 0.05), 0, 1), head_pitch=base.head_pitch + np.random.normal(0, 3), head_yaw=base.head_yaw + np.random.normal(0, 5), head_roll=base.head_roll + np.random.normal(0, 2), gaze_direction=tuple(np.array(base.gaze_direction) + np.random.normal(0, 0.05, 3)), blink_rate=max(0, base.blink_rate + np.random.normal(0, 3)), perclos=np.clip(base.perclos + np.random.normal(0, 5), 0, 100) ) else: state = base states.append(state) return states def generate_lighting_variations( self, num_variations: int = 100 ) -> List[LightingCondition]: """ 生成光照变化 Args: num_variations: 变化数量 Returns: conditions: 光照条件列表 """ conditions = [] presets = [ LightingCondition("day_clear", 1000, 50000, (60, 180), 6500, 0), LightingCondition("day_overcast", 800, 20000, (45, 180), 7000, 0), LightingCondition("sunset", 400, 5000, (10, 270), 3500, 0), LightingCondition("night_urban", 50, 0, (0, 0), 3000, 200), LightingCondition("tunnel_entrance", 300, 1000, (45, 180), 5500, 100), LightingCondition("tunnel_exit", 600, 20000, (45, 180), 6500, 50), LightingCondition("backlight", 200, 80000, (20, 180), 5500, 0), LightingCondition("garage", 100, 0, (0, 0), 4000, 150) ] for _ in range(num_variations): base = presets[np.random.randint(len(presets))] condition = LightingCondition( name=base.name, ambient_intensity=max(0, base.ambient_intensity + np.random.normal(0, 100)), sun_intensity=max(0, base.sun_intensity + np.random.normal(0, 5000)), sun_angle=(base.sun_angle[0] + np.random.normal(0, 5), base.sun_angle[1] + np.random.normal(0, 20)), color_temperature=int(base.color_temperature + np.random.normal(0, 500)), ir_intensity=max(0, base.ir_intensity + np.random.normal(0, 30)) ) conditions.append(condition) return conditions def generate_sample( self, driver_state: DriverState, lighting: LightingCondition, camera: CameraConfig ) -> Dict: """ 生成单个样本 Args: driver_state: 驾驶员状态 lighting: 光照条件 camera: 相机配置 Returns: sample: 样本数据 """ image_id = f"{driver_state.state_name}_{lighting.name}_{np.random.randint(10000)}" annotations = { "image_id": image_id, "driver_state": { "state": driver_state.state_name, "eye_openness": driver_state.eye_openness, "mouth_openness": driver_state.mouth_openness, "head_pose": { "pitch": driver_state.head_pitch, "yaw": driver_state.head_yaw, "roll": driver_state.head_roll }, "gaze": list(driver_state.gaze_direction), "perclos": driver_state.perclos }, "lighting": { "condition": lighting.name, "ambient": lighting.ambient_intensity, "color_temp": lighting.color_temperature }, "camera": { "type": camera.sensor_type, "resolution": list(camera.resolution) }, "euro_ncap_label": self._get_euro_ncap_label(driver_state), "bounding_boxes": self._generate_bbox(driver_state, camera), "keypoints": self._generate_keypoints(driver_state, camera) } return annotations def _get_euro_ncap_label(self, state: DriverState) -> str: """根据状态获取Euro NCAP标签""" if state.perclos >= 30: return "F-01_PERCLOS" elif state.eye_openness == 0: return "F-02_microsleep" elif state.eye_openness <= 0.5: return "F-04_eyelid_droop" elif state.mouth_openness >= 0.8: return "F-05_yawning" elif abs(state.head_yaw) >= 30 or abs(state.head_pitch) >= 20: return "D-01_long_distraction" else: return "normal" def _generate_bbox( self, state: DriverState, camera: CameraConfig ) -> Dict: """生成边界框标注""" base_x, base_y = camera.resolution[0] // 2, camera.resolution[1] // 2 face_size = 200 yaw_offset = int(state.head_yaw * 2) pitch_offset = int(state.head_pitch * 2) return { "face": [ base_x - face_size // 2 + yaw_offset, base_y - face_size // 2 + pitch_offset, face_size, face_size ], "left_eye": [ base_x - 50 + yaw_offset, base_y - 30 + pitch_offset, 40, 20 ], "right_eye": [ base_x + 10 + yaw_offset, base_y - 30 + pitch_offset, 40, 20 ], "mouth": [ base_x - 30 + yaw_offset, base_y + 30 + pitch_offset, 60, int(40 * state.mouth_openness) ] } def _generate_keypoints( self, state: DriverState, camera: CameraConfig ) -> List[Dict]: """生成关键点标注""" base_x, base_y = camera.resolution[0] // 2, camera.resolution[1] // 2 yaw_offset = int(state.head_yaw * 2) pitch_offset = int(state.head_pitch * 2) keypoints = [ {"name": "left_eye_outer", "x": base_x - 60 + yaw_offset, "y": base_y - 35 + pitch_offset}, {"name": "left_eye_inner", "x": base_x - 30 + yaw_offset, "y": base_y - 35 + pitch_offset}, {"name": "right_eye_inner", "x": base_x + 10 + yaw_offset, "y": base_y - 35 + pitch_offset}, {"name": "right_eye_outer", "x": base_x + 40 + yaw_offset, "y": base_y - 35 + pitch_offset}, {"name": "nose_tip", "x": base_x + yaw_offset, "y": base_y + pitch_offset}, {"name": "mouth_left", "x": base_x - 25 + yaw_offset, "y": base_y + 40 + pitch_offset}, {"name": "mouth_right", "x": base_x + 25 + yaw_offset, "y": base_y + 40 + pitch_offset}, {"name": "chin", "x": base_x + yaw_offset, "y": base_y + 70 + pitch_offset} ] return keypoints def generate_dataset( self, num_samples: int = 10000, output_format: str = "coco" ) -> Dict: """ 生成完整数据集 Args: num_samples: 样本数 output_format: 输出格式 (coco, yolo, voc) Returns: dataset: 数据集信息 """ driver_states = self.generate_driver_states(num_samples) lighting_conditions = self.generate_lighting_variations(100) camera = CameraConfig( name="dms_camera", sensor_type="RGB-IR", resolution=(640, 480), fov=60.0, position=(0.0, 0.3, 0.5), rotation=(0.0, 15.0, 0.0), exposure_time=10.0, gain=100.0 ) samples = [] for i, state in enumerate(driver_states): lighting = lighting_conditions[i % len(lighting_conditions)] sample = self.generate_sample(state, lighting, camera) samples.append(sample) if (i + 1) % 1000 == 0: print(f"已生成 {i + 1}/{num_samples} 样本") dataset_info = { "total_samples": len(samples), "states_distribution": {}, "output_format": output_format } for sample in samples: state = sample["driver_state"]["state"] dataset_info["states_distribution"][state] = \ dataset_info["states_distribution"].get(state, 0) + 1 return dataset_info
if __name__ == "__main__": generator = SyntheticDataGenerator() dataset_info = generator.generate_dataset(num_samples=10000) print("=== 合成数据生成完成 ===") print(f"总样本数: {dataset_info['total_samples']}") print(f"状态分布:") for state, count in dataset_info['states_distribution'].items(): print(f" {state}: {count}")
|