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
| """ Isaac Sim DMS合成数据生成脚本 运行环境: Isaac Sim 5.0+ """ import omni from omni.isaac.kit import SimulationApp from omni.isaac.synthetic_utils import SyntheticDataHelper from omni.replicator.core import AnnotatorRegistry import numpy as np from PIL import Image import json import os
simulation_app = SimulationApp({ "headless": True, "width": 1920, "height": 1080 })
class DMSDataPipeline: """DMS数据生成管道""" def __init__(self, output_dir: str = "./dms_synthetic_data"): self.output_dir = output_dir self.sd_helper = SyntheticDataHelper() os.makedirs(output_dir, exist_ok=True) def setup_scene(self): """设置场景""" self._load_vehicle_interior() self._load_virtual_character() self._setup_camera() def _load_vehicle_interior(self): """加载车辆内饰""" from omni.isaac.core.utils.stage import add_reference_to_stage interior_path = "/path/to/vehicle_interior.usd" add_reference_to_stage(interior_path, "/World/Interior") def _load_virtual_character(self): """加载虚拟人物""" from omni.isaac.core.articulations import Articulation from omni.isaac.core.utils.stage import add_reference_to_stage character_path = "/path/to/driver_character.usd" add_reference_to_stage(character_path, "/World/Driver") def _setup_camera(self): """设置DMS摄像头""" from omni.isaac.sensor import Camera self.camera = Camera( prim_path="/World/DMS_Camera", position=np.array([0.5, -0.3, 1.2]), frequency=30, resolution=(1920, 1080), orientation=np.array([0, -30, 0]) ) def generate_frame(self, frame_id: int) -> dict: """ 生成单帧数据 Args: frame_id: 帧ID Returns: frame_data: 生成的数据 """ self._apply_randomization() self.camera.add_distance_to_image_plane_to_frame() self.camera.add_motion_vectors_to_frame() rgb_data = self.camera.get_rgb() depth_data = self.camera.get_depth() instance_seg = self.camera.get_instance_segmentation() keypoints_2d = self._get_keypoints_2d() keypoints_3d = self._get_keypoints_3d() gaze_vector = self._get_gaze_vector() frame_data = { "rgb": rgb_data, "depth": depth_data, "segmentation": instance_seg, "keypoints_2d": keypoints_2d, "keypoints_3d": keypoints_3d, "gaze_vector": gaze_vector } self._save_frame(frame_data, frame_id) return frame_data def _apply_randomization(self): """应用域随机化""" import random light_intensity = random.uniform(100, 5000) self._set_light_intensity(light_intensity) pose_type = random.choice(['neutral', 'looking_left', 'looking_right', 'yawning', 'blinking', 'distracted']) self._set_character_pose(pose_type) weather = random.choice(['clear', 'night', 'tunnel']) self._set_environment(weather) def _get_keypoints_2d(self) -> list: """获取2D关键点(自动标注)""" return [ {"name": "left_eye", "x": 960, "y": 400}, {"name": "right_eye", "x": 860, "y": 400}, {"name": "nose", "x": 910, "y": 480}, {"name": "mouth_left", "x": 880, "y": 560}, {"name": "mouth_right", "x": 940, "y": 560} ] def _get_keypoints_3d(self) -> list: """获取3D关键点(自动标注)""" return [ {"name": "head_center", "x": 0.5, "y": -0.3, "z": 1.5}, {"name": "left_eye", "x": 0.48, "y": -0.28, "z": 1.52}, {"name": "right_eye", "x": 0.52, "y": -0.28, "z": 1.52} ] def _get_gaze_vector(self) -> dict: """获取视线向量(自动标注)""" return { "pitch": -5.0, "yaw": 10.0, "origin": {"x": 0.5, "y": -0.29, "z": 1.52} } def _save_frame(self, frame_data: dict, frame_id: int): """保存帧数据""" frame_dir = os.path.join(self.output_dir, f"frame_{frame_id:06d}") os.makedirs(frame_dir, exist_ok=True) rgb_image = Image.fromarray(frame_data["rgb"]) rgb_image.save(os.path.join(frame_dir, "rgb.png")) np.save(os.path.join(frame_dir, "depth.npy"), frame_data["depth"]) annotations = { "keypoints_2d": frame_data["keypoints_2d"], "keypoints_3d": frame_data["keypoints_3d"], "gaze_vector": frame_data["gaze_vector"] } with open(os.path.join(frame_dir, "annotations.json"), 'w') as f: json.dump(annotations, f, indent=2) def generate_dataset(self, num_frames: int = 10000): """ 生成完整数据集 Args: num_frames: 生成帧数 """ print(f"开始生成 {num_frames} 帧DMS训练数据...") for i in range(num_frames): self.generate_frame(i) if (i + 1) % 100 == 0: print(f"已生成 {i+1}/{num_frames} 帧") print(f"数据生成完成!保存至: {self.output_dir}") self._generate_dataset_manifest(num_frames) def _generate_dataset_manifest(self, num_frames: int): """生成数据集说明文件""" manifest = { "name": "DMS_Synthetic_Dataset", "version": "1.0", "num_frames": num_frames, "resolution": [1920, 1080], "annotations": ["keypoints_2d", "keypoints_3d", "gaze_vector", "depth", "segmentation"], "randomization": ["lighting", "pose", "environment", "weather"], "license": "CC0" } with open(os.path.join(self.output_dir, "manifest.json"), 'w') as f: json.dump(manifest, f, indent=2)
if __name__ == "__main__": pipeline = DMSDataPipeline(output_dir="./dms_dataset") pipeline.setup_scene() pipeline.generate_dataset(num_frames=10000) simulation_app.close()
|