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
| import numpy as np from typing import Dict, List, Tuple from dataclasses import dataclass from enum import Enum
class CameraType(Enum): """相机类型""" RGB = "rgb" RGBD = "rgbd" IR = "infrared" STEREO = "stereo"
@dataclass class CabinSceneConfig: """座舱场景配置""" vehicle_model: str camera_position: Tuple[float, float, float] camera_rotation: Tuple[float, float, float] camera_type: CameraType resolution: Tuple[int, int] fov: float
class CabinDataGenerator: """座舱数据生成器""" def __init__(self, scene_config: CabinSceneConfig, output_dir: str): """ 初始化生成器 Args: scene_config: 场景配置 output_dir: 输出目录 """ self.config = scene_config self.output_dir = output_dir self.vehicle_asset = self._load_vehicle_asset(scene_config.vehicle_model) self.human_assets = self._load_human_assets() self.environment_assets = self._load_environment_assets() def generate_frame(self, randomize: bool = True) -> Dict: """ 生成单帧数据 Args: randomize: 是否随机化 Returns: frame_data: { "rgb": np.ndarray, "depth": np.ndarray, "segmentation": np.ndarray, "keypoints": Dict, "bbox_3d": Dict } """ if randomize: self._randomize_scene() rgb = self._render_rgb() depth = self._render_depth() segmentation = self._render_segmentation() keypoints = self._get_keypoint_annotations() bbox_3d = self._get_3d_bbox_annotations() return { "rgb": rgb, "depth": depth, "segmentation": segmentation, "keypoints": keypoints, "bbox_3d": bbox_3d } def generate_dataset(self, num_frames: int, randomize: bool = True) -> List[Dict]: """ 生成数据集 Args: num_frames: 帧数 randomize: 是否随机化 Returns: dataset: 数据集 """ dataset = [] for i in range(num_frames): frame = self.generate_frame(randomize) dataset.append(frame) if (i + 1) % 1000 == 0: print(f"已生成 {i+1}/{num_frames} 帧") return dataset def _randomize_scene(self): """随机化场景参数""" self._randomize_human_pose() self._randomize_lighting() self._randomize_camera() self._randomize_occlusion() def _randomize_human_pose(self): """随机化人物姿态""" head_roll = np.random.uniform(-30, 30) head_pitch = np.random.uniform(-30, 30) head_yaw = np.random.uniform(-60, 60) eye_openness = np.random.uniform(0.3, 1.0) blink = np.random.choice([True, False], p=[0.1, 0.9]) mouth_openness = np.random.uniform(0, 0.5) body_slouch = np.random.uniform(-10, 30) left_hand_position = self._randomize_hand_position() right_hand_position = self._randomize_hand_position() def _randomize_lighting(self): """随机化光照""" intensity = np.random.uniform(0.3, 1.0) color_temp = np.random.uniform(3000, 7000) sun_angle = np.random.uniform(0, 90) ambient = np.random.uniform(0.1, 0.3) ir_intensity = np.random.uniform(0.5, 1.0) if np.random.random() > 0.5 else 0 def _randomize_camera(self): """随机化相机参数""" position_noise = np.random.uniform(-0.02, 0.02, 3) rotation_noise = np.random.uniform(-1, 1, 3) exposure = np.random.uniform(0.8, 1.2) noise_level = np.random.uniform(0.001, 0.01) blur_radius = np.random.uniform(0, 0.5) def _randomize_occlusion(self): """随机化遮挡""" occluders = ["glasses", "mask", "hat", "hair", "none"] occlusion_prob = [0.1, 0.05, 0.05, 0.2, 0.6] selected_occluder = np.random.choice(occluders, p=occlusion_prob) occlusion_level = np.random.uniform(0.1, 0.5) def _randomize_hand_position(self) -> Tuple[float, float, float]: """随机化手部位置""" positions = { "steering_wheel": (0.3, 0.1, 0.5), "gear_shift": (0.4, 0.2, 0.3), "center_console": (0.5, 0.3, 0.4), "lap": (0.3, 0.0, 0.2) } position_name = np.random.choice(list(positions.keys())) base_position = positions[position_name] noise = np.random.uniform(-0.05, 0.05, 3) return tuple(base_position[i] + noise[i] for i in range(3)) def _render_rgb(self) -> np.ndarray: """渲染RGB图像""" return np.random.randint(0, 255, (self.config.resolution[1], self.config.resolution[0], 3), dtype=np.uint8) def _render_depth(self) -> np.ndarray: """渲染深度图""" return np.random.uniform(0.5, 3.0, (self.config.resolution[1], self.config.resolution[0])) def _render_segmentation(self) -> np.ndarray: """渲染分割掩码""" return np.random.randint(0, 10, (self.config.resolution[1], self.config.resolution[0]), dtype=np.uint8) def _get_keypoint_annotations(self) -> Dict: """获取关键点标注""" return { "face_landmarks": np.random.randn(68, 2), "body_keypoints": np.random.randn(17, 3), "eye_centers": np.random.randn(2, 2) } def _get_3d_bbox_annotations(self) -> Dict: """获取3D边界框标注""" return { "head": {"center": [0, 0, 1], "size": [0.2, 0.3, 0.25]}, "body": {"center": [0, 0, 0.5], "size": [0.4, 0.6, 0.8]} } def _load_vehicle_asset(self, vehicle_model: str): """加载车辆资产""" return None def _load_human_assets(self): """加载人物资产""" return [] def _load_environment_assets(self): """加载环境资产""" return []
if __name__ == "__main__": scene_config = CabinSceneConfig( vehicle_model="sedan_01", camera_position=(0.5, 0.3, 1.2), camera_rotation=(0, -15, 0), camera_type=CameraType.RGB, resolution=(1920, 1080), fov=60.0 ) generator = CabinDataGenerator(scene_config, "output_dir") dataset = generator.generate_dataset(10000) print(f"生成数据集: {len(dataset)} 帧") print(f"单帧数据包含: RGB、深度、分割、关键点、3D框")
|