"""依頼 445: 並べの絵 (左から 実機・元 (look_393/render/A)・445 (render/all)). 机側. figs/desk_stages_445.png : 段階ごとに 1 コマ (ep03 の その段階の真ん中のコマ). 1 段 = 1 段階. 640×480 を半分に縮めて並べる figs/desk_stages_zoom_445.png : 同じコマの 腕のまわり (Blender のラベルの腕の外接の箱 + 30px) を 2 倍に figs/desk_ep03_f080_445.png : ep03 の 80 コマ (トップの絵と同じコマ) を そのままの大きさで + 腕のまわりを 3 倍に figs/top_445.png : 実機 (check_361/ep03/real/080) と top_ep03_f080_desk.png 使い方: python3 so101/sim/look_445/figs_445.py """ import json import sys from pathlib import Path import cv2 import numpy as np HERE = Path(__file__).resolve().parent SO101 = HERE.parent.parent sys.path.insert(0, str(HERE.parent / "look_388")) C361 = SO101 / "check_361" A = HERE.parent / "look_393" / "render" / "A" JA = {"HOME": "HOME", "ABOVE_BLOCK": "above block", "GRASP": "grasp", "LIFT": "lift", "ABOVE_BOX": "above box", "RELEASE": "release", "RETURN": "return"} def frames(ep): P = json.loads((HERE.parent / "look_388" / "poses" / "base" / ep / "poses.json").read_text()) return [(f["frame"], f["stage"]) for f in P["frames"]] def trio(ep, k): return [cv2.imread(str(p)) for p in (C361 / ep / "real" / f"{k:03d}_desk.png", A / ep / "blf" / f"{k:03d}_desk.png", HERE / "render" / "all" / ep / "blf" / f"{k:03d}_desk.png")] def arm_box(ep, k, pad=30): m = cv2.imread(str(C361 / ep / "lab" / f"{k:03d}_desk.png"), 0) ys, xs = np.nonzero(np.isin(m, (2, 3, 7))) if len(xs) == 0: return 0, 480, 0, 640 return max(ys.min() - pad, 0), min(ys.max() + pad, 480), max(xs.min() - pad, 0), min(xs.max() + pad, 640) def label(im, s): cv2.putText(im, s, (6, 22), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 0), 3, cv2.LINE_AA) cv2.putText(im, s, (6, 22), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 1, cv2.LINE_AA) return im def main(): (HERE / "figs").mkdir(exist_ok=True) ep = "ep03" fr = frames(ep) rows, zrows = [], [] for st in JA: ks = [k for k, s in fr if s == st] k = ks[len(ks) // 2] t = trio(ep, k) rows.append(np.hstack([label(cv2.resize(x, (320, 240), interpolation=cv2.INTER_AREA), f"{n} {JA[st]} f{k}") for x, n in zip(t, ("real", "base", "445"))])) y0, y1, x0, x1 = arm_box(ep, k) h = 300 z = [cv2.resize(x[y0:y1, x0:x1], (int((x1 - x0) * h / (y1 - y0)), h), interpolation=cv2.INTER_CUBIC) for x in t] z = np.hstack([label(a, f"{n} {JA[st]} f{k}") for a, n in zip(z, ("real", "base", "445"))]) zrows.append(z) cv2.imwrite(str(HERE / "figs" / "desk_stages_445.png"), np.vstack(rows)) W = max(z.shape[1] for z in zrows) cv2.imwrite(str(HERE / "figs" / "desk_stages_zoom_445.png"), np.vstack([cv2.copyMakeBorder(z, 0, 0, 0, W - z.shape[1], cv2.BORDER_CONSTANT, value=(255, 255, 255)) for z in zrows])) t = trio(ep, 80) full = np.hstack([label(x.copy(), n) for x, n in zip(t, ("real ep03 f080", "base (388+393 A)", "445"))]) y0, y1, x0, x1 = 0, 240, 190, 430 z = np.hstack([cv2.resize(x[y0:y1, x0:x1], None, fx=2.6667, fy=2.6667, interpolation=cv2.INTER_CUBIC) for x in t]) cv2.imwrite(str(HERE / "figs" / "desk_ep03_f080_445.png"), np.vstack([full, cv2.resize(z, (full.shape[1], int(z.shape[0] * full.shape[1] / z.shape[1])))])) tp = HERE / "top_ep03_f080_desk.png" if tp.exists(): cv2.imwrite(str(HERE / "figs" / "top_445.png"), np.hstack([label(cv2.imread(str(C361 / ep / "real" / "080_desk.png")), "real ep03 f080"), label(cv2.imread(str(tp)), "445 top candidate")])) print("figs done") if __name__ == "__main__": main()