File size: 2,081 Bytes
25450de | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | """Full-resolution alignment crops of finished renders: real frame (source video) + mask contour (mask.mp4), cropped to the robot bbox.
Picks the fastest and a slow frame per episode. -> scratch/align_<emb>_<obj>_<seq>_<cam>_f<v>_<speed>.jpg"""
import sys, json
from pathlib import Path
import cv2, numpy as np
sys.path.insert(0, str(Path(__file__).resolve().parent))
from hd_data import Episode, ROOT
out = ROOT / "scratch"
def frame_at(path, v):
cap = cv2.VideoCapture(str(path)); cap.set(cv2.CAP_PROP_POS_FRAMES, v); ok, f = cap.read(); cap.release(); return f
for rel in sys.argv[1:]:
ep = Episode(rel); d = ROOT / "out" / rel
st = np.load(d / "robot_state.npz", allow_pickle=True); q = st["qpos"]
sp = np.r_[0, np.linalg.norm(np.diff(q[:, :6], axis=0), axis=1)]
n = len(q); cand = np.argsort(-sp[10:n - 10]) + 10
picks = {"fast": int(cand[0]), "slow": int(np.argsort(sp[n // 3: 2 * n // 3])[0] + n // 3)}
for cam in ("25452062", "25452066"):
for tag, v in picks.items():
real = frame_at(ep.video_path(cam), v)
m = frame_at(d / cam / "mask.mp4", v)
if real is None or m is None: continue
mask = m[:, :, 0] > 127
ys, xs = np.nonzero(mask)
if len(xs) == 0: continue
cs, _ = cv2.findContours(mask.astype(np.uint8), cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)
cv2.drawContours(real, cs, -1, (0, 255, 255), 2)
x0, x1 = max(xs.min() - 60, 0), min(xs.max() + 60, real.shape[1]); y0, y1 = max(ys.min() - 60, 0), min(ys.max() + 60, real.shape[0])
crop = real[y0:y1, x0:x1]
s = min(1.0, 1100 / crop.shape[1], 900 / crop.shape[0])
crop = cv2.resize(crop, None, fx=s, fy=s, interpolation=cv2.INTER_AREA)
cv2.putText(crop, f"{rel} {cam} f{v} {tag} speed={sp[v]:.3f} rad/frame scale={s:.2f}", (6, 22), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 0, 0), 2)
p = out / f"align_{rel.replace('/', '_')}_{cam}_f{v}_{tag}.jpg"
cv2.imwrite(str(p), crop, [cv2.IMWRITE_JPEG_QUALITY, 90]); print(p)
|