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
| from omni.isaac.kit import SimulationApp
simulation_app = SimulationApp({ "width": 1920, "height": 1080, "headless": True, "physics_dt": 1/60.0, "rendering_dt": 1/30.0, })
from omni.isaac.core import World from omni.isaac.core.robots import Robot from omni.isaac.core.utils.stage import add_reference_to_stage import omni.replicator.core as rep
class DMSSyntheticDataGenerator: """ DMS合成数据生成器 基于Isaac Sim + Replicator """ def __init__(self): self.world = World(stage_units_in_meters=1.0) self.camera = None self.driver = None def setup_cabin(self): """设置座舱场景""" cabin_usd = "/path/to/car_cabin.usd" add_reference_to_stage(usd_path=cabin_usd, prim_path="/World/Cabin") self.camera = self.world.scene.add( "DMS_Camera", prim_path="/World/Cabin/DMS_Camera", position=np.array([0.2, -0.5, 1.2]), orientation=euler_angles_to_quat([0, -15, 0]), fov=60, clipping_range=(0.1, 10.0) ) def setup_driver(self): """设置驾驶员Metahuman""" metahuman_usd = "/path/to/metahuman_driver.usd" add_reference_to_stage(usd_path=metahuman_usd, prim_path="/World/Driver") self.face_keypoints = [ "left_eye", "right_eye", "nose", "mouth", "left_eyebrow", "right_eyebrow" ] def generate_random_pose(self): """生成随机姿态""" import random pose_params = { "head_rotation": random.uniform(-30, 30), "eye_gaze": { "horizontal": random.uniform(-20, 20), "vertical": random.uniform(-15, 15) }, "blink_state": random.choice(["open", "closed", "partial"]), "mouth_state": random.choice(["neutral", "yawn", "talk"]), "fatigue_level": random.choice(["alert", "drowsy", "fatigued"]) } return pose_params def randomize_scene(self): """场景随机化""" light_intensity = rep.random.uniform(100, 1000) light_color = rep.random.choice([ (1.0, 1.0, 1.0), (0.8, 0.8, 1.0), (0.6, 0.5, 0.4), (0.1, 0.1, 0.2), ]) occlusion = rep.random.choice([ "none", "sunglasses", "mask", "both" ], weights=[0.7, 0.15, 0.1, 0.05]) return { "light_intensity": light_intensity, "light_color": light_color, "occlusion": occlusion } def capture_frame(self, pose_params: dict, scene_params: dict) -> dict: """ 捕获单帧数据 Returns: data: { 'rgb': np.ndarray, 'depth': np.ndarray, 'semantic': np.ndarray, 'annotations': dict } """ self._apply_pose(pose_params) self._apply_scene_randomization(scene_params) self.world.step(render=True) rgb_data = rep.ophirobject.get_rgb() depth_data = rep.ophirobject.get_depth() semantic_data = rep.ophirobject.get_semantic_segmentation() annotations = self._auto_annotate(pose_params) return { 'rgb': rgb_data, 'depth': depth_data, 'semantic': semantic_data, 'annotations': annotations } def _apply_pose(self, pose_params: dict): """应用姿态参数""" head_prim = self.world.stage.GetPrimAtPath("/World/Driver/Head") def _apply_scene_randomization(self, scene_params: dict): """应用场景随机化""" light_prim = self.world.stage.GetPrimAtPath("/World/MainLight") def _auto_annotate(self, pose_params: dict) -> dict: """自动标注""" return { 'gaze_direction': pose_params['eye_gaze'], 'blink_state': pose_params['blink_state'], 'fatigue_level': pose_params['fatigue_level'], 'head_pose': pose_params['head_rotation'], 'keypoints_2d': self._get_2d_keypoints(), 'keypoints_3d': self._get_3d_keypoints() } def _get_2d_keypoints(self) -> np.ndarray: """获取2D关键点""" pass def _get_3d_keypoints(self) -> np.ndarray: """获取3D关键点""" pass
def generate_dms_dataset( num_samples: int = 10000, output_dir: str = "/path/to/output" ): """ 生成DMS训练数据集 Args: num_samples: 样本数量 output_dir: 输出目录 """ generator = DMSSyntheticDataGenerator() generator.setup_cabin() generator.setup_driver() for i in range(num_samples): pose = generator.generate_random_pose() scene = generator.randomize_scene() data = generator.capture_frame(pose, scene) save_sample(output_dir, i, data) print(f"生成完成: {num_samples} 样本")
def save_sample(output_dir: str, index: int, data: dict): """保存单样本""" import cv2 import json rgb_path = f"{output_dir}/images/{index:06d}.jpg" cv2.imwrite(rgb_path, data['rgb']) anno_path = f"{output_dir}/annotations/{index:06d}.json" with open(anno_path, 'w') as f: json.dump(data['annotations'], f)
if __name__ == "__main__": generate_dms_dataset(num_samples=10000) simulation_app.close()
|