Download examples/read_episode.py from JGuo666/RoboInF-4K: direct link, hf CLI and curl.
- Browser
- Download file 3.4 kB
-
https://huggingface.co/datasets/JGuo666/RoboInF-4K/resolve/main/examples/read_episode.py
- Command line
-
hf download hf://datasets/JGuo666/RoboInF-4K/examples/read_episode.py
-
curl -L -o read_episode.py https://huggingface.co/datasets/JGuo666/RoboInF-4K/resolve/main/examples/read_episode.py
3.4 kB
| """Read one labeled demonstration from a local RoboInF-4K snapshot.""" | |
| import argparse | |
| import gzip | |
| import io | |
| import json | |
| import tarfile | |
| from pathlib import Path | |
| import h5py | |
| import numpy as np | |
| CAMERAS = ("head", "front", "left_wrist", "right_wrist") | |
| def read_episode(root, split, task=None): | |
| with (root / "manifests" / f"{split}.jsonl").open() as manifest: | |
| for line in manifest: | |
| row = json.loads(line) | |
| if task is None or row["task"] == task: | |
| break | |
| else: | |
| raise ValueError(f"No episode found for {task!r} in {split}") | |
| with tarfile.open(root / row["shard"], "r:") as shard: | |
| episode_id = row["episode_id"] | |
| with shard.extractfile(f"{episode_id}/trajectory.hdf5") as entry: | |
| with h5py.File(io.BytesIO(entry.read()), "r") as h5: | |
| states = h5["state/left_arm_joint_states"][:] | |
| actions = h5["action/left_arm_joint_states"][:] | |
| times = h5["time/time_sec"][:] | |
| assert np.array_equal(h5["time/action_frame_index"][:], np.arange(1, len(times))) | |
| with shard.extractfile(f"{episode_id}/annotations.json.gz") as entry: | |
| annotations = json.loads(gzip.decompress(entry.read())) | |
| return row, states, actions, times, annotations | |
| def read_rgb(root, row, camera, frame): | |
| from PIL import Image | |
| if camera not in CAMERAS: | |
| raise ValueError(f"Unknown camera: {camera}") | |
| n = row["frame_count"] | |
| frame = n + frame if frame < 0 else frame | |
| if not 0 <= frame < n: | |
| raise IndexError(f"Frame must index {n} samples") | |
| shard_path = Path(row["shard"]).relative_to("data") | |
| member = f"{row['episode_id']}/{camera}" | |
| if frame == n - 1: | |
| with tarfile.open(root / "terminal_rgb" / shard_path, "r:") as tar: | |
| with tar.extractfile(member + ".png") as src: | |
| payload = src.read() | |
| else: | |
| with tarfile.open(root / "jpeg" / shard_path, "r:") as tar: | |
| with tar.extractfile(member + ".offsets.npy") as src: | |
| offsets = np.load(io.BytesIO(src.read()), allow_pickle=False) | |
| start, end = map(int, offsets[frame:frame + 2]) | |
| with tar.extractfile(member + ".mjpg") as src: | |
| src.seek(start) | |
| payload = src.read(end - start) | |
| with Image.open(io.BytesIO(payload)) as img: | |
| return np.asarray(img.convert("RGB")) | |
| if __name__ == "__main__": | |
| parser = argparse.ArgumentParser(description=__doc__) | |
| parser.add_argument("--root", type=Path, default=Path(".")) | |
| parser.add_argument("--split", choices=("train", "val"), default="train") | |
| parser.add_argument("--task") | |
| parser.add_argument("--camera", choices=CAMERAS, help="Read RGB for this camera") | |
| parser.add_argument("--frame", type=int, default=0, help="Camera sample index; -1 is the terminal frame") | |
| args = parser.parse_args() | |
| row, states, actions, times, annotations = read_episode(args.root, args.split, args.task) | |
| print(row["episode_uid"], row["instruction"], sep="\n") | |
| print(f"{len(times)} samples, {len(actions)} targets; left-arm state shape {states.shape}") | |
| print(f"{len(annotations['point_ids'])} tracked keypoints") | |
| if args.camera: | |
| rgb = read_rgb(args.root, row, args.camera, args.frame) | |
| print(f"{args.camera} at t={times[args.frame]:.4f}s: {rgb.shape}, {rgb.dtype}") | |