Download droid/scripts/debug_wrist_projection.py from zhicao/openpi: direct link, hf CLI and curl.
- Browser
- Download file 6.28 kB
-
https://huggingface.co/datasets/zhicao/openpi/resolve/main/droid/scripts/debug_wrist_projection.py
- Command line
-
hf download hf://datasets/zhicao/openpi/droid/scripts/debug_wrist_projection.py
-
curl -L -o debug_wrist_projection.py https://huggingface.co/datasets/zhicao/openpi/resolve/main/droid/scripts/debug_wrist_projection.py
6.28 kB
| #!/usr/bin/env python3 | |
| """Overlay wrist gripper projections (compare inv / direct extrinsic, K sources). | |
| Usage: | |
| cd openpi/droid | |
| ../.venv/bin/python scripts/debug_wrist_projection.py \\ | |
| --h5 /z/home/zhicao/droid_raw/.../trajectory.h5 \\ | |
| --out /tmp/wrist_proj_debug.jpg | |
| """ | |
| from __future__ import annotations | |
| import argparse | |
| import json | |
| import sys | |
| from pathlib import Path | |
| import cv2 | |
| import h5py | |
| import numpy as np | |
| _DROID = Path(__file__).resolve().parents[1] | |
| for p in (_DROID.parent / "third_party", _DROID.parent / "third_party" / "libero", _DROID): | |
| ps = str(p) | |
| if p.exists() and ps not in sys.path: | |
| sys.path.insert(0, ps) | |
| if str(_DROID) not in sys.path: | |
| sys.path.insert(0, str(_DROID)) | |
| from utils.camera_geometry import ( # noqa: E402 | |
| DROID_NATIVE_HW, | |
| GRIPPER_OFFSETS, | |
| pose6_to_matrix, | |
| project_gripper_mesh, | |
| project_gripper_mesh_wrist, | |
| scale_pixels_between_hw, | |
| ) | |
| from utils.karlp_calib import KarlpCalibStore, episode_id_from_metadata # noqa: E402 | |
| from utils.raw_episode_loader import _load_episode # noqa: E402 | |
| from utils.zed_intrinsics import default_K_native, default_dist_native, zed_model_for_camera_type # noqa: E402 | |
| def _project_manual( | |
| cart6: np.ndarray, | |
| ext6: np.ndarray, | |
| K: np.ndarray, | |
| *, | |
| use_inv: bool, | |
| ) -> np.ndarray: | |
| from scipy.spatial.transform import Rotation | |
| c = np.asarray(cart6, dtype=np.float64).reshape(-1) | |
| R_wg = Rotation.from_euler("xyz", c[3:6]).as_matrix() | |
| world = (R_wg @ GRIPPER_OFFSETS.T).T + c[:3] | |
| T = pose6_to_matrix(ext6) | |
| w2c = np.linalg.inv(T) if use_inv else T | |
| pix = [] | |
| for p in world: | |
| pc = (w2c @ np.concatenate([p, [1.0]]))[:3] | |
| u = K @ pc | |
| pix.append(u[:2] / u[2]) | |
| return np.asarray(pix, dtype=np.float64) | |
| def main() -> None: | |
| p = argparse.ArgumentParser() | |
| p.add_argument("--h5", type=str, required=True) | |
| p.add_argument("--t", type=int, default=0) | |
| p.add_argument("--size", type=int, default=448) | |
| p.add_argument("--karlp-calib-dir", type=str, default="") | |
| p.add_argument("--out", type=str, default="wrist_proj_debug.jpg") | |
| args = p.parse_args() | |
| h5_path = Path(args.h5).expanduser().resolve() | |
| meta_paths = sorted(h5_path.parent.glob("metadata_*.json")) | |
| if not meta_paths: | |
| raise SystemExit(f"No metadata_*.json next to {h5_path}") | |
| meta = json.loads(meta_paths[0].read_text(encoding="utf-8")) | |
| episode_id = episode_id_from_metadata(meta_paths[0], meta) | |
| karlp = None | |
| kdir = args.karlp_calib_dir.strip() | |
| if not kdir: | |
| default = _DROID / "calibration" / "karlp" | |
| if (default / "cam2base_extrinsics.json").is_file(): | |
| kdir = str(default) | |
| if kdir: | |
| karlp = KarlpCalibStore(kdir) | |
| ep = _load_episode(h5_path, 0, karlp=karlp, preset_episode_id=episode_id) | |
| if ep is None: | |
| raise SystemExit("Failed to load episode (KarlP filter or missing MP4)") | |
| t = int(np.clip(args.t, 0, len(ep.cartesian) - 1)) | |
| s = int(args.size) | |
| out_hw = (s, s) | |
| frame = cv2.resize(ep.wrist_images[t], (s, s)) | |
| vis = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR) | |
| model = zed_model_for_camera_type(ep.wrist_camera_type) | |
| K_zed = default_K_native(model) | |
| dist_zed = default_dist_native(model) | |
| K_karlp, dist_karlp = K_zed, dist_zed | |
| if karlp is not None: | |
| parsed = karlp.intrinsics_for_serial(ep.episode_id, ep.wrist_serial) | |
| if parsed is not None: | |
| K_karlp, dist_karlp = parsed | |
| cart = ep.cartesian[t] | |
| ext = ep.wrist_extrinsics[t] | |
| go = ep.wrist_gripper_offset[t] | |
| with h5py.File(h5_path, "r") as f: | |
| has_action = "action/robot_state/cartesian_position" in f | |
| cart_action = ( | |
| np.asarray(f["action/robot_state/cartesian_position"], dtype=np.float64)[t] | |
| if has_action | |
| else None | |
| ) | |
| layers: list[tuple[str, tuple[int, int, int], np.ndarray]] = [] | |
| pix_wrist, _ = project_gripper_mesh_wrist(cart, ext, go, K=K_karlp, dist=dist_karlp) | |
| layers.append( | |
| ( | |
| "wrist mesh (fixed)", | |
| (0, 255, 0), | |
| scale_pixels_between_hw(pix_wrist, DROID_NATIVE_HW, out_hw), | |
| ) | |
| ) | |
| for label, K, dist, use_inv in [ | |
| ("inv_no_offset", K_karlp, dist_karlp, True), | |
| ("direct_no_offset", K_karlp, dist_karlp, False), | |
| ]: | |
| pix, _ = project_gripper_mesh(cart, ext, K=K, dist=dist) | |
| pix_s = scale_pixels_between_hw(pix, DROID_NATIVE_HW, out_hw) | |
| layers.append((label, (0, 0, 255), pix_s)) | |
| manual = _project_manual(cart, ext, K, use_inv=use_inv) | |
| manual_s = scale_pixels_between_hw(manual, DROID_NATIVE_HW, out_hw) | |
| layers.append((f"{label}_manual", (255, 0, 0), manual_s)) | |
| if cart_action is not None: | |
| pix_a, _ = project_gripper_mesh(cart_action, ext, K=K_karlp, dist=dist_karlp) | |
| layers.append( | |
| ("action_cart", (0, 255, 0), scale_pixels_between_hw(pix_a, DROID_NATIVE_HW, out_hw)) | |
| ) | |
| y0 = 20 | |
| for label, color, pts in layers: | |
| cv2.putText(vis, label, (8, y0), cv2.FONT_HERSHEY_SIMPLEX, 0.45, color, 1, cv2.LINE_AA) | |
| y0 += 16 | |
| for i, (x, y) in enumerate(pts): | |
| if 0 <= x < s and 0 <= y < s: | |
| cv2.circle(vis, (int(round(x)), int(round(y))), 4, color, -1) | |
| if label.startswith("wrist+offset"): | |
| cv2.putText( | |
| vis, | |
| str(i), | |
| (int(round(x)) + 3, int(round(y)) - 3), | |
| cv2.FONT_HERSHEY_SIMPLEX, | |
| 0.35, | |
| (255, 255, 255), | |
| 1, | |
| cv2.LINE_AA, | |
| ) | |
| out_path = Path(args.out).expanduser().resolve() | |
| out_path.parent.mkdir(parents=True, exist_ok=True) | |
| cv2.imwrite(str(out_path), vis) | |
| print(f"Wrote {out_path}") | |
| print(f" episode_id={ep.episode_id} wrist_serial={ep.wrist_serial} t={t}") | |
| print(f" extrinsic_source={ep.extrinsic_source.get('wrist')}") | |
| print(" Green=wrist mesh (inv(c2w)@T_eef@mesh, same as exterior; preprocess uses this).") | |
| print(" Red/blue=legacy debug paths (inv vs direct extrinsic, no mesh).") | |
| if __name__ == "__main__": | |
| main() | |