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 272 273 274 275 276 277 278
| """ NVIDIA Isaac Sim座舱数据合成流程
功能: 1. 场景加载 2. 角色生成(Metahuman) 3. 动作编排 4. 传感器仿真 5. 数据导出(RGB + 标注) """
from omni.isaac.kit import SimulationApp simulation_app = SimulationApp({"headless": True})
import omni from omni.isaac.core import World from omni.isaac.core.robots import Robot from omni.isaac.core.utils.stage import add_reference_to_stage from omni.isaac.sensor import Camera from pxr import UsdGeom, UsdLux, Gf import numpy as np import os
class CabinDataSynthesizer: """ 座舱数据合成器 生成数据类型: - RGB图像 - 深度图 - 语义分割 - 3D关键点 - 姿态标签 """ def __init__(self, cabin_usd: str, output_dir: str): """ 初始化 Args: cabin_usd: 座舱USD场景路径 output_dir: 输出目录 """ self.world = World(stage_units_in_meters=1.0) self.output_dir = output_dir add_reference_to_stage(cabin_usd, "/World/Cabin") self.camera = Camera( prim_path="/World/Camera", position=np.array([0.5, 0.0, 1.2]), frequency=30, resolution=(1920, 1080), orientation=np.array([0, 0, 0]) ) self._setup_lighting() print("[INFO] 座舱数据合成器初始化完成") def _setup_lighting(self): """设置光照""" world_prim = self.world.stage.GetPrimAtPath("/World") light_path = "/World/EnvironmentLight" light = UsdLux.DomeLight.Define(self.world.stage, light_path) light.CreateIntensityAttr(1000) sun_path = "/World/Sun" sun = UsdLux.DistantLight.Define(self.world.stage, sun_path) sun.CreateIntensityAttr(500) sun.CreateAngleAttr(1.0) def add_metahuman(self, position: np.ndarray, gender: str = "male", age: str = "adult"): """ 添加Metahuman角色 Args: position: 位置 (x, y, z) gender: 性别 age: 年龄类别 """ metahuman_path = f"/Isaac/Characters/Metahuman/{gender}_{age}" char_prim = add_reference_to_stage( f"{metahuman_path}/character.usd", f"/World/Character_{len(self.world.scene.get_prims())}" ) char_prim.GetAttribute("xformOp:translate").Set(Gf.Vec3d(*position)) print(f"[INFO] 添加角色: {gender}_{age} at {position}") def set_driver_state(self, state: str): """ 设置驾驶员状态 Args: state: 'normal' | 'fatigue' | 'distraction' | 'drowsy' """ if state == 'fatigue': self._set_eye_openness(0.3) self._set_head_pose(0.1, 0, 0.1) elif state == 'distraction': self._set_gaze_direction(0.3, 0.2) print(f"[INFO] 设置驾驶员状态: {state}") def capture_frame(self) -> Dict: """ 捕获一帧数据 Returns: data: { 'rgb': np.ndarray, 'depth': np.ndarray, 'segmentation': np.ndarray, 'keypoints_3d': np.ndarray, 'labels': Dict } """ self.world.step(render=True) rgb = self.camera.get_rgb() depth = self.camera.get_depth() segmentation = self.camera.get_segmentation() keypoints_3d = self._get_keypoints_ground_truth() labels = { 'fatigue': self.current_fatigue_level, 'distraction': self.current_distraction_type, 'gaze_direction': self.current_gaze, 'head_pose': self.current_head_pose } return { 'rgb': rgb, 'depth': depth, 'segmentation': segmentation, 'keypoints_3d': keypoints_3d, 'labels': labels } def _get_keypoints_ground_truth(self) -> np.ndarray: """ 获取3D关键点真值 Returns: keypoints: (17, 3) 关键点坐标 """ keypoints = np.array([ [0.0, 0.0, 1.0], [0.0, 0.1, 0.9], [-0.2, 0.0, 0.7], [0.2, 0.0, 0.7], ]) return keypoints def generate_dataset( self, num_frames: int, scenarios: List[str], variation_config: Dict ): """ 生成数据集 Args: num_frames: 总帧数 scenarios: 场景列表 variation_config: 变化配置 """ for i in range(num_frames): scenario = np.random.choice(scenarios) self._apply_variations(variation_config) data = self.capture_frame() self._save_frame(data, i, scenario) if i % 100 == 0: print(f"[INFO] 已生成 {i}/{num_frames} 帧") print(f"[INFO] 数据集生成完成: {num_frames} 帧") def _apply_variations(self, config: Dict): """应用变化""" if 'lighting' in config: intensity = np.random.uniform( config['lighting']['min'], config['lighting']['max'] ) self._set_light_intensity(intensity) if 'character' in config: gender = np.random.choice(['male', 'female']) age = np.random.choice(['child', 'adult', 'elderly']) if 'pose' in config: head_rotation = np.random.uniform( config['pose']['head_rotation_min'], config['pose']['head_rotation_max'] ) self._set_head_rotation(head_rotation) def _save_frame(self, data: Dict, frame_id: int, scenario: str): """保存帧数据""" frame_dir = os.path.join(self.output_dir, f"frame_{frame_id:06d}") os.makedirs(frame_dir, exist_ok=True) from PIL import Image Image.fromarray(data['rgb']).save(os.path.join(frame_dir, "rgb.png")) np.save(os.path.join(frame_dir, "depth.npy"), data['depth']) import json with open(os.path.join(frame_dir, "labels.json"), 'w') as f: json.dump(data['labels'], f)
if __name__ == "__main__": synthesizer = CabinDataSynthesizer( cabin_usd="/Assets/vehicle_interior.usd", output_dir="/Output/CabinDataset" ) synthesizer.add_metahuman(position=[0.0, 0.0, 0.0], gender="male", age="adult") variation_config = { 'lighting': {'min': 100, 'max': 2000}, 'pose': {'head_rotation_min': -30, 'head_rotation_max': 30} } synthesizer.generate_dataset( num_frames=10000, scenarios=['normal', 'fatigue', 'distraction'], variation_config=variation_config ) simulation_app.close()
|