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
| """ 合成数据自动标注生成器
基于 Anyverse 渲染引擎输出 """
import numpy as np from dataclasses import dataclass from typing import List, Dict, Optional
@dataclass class FaceAnnotation: """面部标注数据""" keypoints_2d: np.ndarray keypoints_3d: np.ndarray eye_openness_left: float eye_openness_right: float gaze_vector: np.ndarray blink_state: bool yawn_state: bool yawn_intensity: float @dataclass class HandAnnotation: """手部标注数据""" keypoints_2d: np.ndarray keypoints_3d: np.ndarray is_holding_object: bool object_type: str occlusion_ratio: float
@dataclass class BehaviorAnnotation: """行为标注数据""" category: str subcategory: str severity: float duration_frames: int confidence: float
class SyntheticDataGenerator: """ 合成数据生成器 生成 Euro NCAP 2026 所需的 DMS 训练数据 """ def __init__(self, config: dict): self.config = config self.scene_templates = { 'normal_driving': self._generate_normal, 'phone_call': self._generate_phone_call, 'texting': self._generate_texting, 'eating': self._generate_eating, 'yawning': self._generate_yawning, 'drowsy': self._generate_drowsy, 'cognitive_distraction': self._generate_cognitive, } self.occlusion_types = ['none', 'sunglasses', 'mask', 'hat', 'steering_wheel'] self.lighting_conditions = ['day', 'dusk', 'night', 'tunnel', 'backlight'] def generate_dataset( self, target_scenes: List[str], samples_per_scene: int = 1000, occlusion_mix: bool = True, lighting_mix: bool = True ) -> Dict: """ 生成合成数据集 Args: target_scenes: 目标场景列表 samples_per_scene: 每个场景样本数 occlusion_mix: 是否混合遮挡 lighting_mix: 是否混合光照 Returns: 数据集字典 """ dataset = { 'images': [], 'annotations': [], 'metadata': [] } for scene in target_scenes: for i in range(samples_per_scene): occlusion = np.random.choice(self.occlusion_types) if occlusion_mix else 'none' lighting = np.random.choice(self.lighting_conditions) if lighting_mix else 'day' image, annotation, metadata = self._generate_sample( scene, occlusion, lighting ) dataset['images'].append(image) dataset['annotations'].append(annotation) dataset['metadata'].append(metadata) return dataset def _generate_sample( self, scene: str, occlusion: str, lighting: str ) -> tuple: """ 生成单个样本 Returns: (image, annotation, metadata) """ annotation = self._generate_annotation(scene, occlusion) metadata = { 'scene': scene, 'occlusion': occlusion, 'lighting': lighting, 'synthetic': True, 'source': 'anyverse' } image = np.random.randint(0, 255, (1080, 1920, 3), dtype=np.uint8) return image, annotation, metadata def _generate_annotation(self, scene: str, occlusion: str) -> dict: """ 生成标注数据 """ face = FaceAnnotation( keypoints_2d=np.random.randint(0, 1920, (68, 2)), keypoints_3d=np.random.randn(68, 3) * 100, eye_openness_left=np.random.uniform(0.3, 1.0), eye_openness_right=np.random.uniform(0.3, 1.0), gaze_vector=self._get_gaze_for_scene(scene), blink_state=np.random.random() < 0.05, yawn_state=scene in ['yawning', 'drowsy'], yawn_intensity=0.8 if scene == 'yawning' else 0.2 ) hand_left = HandAnnotation( keypoints_2d=np.random.randint(0, 1920, (21, 2)), keypoints_3d=np.random.randn(21, 3) * 100, is_holding_object=scene in ['phone_call', 'texting', 'eating'], object_type='phone' if scene in ['phone_call', 'texting'] else 'food' if scene == 'eating' else 'none', occlusion_ratio=0.3 if occlusion == 'steering_wheel' else 0.0 ) hand_right = HandAnnotation( keypoints_2d=np.random.randint(0, 1920, (21, 2)), keypoints_3d=np.random.randn(21, 3) * 100, is_holding_object=False, object_type='steering_wheel' if scene == 'normal_driving' else 'none', occlusion_ratio=0.2 ) behavior = BehaviorAnnotation( category=self._get_behavior_category(scene), subcategory=scene, severity=self._get_severity(scene), duration_frames=np.random.randint(30, 300), confidence=1.0 ) return { 'face': face.__dict__, 'hand_left': hand_left.__dict__, 'hand_right': hand_right.__dict__, 'behavior': behavior.__dict__ } def _get_gaze_for_scene(self, scene: str) -> np.ndarray: """ 根据场景生成视线向量 """ gaze_map = { 'normal_driving': np.array([0, 0, 1]), 'phone_call': np.array([-0.3, -0.2, 0.9]), 'texting': np.array([-0.4, -0.3, 0.8]), 'eating': np.array([0, -0.4, 0.9]), 'yawning': np.array([0, 0, 1]), 'drowsy': np.array([0.1, 0.1, 0.98]), 'cognitive_distraction': np.array([0.2, 0.1, 0.97]) } base_gaze = gaze_map.get(scene, np.array([0, 0, 1])) noise = np.random.randn(3) * 0.05 gaze = base_gaze + noise return gaze / np.linalg.norm(gaze) def _get_behavior_category(self, scene: str) -> str: """ 获取行为类别 """ category_map = { 'normal_driving': 'safe', 'phone_call': 'distraction_visual', 'texting': 'distraction_visual', 'eating': 'distraction_manual', 'yawning': 'fatigue', 'drowsy': 'fatigue', 'cognitive_distraction': 'cognitive' } return category_map.get(scene, 'safe') def _get_severity(self, scene: str) -> float: """ 获取严重程度 """ severity_map = { 'normal_driving': 0.0, 'phone_call': 0.6, 'texting': 0.8, 'eating': 0.5, 'yawning': 0.4, 'drowsy': 0.9, 'cognitive_distraction': 0.7 } return severity_map.get(scene, 0.0) def _generate_normal(self): pass def _generate_phone_call(self): pass def _generate_texting(self): pass def _generate_eating(self): pass def _generate_yawning(self): pass def _generate_drowsy(self): pass def _generate_cognitive(self): pass
if __name__ == "__main__": config = { 'resolution': (1920, 1080), 'fps': 30, 'output_format': 'png' } generator = SyntheticDataGenerator(config) dataset = generator.generate_dataset( target_scenes=['cognitive_distraction', 'drowsy', 'yawning'], samples_per_scene=5000, occlusion_mix=True, lighting_mix=True ) print(f"生成数据集大小: {len(dataset['images'])} 张图像") print(f"标注类型: {list(dataset['annotations'][0].keys())}")
|