| """Per-session preview images for the dataset README. |
| |
| For each session, composites the GelSight stitched panorama (left) and the |
| D555 overhead with crop-grid overlay (right) into a single side-by-side PNG |
| at modest height so the README renders fast on the Hub. |
| |
| Run after 01_crop_d555.py (which writes rgb_grid_debug.png): |
| uv run python scripts/v1/03_make_previews.py |
| |
| Outputs previews/v1/<session>.png. |
| """ |
|
|
| from __future__ import annotations |
|
|
| from pathlib import Path |
|
|
| import cv2 |
| import numpy as np |
|
|
| REPO_ROOT = Path(__file__).resolve().parents[2] |
| SOURCE_DIR = REPO_ROOT / "source" / "v1" |
| PREVIEWS_DIR = REPO_ROOT / "previews" / "v1" |
|
|
| 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) -> None: |
| gel = cv2.imread(str(session_dir / "gelsight_stitched.png")) |
| rgb = cv2.imread(str(session_dir / "rgb_grid_debug.png")) |
| if gel is None or rgb is None: |
| raise FileNotFoundError(f"missing preview source in {session_dir}") |
|
|
| 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]}") |
|
|
|
|
| def main() -> None: |
| sessions = sorted(p for p in SOURCE_DIR.iterdir() if p.is_dir()) |
| if not sessions: |
| raise SystemExit(f"no sessions found in {SOURCE_DIR}") |
| PREVIEWS_DIR.mkdir(parents=True, exist_ok=True) |
| print(f"writing previews for {len(sessions)} sessions -> {PREVIEWS_DIR}") |
| for sess in sessions: |
| make_preview(sess) |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|