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
| """ SKY ENGINE AI 合成数据生成示例 """
import numpy as np from typing import Dict, List, Optional from dataclasses import dataclass
@dataclass class DriverScenario: """驾驶员场景配置""" age: int gender: str skin_tone: int hair_style: str glasses: bool mask: bool state: str gaze_direction: tuple head_pose: tuple lighting: str weather: str time_of_day: float
class SkyEngineDataGenerator: """ SKY ENGINE AI 数据生成器 用于 DMS 模型训练数据生成 """ def __init__(self, api_key: str): self.api_key = api_key self.scenarios = self._init_scenarios() def _init_scenarios(self) -> Dict[str, DriverScenario]: """初始化 Euro NCAP 相关场景""" return { 'FT-01_normal': DriverScenario( age=35, gender='male', skin_tone=3, hair_style='short', glasses=False, mask=False, state='normal', gaze_direction=(0, 0), head_pose=(0, 0, 0), lighting='day', weather='clear', time_of_day=14.0 ), 'FT-02_drowsy': DriverScenario( age=45, gender='male', skin_tone=2, hair_style='short', glasses=False, mask=False, state='fatigued', gaze_direction=(0, -10), head_pose=(0, 5, 0), lighting='day', weather='clear', time_of_day=6.0 ), 'FT-03_microsleep': DriverScenario( age=30, gender='female', skin_tone=4, hair_style='long', glasses=True, mask=False, state='fatigued', gaze_direction=(0, 0), head_pose=(0, 0, 0), lighting='night', weather='clear', time_of_day=2.0 ), 'DT-01_phone_call': DriverScenario( age=25, gender='male', skin_tone=5, hair_style='short', glasses=False, mask=False, state='distracted', gaze_direction=(-30, 0), head_pose=(-20, 0, 0), lighting='day', weather='clear', time_of_day=12.0 ), 'DT-02_texting': DriverScenario( age=35, gender='female', skin_tone=3, hair_style='medium', glasses=True, mask=False, state='distracted', gaze_direction=(-45, -20), head_pose=(-30, -15, 0), lighting='day', weather='clear', time_of_day=15.0 ), 'OC-01_sunglasses': DriverScenario( age=40, gender='male', skin_tone=2, hair_style='short', glasses=True, mask=False, state='normal', gaze_direction=(0, 0), head_pose=(0, 0, 0), lighting='day', weather='clear', time_of_day=12.0 ), 'OC-02_mask': DriverScenario( age=28, gender='female', skin_tone=4, hair_style='long', glasses=False, mask=True, state='normal', gaze_direction=(0, 0), head_pose=(0, 0, 0), lighting='day', weather='clear', time_of_day=10.0 ), } def generate_dataset( self, num_samples: int = 10000, output_format: str = "coco", include_annotations: bool = True ) -> Dict: """ 生成合成数据集 Args: num_samples: 样本数量 output_format: 输出格式 ('coco', 'yolo', 'voc') include_annotations: 是否包含标注 Returns: { 'images': list of image paths, 'annotations': list of annotations, 'statistics': dataset statistics } """ samples = [] annotations = [] for i in range(num_samples): scenario_key = np.random.choice(list(self.scenarios.keys())) scenario = self.scenarios[scenario_key] randomized = self._randomize_scenario(scenario) image_data = self._render_image(randomized) samples.append(image_data) if include_annotations: annotation = self._generate_annotation(randomized) annotations.append(annotation) return { 'images': samples, 'annotations': annotations, 'statistics': self._compute_statistics(samples, annotations) } def _randomize_scenario(self, scenario: DriverScenario) -> DriverScenario: """随机化场景参数""" randomized = DriverScenario( age=scenario.age + np.random.randint(-5, 5), gender=scenario.gender, skin_tone=max(1, min(6, scenario.skin_tone + np.random.randint(-1, 1))), hair_style=scenario.hair_style, glasses=scenario.glasses, mask=scenario.mask, state=scenario.state, gaze_direction=( scenario.gaze_direction[0] + np.random.randn() * 5, scenario.gaze_direction[1] + np.random.randn() * 5 ), head_pose=( scenario.head_pose[0] + np.random.randn() * 3, scenario.head_pose[1] + np.random.randn() * 3, scenario.head_pose[2] + np.random.randn() * 2 ), lighting=scenario.lighting, weather=scenario.weather, time_of_day=scenario.time_of_day + np.random.rand() * 2 ) return randomized def _render_image(self, scenario: DriverScenario) -> dict: """渲染图像(模拟)""" return { 'path': f'synthetic/image_{hash(scenario)}.jpg', 'width': 640, 'height': 480, 'scenario': scenario } def _generate_annotation(self, scenario: DriverScenario) -> dict: """生成标注""" return { 'face_bbox': [100, 100, 200, 200], 'landmarks_68': self._generate_landmarks(scenario), 'gaze_direction': scenario.gaze_direction, 'head_pose': scenario.head_pose, 'state': scenario.state } def _generate_landmarks(self, scenario: DriverScenario) -> List: """生成 68 个关键点""" return [[x, y] for x, y in zip(range(68), range(68))] def _compute_statistics(self, samples, annotations) -> dict: """计算数据集统计""" return { 'total_samples': len(samples), 'state_distribution': { 'normal': len(samples) // 3, 'fatigued': len(samples) // 3, 'distracted': len(samples) // 3 } }
|