File size: 10,505 Bytes
cff3150 | 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 | 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
"""
# Create folder with same name as HDF5 file (without extension)
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)
# Create the folder if it doesn't exist
os.makedirs(folder_path, exist_ok=True)
# Define output paths
hdf_output_path = os.path.join(folder_path, f"{folder_name}_replay.hdf5")
video_dir = folder_path
# Metrics path
metrics_output_path = os.path.join(folder_path, f"qa_metrics.json")
# Move original HDF5 file to the new folder
new_hdf_input_path = os.path.join(folder_path, base_name)
if hdf_input_path != new_hdf_input_path: # Avoid copying if already in target folder
os.rename(hdf_input_path, new_hdf_input_path)
hdf_input_path = new_hdf_input_path
# Define resolution for consistency
RESOLUTION_DEFAULT = 560
RESOLUTION_WRIST = 240
# This flag is needed to run data playback wrapper
gm.ENABLE_TRANSITION_RULES = False
# Define external camera positions and orientations
external_camera_poses = [
# Camera 1
[[-0.4, 0, 2.0], [0.2706, -0.2706, -0.6533, 0.6533]],
# # Camera 2
# [[-0.2, 0.6, 2.0], [-0.1930, 0.4163, 0.8062, -0.3734]],
# # Camera 3
# [[-0.2, -0.6, 2.0], [0.4164, -0.1929, -0.3737, 0.8060]]
]
# Robot sensor configuration
robot_sensor_config = {
"VisionSensor": {
"modalities": ["rgb"],
"sensor_kwargs": {
"image_height": RESOLUTION_WRIST,
"image_width": RESOLUTION_WRIST,
},
},
}
# Generate external sensors config automatically
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",
})
# Replace normal head camera with custom config
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",
})
# Create the environment
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,
)
# Optimize rendering for faster speeds
og.sim.add_callback_on_play("optimize_rendering", optimize_sim_settings)
if RUN_QA:
# Add QA metrics
metric_kwargs = dict(
step_dt=1/30,
vel_threshold=0.001,
color_arms=False, # For ghost hand
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()
# Create a list to store video writers and RGB keys
video_writers = []
video_rgb_keys = []
# Create video writer for robot cameras
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)
# Create video writers for external cameras
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")
# Playback the dataset with all video writers
# We avoid calling playback_dataset and call playback_episode individually in order to manually
# aggregate per-episode metrics
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
# Close all video writers
for container, stream in video_writers:
# Flush any remaining packets
for packet in stream.encode():
container.mux(packet)
# Close the container
container.close()
env.save_data()
# Save metrics
with open(metrics_output_path, "w+") as f:
json.dump(metrics, f, cls=TorchEncoder, indent=4)
# Always clear the environment to free resources
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):
# Process all HDF5 files in the directory (non-recursively)
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:
# Process individual files specified
hdf_files = args.files
else:
parser.print_help()
print("\nError: Either --dir or --files must be specified", file=sys.stderr)
return
# Process each file
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()
|