| from omnigibson.envs import DataPlaybackWrapper |
| from omnigibson.utils.config_utils import TorchEncoder |
| import torch as th |
| import os |
| import omnigibson as og |
| from omnigibson.macros import gm |
| import argparse |
| import sys |
| import json |
| from gello.robots.sim_robot.og_teleop_utils import optimize_sim_settings |
| from gello.utils.qa_utils import * |
| from gello.utils.b1k_utils import ALL_QA_METRICS, COMMON_QA_METRICS, TASK_QA_METRICS |
| import inspect |
|
|
| RUN_QA = True |
|
|
| gm.RENDER_VIEWER_CAMERA = False |
| gm.DEFAULT_VIEWER_WIDTH = 128 |
| gm.DEFAULT_VIEWER_HEIGHT = 128 |
|
|
|
|
| def extract_arg_names(func): |
| return list(inspect.signature(func).parameters.keys()) |
|
|
|
|
| def replay_hdf5_file(hdf_input_path): |
| """ |
| Replays a single HDF5 file and saves videos to a new folder |
| |
| Args: |
| hdf_input_path: Path to the HDF5 file to replay |
| """ |
| |
| base_name = os.path.basename(hdf_input_path) |
| folder_name = os.path.splitext(base_name)[0] |
| folder_path = os.path.join(os.path.dirname(hdf_input_path), folder_name) |
| |
| |
| os.makedirs(folder_path, exist_ok=True) |
| |
| |
| hdf_output_path = os.path.join(folder_path, f"{folder_name}_replay.hdf5") |
| video_dir = folder_path |
|
|
| |
| metrics_output_path = os.path.join(folder_path, f"qa_metrics.json") |
|
|
| |
| new_hdf_input_path = os.path.join(folder_path, base_name) |
| if hdf_input_path != new_hdf_input_path: |
| os.rename(hdf_input_path, new_hdf_input_path) |
| hdf_input_path = new_hdf_input_path |
| |
| |
| RESOLUTION_DEFAULT = 560 |
| RESOLUTION_WRIST = 240 |
| |
| |
| gm.ENABLE_TRANSITION_RULES = False |
| |
| |
| external_camera_poses = [ |
| |
| [[-0.4, 0, 2.0], [0.2706, -0.2706, -0.6533, 0.6533]], |
| |
| |
| |
| |
| ] |
| |
| |
| robot_sensor_config = { |
| "VisionSensor": { |
| "modalities": ["rgb"], |
| "sensor_kwargs": { |
| "image_height": RESOLUTION_WRIST, |
| "image_width": RESOLUTION_WRIST, |
| }, |
| }, |
| } |
| |
| |
| external_sensors_config = [] |
| for i, (position, orientation) in enumerate(external_camera_poses): |
| external_sensors_config.append({ |
| "sensor_type": "VisionSensor", |
| "name": f"external_sensor{i}", |
| "relative_prim_path": f"/controllable__r1pro__robot_r1/base_link/external_sensor{i}", |
| "modalities": ["rgb"], |
| "sensor_kwargs": { |
| "image_height": RESOLUTION_DEFAULT, |
| "image_width": RESOLUTION_DEFAULT, |
| "horizontal_aperture": 40.0, |
| }, |
| "position": th.tensor(position, dtype=th.float32), |
| "orientation": th.tensor(orientation, dtype=th.float32), |
| "pose_frame": "parent", |
| }) |
|
|
| |
| idx = len(external_sensors_config) |
| external_sensors_config.append({ |
| "sensor_type": "VisionSensor", |
| "name": f"external_sensor{idx}", |
| "relative_prim_path": f"/controllable__r1pro__robot_r1/zed_link/external_sensor{idx}", |
| "modalities": ["rgb", "seg_instance_id"], |
| "sensor_kwargs": { |
| "image_height": RESOLUTION_DEFAULT, |
| "image_width": RESOLUTION_DEFAULT, |
| "horizontal_aperture": 40.0, |
| }, |
| "position": th.tensor([0.06, 0.0, 0.01], dtype=th.float32), |
| "orientation": th.tensor([-1.0, 0.0, 0.0, 0.0], dtype=th.float32), |
| "pose_frame": "parent", |
| }) |
|
|
| |
| additional_wrapper_configs = [] |
| if RUN_QA: |
| additional_wrapper_configs.append({ |
| "type": "MetricsWrapper", |
| }) |
| env = DataPlaybackWrapper.create_from_hdf5( |
| input_path=hdf_input_path, |
| output_path=hdf_output_path, |
| robot_obs_modalities=["rgb"], |
| robot_sensor_config=robot_sensor_config, |
| external_sensors_config=external_sensors_config, |
| exclude_sensor_names=["zed"], |
| n_render_iterations=1, |
| only_successes=False, |
| additional_wrapper_configs=additional_wrapper_configs, |
| include_task=True, |
| include_task_obs=False, |
| include_robot_control=False, |
| include_contacts=True, |
| ) |
|
|
| |
| og.sim.add_callback_on_play("optimize_rendering", optimize_sim_settings) |
|
|
| if RUN_QA: |
| |
| metric_kwargs = dict( |
| step_dt=1/30, |
| vel_threshold=0.001, |
| color_arms=False, |
| default_color=(0.8235, 0.8235, 1.0000), |
| head_camera=env.external_sensors[f"external_sensor{len(env.external_sensors) - 1}"], |
| head_camera_link_name="torso_link4", |
| navigation_window=3.0, |
| translation_threshold=0.1, |
| rotation_threshold=0.05, |
| camera_tilt_threshold=0.4, |
| gripper_link_paths={ |
| "left": |
| set([ |
| '/World/scene_0/controllable__r1pro__robot_r1/left_realsense_link/visuals', |
| '/World/scene_0/controllable__r1pro__robot_r1/left_gripper_link/visuals', |
| '/World/scene_0/controllable__r1pro__robot_r1/left_gripper_finger_link1/visuals', |
| '/World/scene_0/controllable__r1pro__robot_r1/left_gripper_finger_link2/visuals' |
| ]), |
| "right": |
| set([ |
| '/World/scene_0/controllable__r1pro__robot_r1/right_realsense_link/visuals', |
| '/World/scene_0/controllable__r1pro__robot_r1/right_gripper_link/visuals', |
| '/World/scene_0/controllable__r1pro__robot_r1/right_gripper_finger_link1/visuals', |
| '/World/scene_0/controllable__r1pro__robot_r1/right_gripper_finger_link2/visuals' |
| ]) |
| }, |
| ) |
| active_metrics_info = {metric_name: ALL_QA_METRICS[metric_name] for metric_name in COMMON_QA_METRICS} |
| for metric_name, metric_info in active_metrics_info.items(): |
| create_fcn = metric_info["cls"] if metric_info["init"] is None else metric_info["init"] |
| init_kwargs = {arg: metric_kwargs[arg] for arg in extract_arg_names(create_fcn)} |
| metric = create_fcn(**init_kwargs) |
| env.add_metric(name=metric_name, metric=metric) |
| env.reset() |
|
|
| |
| video_writers = [] |
| video_rgb_keys = [] |
| |
| |
| robot_camera_names = ['robot_r1::robot_r1:left_realsense_link:Camera:0::rgb', |
| 'robot_r1::robot_r1:right_realsense_link:Camera:0::rgb'] |
| for robot_camera_name in robot_camera_names: |
| video_writers.append(env.create_video_writer( |
| fpath=f"{video_dir}/{robot_camera_name}.mp4", |
| resolution=(RESOLUTION_WRIST, RESOLUTION_WRIST), |
| )) |
| video_rgb_keys.append(robot_camera_name) |
| |
| for i in range(len(external_sensors_config)): |
| camera_name = f"external_sensor{i}" |
| video_writers.append(env.create_pyav_writer( |
| fpath=f"{video_dir}/{camera_name}.mp4", |
| resolution=(RESOLUTION_DEFAULT, RESOLUTION_DEFAULT), |
| )) |
| video_rgb_keys.append(f"external::{camera_name}::rgb") |
| |
| |
| |
| metrics = dict() |
| for episode_id in range(env.input_hdf5["data"].attrs["n_episodes"]): |
| env.playback_episode( |
| episode_id=episode_id, |
| record_data=False, |
| video_writers=video_writers, |
| video_keys=video_rgb_keys, |
| ) |
| if RUN_QA: |
| episode_metrics = env.aggregate_metrics(flatten=True) |
| for k, v in episode_metrics.items(): |
| print(f"Metric [{k}]: {v}") |
| metrics[f"episode_{episode_id}"] = episode_metrics |
| |
| |
| for container, stream in video_writers: |
| |
| for packet in stream.encode(): |
| container.mux(packet) |
| |
| container.close() |
|
|
| env.save_data() |
|
|
| |
| with open(metrics_output_path, "w+") as f: |
| json.dump(metrics, f, cls=TorchEncoder, indent=4) |
|
|
| |
| og.clear() |
| |
| print(f"Successfully processed {hdf_input_path}") |
|
|
|
|
| def main(): |
| parser = argparse.ArgumentParser(description="Replay HDF5 files and save videos") |
| parser.add_argument("--dir", help="Directory containing HDF5 files to process") |
| parser.add_argument("--files", nargs="*", help="Individual HDF5 file(s) to process") |
| |
| args = parser.parse_args() |
| |
| if args.dir and os.path.isdir(args.dir): |
| |
| hdf_files = [os.path.join(args.dir, f) for f in os.listdir(args.dir) |
| if f.lower().endswith('.hdf5') and os.path.isfile(os.path.join(args.dir, f))] |
| |
| if not hdf_files: |
| print(f"No HDF5 files found in directory: {args.dir}") |
| else: |
| print(f"Found {len(hdf_files)} HDF5 files to process") |
| elif args.files: |
| |
| hdf_files = args.files |
| else: |
| parser.print_help() |
| print("\nError: Either --dir or --files must be specified", file=sys.stderr) |
| return |
| |
| |
| for hdf_file in hdf_files: |
| if not os.path.exists(hdf_file): |
| print(f"Error: File {hdf_file} does not exist", file=sys.stderr) |
| continue |
| |
| replay_hdf5_file(hdf_file) |
|
|
| og.shutdown() |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|