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
| """ Anyverse DMS 数据生成示例
模拟 Euro NCAP 测试场景 """
from dataclasses import dataclass from typing import List, Dict import numpy as np
@dataclass class DMSDataConfig: """DMS 数据生成配置""" age_range: tuple = (18, 70) gender_ratio: float = 0.5 ethnicity_distribution: Dict[str, float] = None lighting_conditions: List[str] = None time_of_day: List[str] = None weather: List[str] = None distraction_types: List[str] = None fatigue_levels: List[str] = None head_poses: List[tuple] = None camera_type: str = "IR" resolution: tuple = (1280, 800) fps: int = 30 num_samples: int = 10000 output_format: str = "COCO"
class AnyverseDataGenerator: """ Anyverse 数据生成器 生成 Euro NCAP 合规的 DMS 训练数据 """ def __init__(self, config: DMSDataConfig): self.config = config self.encap_scenarios = self._define_encap_scenarios() def _define_encap_scenarios(self) -> Dict: """定义 Euro NCAP 测试场景""" return { 'F-01': { 'description': '轻度疲劳 KSS 6-7', 'behaviors': ['yawning', 'eye_rubbing', 'slow_blinking'], 'duration_range': (60, 120) }, 'F-02': { 'description': '中度疲劳 KSS 7-8', 'behaviors': ['frequent_yawning', 'head_nodding', 'eye_closing'], 'duration_range': (30, 60) }, 'F-03': { 'description': '重度疲劳 KSS >8', 'behaviors': ['microsleep', 'prolonged_eye_closure'], 'duration_range': (10, 30) }, 'F-04': { 'description': '微睡眠 1-2秒', 'behaviors': ['eye_closure_1_2s'], 'duration_range': (1, 2) }, 'D-01': { 'description': '手持通话', 'behaviors': ['phone_to_ear', 'talking'], 'duration_range': (3, 10) }, 'D-02': { 'description': '手机打字', 'behaviors': ['looking_down', 'hands_on_phone', 'typing'], 'duration_range': (3, 10) }, 'D-03': { 'description': '手机浏览', 'behaviors': ['looking_down', 'scrolling'], 'duration_range': (3, 10) }, 'D-04': { 'description': '调整中控', 'behaviors': ['looking_sideways', 'hands_on_dashboard'], 'duration_range': (3, 10) }, 'D-05': { 'description': '视线偏离 >3秒', 'behaviors': ['looking_away'], 'duration_range': (3, 5) }, 'CD-01': { 'description': '心算任务', 'behaviors': ['blank_stare', 'reduced_blinking'], 'duration_range': (5, 15) }, 'CD-02': { 'description': '白日梦', 'behaviors': ['fixed_gaze', 'reduced_eye_movement'], 'duration_range': (10, 20) } } def generate_dataset( self, scenarios: List[str] = None ) -> Dict: """ 生成数据集 Args: scenarios: 要生成的场景列表(None = 全部) Returns: dataset_info: 数据集信息 """ if scenarios is None: scenarios = list(self.encap_scenarios.keys()) samples_per_scenario = self.config.num_samples // len(scenarios) dataset = { 'samples': [], 'annotations': [], 'metadata': { 'total_samples': 0, 'scenarios': {}, 'statistics': {} } } for scenario_id in scenarios: scenario = self.encap_scenarios[scenario_id] scenario_samples = self._generate_scenario_samples( scenario_id, scenario, samples_per_scenario ) dataset['samples'].extend(scenario_samples['samples']) dataset['annotations'].extend(scenario_samples['annotations']) dataset['metadata']['scenarios'][scenario_id] = { 'count': len(scenario_samples['samples']), 'description': scenario['description'] } dataset['metadata']['total_samples'] = len(dataset['samples']) dataset['metadata']['statistics'] = self._calculate_statistics(dataset) return dataset def _generate_scenario_samples( self, scenario_id: str, scenario: Dict, num_samples: int ) -> Dict: """生成单个场景的样本""" samples = [] annotations = [] for i in range(num_samples): driver = self._random_driver() scene = self._random_scene() behavior = self._random_behavior(scenario['behaviors']) sample = { 'id': f"{scenario_id}_{i:06d}", 'scenario_id': scenario_id, 'driver': driver, 'scene': scene, 'behavior': behavior, 'image_path': f"data/{scenario_id}/{i:06d}.png", 'annotation_path': f"data/{scenario_id}/{i:06d}.json" } annotation = self._generate_annotation(sample, scenario) samples.append(sample) annotations.append(annotation) return {'samples': samples, 'annotations': annotations} def _random_driver(self) -> Dict: """随机生成驾驶员属性""" return { 'age': np.random.randint(*self.config.age_range), 'gender': 'male' if np.random.random() < self.config.gender_ratio else 'female', 'ethnicity': 'asian', 'glasses': np.random.random() < 0.3, 'sunglasses': np.random.random() < 0.1, 'mask': np.random.random() < 0.1, 'facial_hair': np.random.random() < 0.3 } def _random_scene(self) -> Dict: """随机生成场景属性""" return { 'lighting': np.random.choice(['daylight', 'night', 'tunnel', 'sunset']), 'weather': np.random.choice(['clear', 'rain', 'fog']), 'vehicle_type': np.random.choice(['sedan', 'suv', 'truck']), 'camera_position': 'steering_column' } def _random_behavior(self, behaviors: List[str]) -> Dict: """随机生成行为""" selected = np.random.choice(behaviors) return { 'primary': selected, 'intensity': np.random.uniform(0.5, 1.0), 'duration': np.random.uniform(1, 10) } def _generate_annotation(self, sample: Dict, scenario: Dict) -> Dict: """生成标注""" return { 'image_id': sample['id'], 'category_id': self._get_category_id(sample['scenario_id']), 'bbox': [100, 100, 200, 200], 'keypoints': self._generate_keypoints(), 'attributes': { 'distraction_type': sample['behavior']['primary'], 'severity': sample['behavior']['intensity'] } } def _get_category_id(self, scenario_id: str) -> int: """获取类别 ID""" category_map = { 'F': 1, 'D': 2, 'CD': 3 } prefix = scenario_id.split('-')[0] return category_map.get(prefix, 0) def _generate_keypoints(self) -> List: """生成关键点""" keypoints = [] for i in range(68): x = np.random.uniform(0, 1280) y = np.random.uniform(0, 800) v = 2 keypoints.extend([x, y, v]) return keypoints def _calculate_statistics(self, dataset: Dict) -> Dict: """计算统计信息""" return { 'total_samples': dataset['metadata']['total_samples'], 'num_scenarios': len(dataset['metadata']['scenarios']), 'samples_per_scenario': dataset['metadata']['total_samples'] // len(dataset['metadata']['scenarios']) }
if __name__ == "__main__": config = DMSDataConfig( num_samples=10000, camera_type="IR", resolution=(1280, 800) ) generator = AnyverseDataGenerator(config) dataset = generator.generate_dataset( scenarios=['F-01', 'F-02', 'D-01', 'D-02', 'CD-01'] ) print(f"生成数据集: {dataset['metadata']['total_samples']} 样本") for scenario_id, info in dataset['metadata']['scenarios'].items(): print(f" {scenario_id}: {info['count']} 样本 - {info['description']}")
|