"""Per-session preview images for the v2 dataset README. Iterates both source/v1/ and source/v2/ -- v2 includes all v1 sessions plus the new collection batches -- and writes a side-by-side composite of the GelSight stitched panorama (left) and the D555 overhead with crop-grid overlay (right) into previews/v2/.png. Incomplete sessions (missing stitched output or rgb_grid_debug.png) are skipped. Run after the v1 and v2 croppers (which write rgb_grid_debug.png): uv run python scripts/v2/03_make_previews.py """ from __future__ import annotations from pathlib import Path import cv2 import numpy as np REPO_ROOT = Path(__file__).resolve().parents[2] SOURCE_ROOT = REPO_ROOT / "source" SOURCE_VERSIONS = ("v1", "v2") PREVIEWS_DIR = REPO_ROOT / "previews" / "v2" PREVIEW_HEIGHT = 400 GAP_PX = 16 BG = (255, 255, 255) def _resize_to_height(img: np.ndarray, h: int) -> np.ndarray: w = int(round(img.shape[1] * h / img.shape[0])) return cv2.resize(img, (w, h), interpolation=cv2.INTER_AREA) def make_preview(session_dir: Path) -> bool: gel_path = session_dir / "gelsight_stitched.png" rgb_path = session_dir / "rgb_grid_debug.png" if not gel_path.exists() or not rgb_path.exists(): print(f" skip {session_dir.name}: missing {gel_path.name if not gel_path.exists() else rgb_path.name}") return False gel = cv2.imread(str(gel_path)) rgb = cv2.imread(str(rgb_path)) gel_r = _resize_to_height(gel, PREVIEW_HEIGHT) rgb_r = _resize_to_height(rgb, PREVIEW_HEIGHT) total_w = gel_r.shape[1] + GAP_PX + rgb_r.shape[1] canvas = np.full((PREVIEW_HEIGHT, total_w, 3), BG, dtype=np.uint8) canvas[:, : gel_r.shape[1]] = gel_r canvas[:, gel_r.shape[1] + GAP_PX :] = rgb_r out_path = PREVIEWS_DIR / f"{session_dir.name}.png" cv2.imwrite(str(out_path), canvas) print(f" {out_path.name}: {canvas.shape[1]}x{canvas.shape[0]}") return True def main() -> None: PREVIEWS_DIR.mkdir(parents=True, exist_ok=True) written = 0 for version in SOURCE_VERSIONS: src = SOURCE_ROOT / version if not src.exists(): continue sessions = sorted(p for p in src.iterdir() if p.is_dir()) print(f"\n== {version} ({len(sessions)} sessions) -> {PREVIEWS_DIR} ==") for sess in sessions: if make_preview(sess): written += 1 print(f"\nwrote {written} previews to {PREVIEWS_DIR}") if __name__ == "__main__": main()