"""依頼 353 の校正: check_353//ep*_colors.csv (simreplay_353 の ラベルごとの 実機・シムの色の中央値) から, 実機 ÷ シム (B・G・R ごと. コマの中央値) を今の値に掛けて appearance_353.json の値を直す案を出す (--apply で書き込む. 出どころも書く). - カメラの倍率 (露出・ホワイトバランス): 机 (table) の比. 回ごと (5 本に無い回は 同じカメラのほかの回の平均. pos1b は光の倍率で割る) - 色: 机側 = 白い部材 (arm)・サーボ (servo)・箱の底・壁. 手首 = 爪 (jaw). 倍率の直しの分は除く - ブロック (--block): 手首のブロック (level 3 で使う) 読むだけ (--apply のときだけ appearance_353.json を書く). 使い方: python so101/sim/cal_353.py --tag lv1 [--apply] [--what gain,color] [--block] """ import argparse import json import sys from pathlib import Path import numpy as np HERE = Path(__file__).resolve().parent sys.path.insert(0, str(HERE)) import appearance_353 as A # noqa: E402 OUT = HERE.parent / "check_353" BGR = ("B", "G", "R") def ratios(c, cam, label, session=None): g = c[(c["cam"] == cam) & (c["label"] == label)] if session is not None: g = g[g["session"] == session] if len(g) < 5: return None return np.array([float(g[f"real_{k}"].median() / max(1.0, g[f"sim_{k}"].median())) for k in BGR]) def main(): import pandas as pd ap = argparse.ArgumentParser() ap.add_argument("--tag", required=True) ap.add_argument("--what", default="gain,color") ap.add_argument("--block", action="store_true") ap.add_argument("--apply", action="store_true") a = ap.parse_args() what = a.what.split(",") c = pd.concat([pd.read_csv(f) for f in sorted((OUT / a.tag).glob("ep*_colors.csv"))], ignore_index=True) P = A.load_params() P1 = P["level1_color_exposure"] src = f"依頼 353 の校正 (cal_353.py. check_353/{a.tag} の 5 本. 実機 ÷ シムの中央値を前の値に掛けた)" log = {} gfix = {} if "gain" in what: scale = P1["session_light_scale"]["value"] for cam in ("desk", "wrist"): have = {} for s in ("v1", "pos23", "pos45", "pos1b"): r = ratios(c, cam, "table", s) if r is not None: have[s] = r if not have: continue mean = np.mean(list(have.values()), 0) for s in ("v1", "pos23", "pos45", "pos1b"): r = have.get(s, mean) old = np.array(P1["camera_gain_bgr"][cam][s]) new = old * r if s not in have and s == "pos1b" and cam == "wrist": new = new / scale["pos1b"] # 手首は 自動露出で 机の明るさが回で変わらない (依頼 337 wrist_table.by_session) gfix[(cam, s)] = r log[f"gain/{cam}/{s}"] = {"old": old.round(3).tolist(), "ratio": r.round(3).tolist(), "new": new.round(3).tolist(), "measured": s in have} P1["camera_gain_bgr"][cam][s] = new.round(3).tolist() P1["camera_gain_bgr"]["source"] = src + ". 机 (table) の比. 5 本に無い回は ほかの回の平均 (手首の pos1b はさらに光の倍率 0.834 で割る: 手首は自動露出で 机の明るさが回で変わらない. 依頼 337)" if "color" in what: gd = np.mean([v for (cam, s), v in gfix.items() if cam == "desk"] or [np.ones(3)], 0) gw = np.mean([v for (cam, s), v in gfix.items() if cam == "wrist"] or [np.ones(3)], 0) for key, cam, lb, gg in (("printed_rgb", "desk", "arm", gd), ("servo_rgb", "desk", "servo", gd), ("jaw_rgb", "wrist", "jaw", gw), ("box_floor_rgb", "desk", "boxfloor", gd), ("box_wall_rgb", "desk", "boxwall", gd)): r = ratios(c, cam, lb) if r is None: continue r = r / gg # 倍率の直しで変わる分は除く old = np.array(P1[key]["value"]) new = np.clip(old * r[::-1], 0, 1) # 色は R・G・B の順 log[key] = {"old": old.round(3).tolist(), "ratio_rgb": r[::-1].round(3).tolist(), "new": new.round(3).tolist()} P1[key]["value"] = new.round(3).tolist() P1[key]["source"] = P1[key]["source"].split(" → ")[0] + f" → {src} ({cam} の {lb})" if a.block: P3 = P["level3_block_light"] r = ratios(c, "wrist", "block") if r is not None: old = np.array(P3["block_rgb"]["value"]) new = np.clip(old * r[::-1], 0, 1) log["block_rgb"] = {"old": old.round(3).tolist(), "ratio_rgb": r[::-1].round(3).tolist(), "new": new.round(3).tolist()} P3["block_rgb"]["value"] = new.round(3).tolist() P3["block_rgb"]["source"] = P3["block_rgb"]["source"].split(" → ")[0] + f" → {src} (手首のブロック)" print(json.dumps(log, ensure_ascii=False, indent=1)) (OUT / f"cal_{a.tag}.json").write_text(json.dumps(log, ensure_ascii=False, indent=1)) if a.apply: A.PARAMS.write_text(json.dumps(P, ensure_ascii=False, indent=1) + "\n") print("appearance_353.json を書いた") if __name__ == "__main__": main()