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
| """ 合成数据DMS训练数据生成 模拟Anyverse/SKY ENGINE AI生成流程
生成内容: - 多光照条件(日间/夜间/逆光) - 多遮挡场景(墨镜/口罩/帽子) - 多驾驶员状态(正常/疲劳/分心) - 自动标注(眼睑/注视/姿态) """
import numpy as np import json from typing import Dict, List from dataclasses import dataclass
@dataclass class SyntheticDriverState: """合成驾驶员状态""" age: int gender: str height_cm: float state_type: str eyes_closed_ratio: float gaze_deviation_deg: float head_yaw_deg: float head_pitch_deg: float sunglasses_type: str sunglasses_transmittance: float mask_type: str hat_type: str hair_occlusion: bool lighting_condition: str illumination_lux: float annotation: Dict
class SyntheticDataGenerator: """ 合成数据生成器 Anyverse/SKY ENGINE AI风格生成: - 可定制场景参数 - 自动标注(眼睑/注视/姿态) - ISP处理模拟 - 多样化场景覆盖 Euro NCAP要求覆盖: - 光照:日间、夜间、逆光 - 遮挡:墨镜、口罩、帽子、头发 - 状态:疲劳、分心、损伤 - 年龄:儿童、成人(不同身高) """ def __init__(self): self.lighting_conditions = { 'day': {'illumination_lux': 500, 'color_temp': 5500}, 'night': {'illumination_lux': 50, 'color_temp': 3000}, 'backlight': {'illumination_lux': 1000, 'color_temp': 6500}, 'side': {'illumination_lux': 300, 'color_temp': 4500} } self.sunglasses_types = { 'none': {'transmittance': 1.0}, 'clear': {'transmittance': 0.80}, 'light': {'transmittance': 0.50}, 'dark': {'transmittance': 0.15} } self.state_types = { 'normal': {'eyes_closed_ratio': 0.0, 'gaze_deviation': 0.0}, 'fatigue': {'eyes_closed_ratio': 0.6, 'gaze_deviation': 10.0}, 'distraction': {'eyes_closed_ratio': 0.0, 'gaze_deviation': 45.0}, 'impairment': {'eyes_closed_ratio': 0.0, 'gaze_deviation': 15.0} } def generate_driver_state(self, state_type: str, lighting_condition: str, sunglasses_type: str, mask_type: str, hat_type: str, age: int) -> SyntheticDriverState: """ 生成合成驾驶员状态 Args: state_type: 状态类型 lighting_condition: 光照条件 sunglasses_type: 墨镜类型 mask_type: 口罩类型 hat_type: 帽子类型 age: 年龄 Returns: SyntheticDriverState: 合成状态数据 """ lighting_params = self.lighting_conditions[lighting_condition] sunglasses_params = self.sunglasses_types[sunglasses_type] state_params = self.state_types[state_type] driver_state = SyntheticDriverState( age=age, gender='male' if age > 18 else 'unknown', height_cm=180.0 if age > 18 else 100.0, state_type=state_type, eyes_closed_ratio=state_params['eyes_closed_ratio'], gaze_deviation_deg=state_params['gaze_deviation'], head_yaw_deg=0.0, head_pitch_deg=0.0, sunglasses_type=sunglasses_type, sunglasses_transmittance=sunglasses_params['transmittance'], mask_type=mask_type, hat_type=hat_type, hair_occlusion=False, lighting_condition=lighting_condition, illumination_lux=lighting_params['illumination_lux'], annotation={} ) driver_state.annotation = self._generate_annotation(driver_state) return driver_state def _generate_annotation(self, state: SyntheticDriverState) -> Dict: """ 自动标注生成 Anyverse自动标注: - 眼睑状态(睁眼/闭眼) - 注视方向(前方/偏离) - 头部姿态(Yaw/Pitch/Roll) - 遮挡类型(墨镜/口罩/帽子) Args: state: 驾驶员状态 Returns: annotation: 标注字典 """ annotation = { 'eyes_closed': state.eyes_closed_ratio > 0.5, 'eyes_closed_ratio': state.eyes_closed_ratio, 'gaze_forward': state.gaze_deviation_deg < 15.0, 'gaze_deviation_deg': state.gaze_deviation_deg, 'gaze_direction': { 'x': np.sin(np.radians(state.gaze_deviation_deg)), 'y': 0.0, 'z': np.cos(np.radians(state.gaze_deviation_deg)) }, 'head_pose': { 'yaw': state.head_yaw_deg, 'pitch': state.head_pitch_deg, 'roll': 0.0 }, 'occlusion': { 'sunglasses': state.sunglasses_type != 'none', 'sunglasses_type': state.sunglasses_type, 'mask': state.mask_type != 'none', 'hat': state.hat_type != 'none', 'hair_occlusion': state.hair_occlusion }, 'state_label': state.state_type, 'age_estimate': self._estimate_age_group(state.age) } return annotation def _estimate_age_group(self, age: int) -> str: """年龄组估计""" if age < 1: return 'newborn' elif age < 3: return '1-year' elif age < 6: return '3-year' elif age < 18: return 'child' else: return 'adult' def generate_dataset(self, num_samples: int = 1000, diversity_config: Dict = None) -> List[SyntheticDriverState]: """ 生成合成数据集 Args: num_samples: 样本数量 diversity_config: 多样性配置 Returns: dataset: 合成数据集 """ dataset = [] default_diversity = { 'state_types': ['normal', 'fatigue', 'distraction', 'impairment'], 'lighting_conditions': ['day', 'night', 'backlight', 'side'], 'sunglasses_types': ['none', 'clear', 'light', 'dark'], 'mask_types': ['none', 'medical'], 'hat_types': ['none', 'cap'], 'ages': [25, 30, 35, 40, 45] } diversity = diversity_config or default_diversity for i in range(num_samples): state_type = np.random.choice(diversity['state_types']) lighting = np.random.choice(diversity['lighting_conditions']) sunglasses = np.random.choice(diversity['sunglasses_types']) mask = np.random.choice(diversity['mask_types']) hat = np.random.choice(diversity['hat_types']) age = np.random.choice(diversity['ages']) state = self.generate_driver_state( state_type, lighting, sunglasses, mask, hat, age ) dataset.append(state) return dataset def export_dataset_json(self, dataset: List[SyntheticDriverState], output_path: str): """ 导出数据集为JSON Args: dataset: 合成数据集 output_path: 输出路径 """ dataset_dict = [] for state in dataset: state_dict = { 'age': state.age, 'gender': state.gender, 'height_cm': state.height_cm, 'state_type': state.state_type, 'eyes_closed_ratio': state.eyes_closed_ratio, 'gaze_deviation_deg': state.gaze_deviation_deg, 'head_yaw_deg': state.head_yaw_deg, 'head_pitch_deg': state.head_pitch_deg, 'sunglasses_type': state.sunglasses_type, 'sunglasses_transmittance': state.sunglasses_transmittance, 'mask_type': state.mask_type, 'hat_type': state.hat_type, 'hair_occlusion': state.hair_occlusion, 'lighting_condition': state.lighting_condition, 'illumination_lux': state.illumination_lux, 'annotation': state.annotation } dataset_dict.append(state_dict) with open(output_path, 'w', encoding='utf-8') as f: json.dump(dataset_dict, f, indent=2)
if __name__ == "__main__": generator = SyntheticDataGenerator() print("=== 合成数据生成器测试 ===") state = generator.generate_driver_state( state_type='fatigue', lighting_condition='night', sunglasses_type='clear', mask_type='none', hat_type='none', age=35 ) print(f"\n生成样本:") print(f" 状态类型:{state.state_type}") print(f" 光照条件:{state.lighting_condition} ({state.illumination_lux} lux)") print(f" 墨镜类型:{state.sunglasses_type} (透光率 {state.sunglasses_transmittance})") print(f" 眼睑闭合比例:{state.eyes_closed_ratio}") print(f" 注视偏离角度:{state.gaze_deviation_deg}°") print(f"\n自动标注:") print(f" 眼睑状态:{state.annotation['eyes_closed']}") print(f" 注视前方:{state.annotation['gaze_forward']}") print(f" 墨镜遮挡:{state.annotation['occlusion']['sunglasses']}") print(f" 状态标签:{state.annotation['state_label']}") print(f"\n生成数据集...") dataset = generator.generate_dataset(num_samples=100) print(f"数据集大小:{len(dataset)}") state_types = [s.state_type for s in dataset] lighting_conditions = [s.lighting_condition for s in dataset] print(f"\n状态类型分布:") for st in set(state_types): count = sum(1 for s in state_types if s == st) print(f" {st}: {count} ({count/len(dataset)*100:.1f}%)") print(f"\n光照条件分布:") for lc in set(lighting_conditions): count = sum(1 for l in lighting_conditions if l == lc) print(f" {lc}: {count} ({count/len(dataset)*100:.1f}%)")
|