#!/usr/bin/env python3 """Compact first-frame grid for one subdir, exo view only (37149196_left).""" import subprocess, sys from io import BytesIO from pathlib import Path from PIL import Image, ImageDraw, ImageFont ROOT = Path("/home/jimin/droid/collected_datasets/rlwrld_datasets_jonghoon/ICLR") SUB = sys.argv[1] if len(sys.argv) > 1 else "white_spray" VIEW = sys.argv[2] if len(sys.argv) > 2 else "37149196_left" # exo NCOL = 6 TW, PAD, LBL = 320, 6, 20 def first_frame(mp4): out = subprocess.run(["ffmpeg", "-nostdin", "-loglevel", "error", "-i", str(mp4), "-frames:v", "1", "-f", "image2pipe", "-vcodec", "png", "pipe:1"], capture_output=True).stdout return Image.open(BytesIO(out)).convert("RGB") if out else None def font(s): p = "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf" return ImageFont.truetype(p, s) if Path(p).exists() else ImageFont.load_default() eps = sorted([d for d in (ROOT / SUB).iterdir() if d.is_dir() and d.name.isdigit()]) imgs, th = [], None for ep in eps: im = first_frame(ep / "recorded_videos" / f"{VIEW}.mp4") if im: w, h = im.size; nh = round(TW * h / w); im = im.resize((TW, nh)); th = th or nh imgs.append((ep.name, im)) th = th or 180 n = len(imgs); ncol = min(NCOL, n); nrow = (n + ncol - 1) // ncol cw, ch = TW, th + LBL W = ncol * cw + (ncol + 1) * PAD H = 30 + nrow * ch + (nrow + 1) * PAD cv = Image.new("RGB", (W, H), (24, 24, 28)); d = ImageDraw.Draw(cv) d.text((8, 7), f"{SUB} — exo ({VIEW}) — {n} episodes", fill=(235, 235, 240), font=font(16)) for i, (name, im) in enumerate(imgs): r, c = divmod(i, ncol) x = PAD + c * (cw + PAD); y = 30 + PAD + r * (ch + PAD) if im: cv.paste(im, (x, y)) else: d.rectangle([x, y, x + cw, y + th], fill=(50, 40, 40)); d.text((x + 8, y + th // 2), "missing", fill=(200, 120, 120), font=font(13)) d.text((x + 3, y + th + 3), name, fill=(255, 210, 90), font=font(14)) out = ROOT / f"first_frame_grid_{SUB}_exo.png" cv.save(out); print(f"saved {out} ({W}x{H}), {n} eps")