"""把 `scripts/video_eval.py` 的 JSONL 汇总成可读结论(L3 回放报告)。""" import json import os import sys from collections import Counter REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) LABELS = {0: "not_on_train", 1: "bed", 2: "flatbed", 255: "unknown"} def runs(rows, key): out = [] cur = None for row in rows: state = int(row[key]) if cur is None or cur["state"] != state: cur = {"state": state, "start_ts": row["ts"], "end_ts": row["ts"], "frames": 0, "stable": 0, "conf_sum": 0.0} out.append(cur) cur["frames"] += 1 cur["end_ts"] = row["ts"] cur["stable"] += 1 if row.get("prod_stable" if key == "prod" else "diag_stable") else 0 cur["conf_sum"] += float(row.get("prod_conf" if key == "prod" else "diag_conf", 0.0)) for run in out: run["dur_s"] = round(run["end_ts"] - run["start_ts"], 2) run["conf"] = round(run["conf_sum"] / max(1, run["frames"]), 3) del run["conf_sum"] return out def main(): paths = sys.argv[1:] if not paths: print("用法:python scripts/summarize_eval.py [更多 jsonl]") return 2 report = {} for path in paths: rows = [json.loads(line) for line in open(path, encoding="utf-8") if line.strip()] tag = os.path.splitext(os.path.basename(path))[0] prod_runs = runs(rows, "prod") cand = Counter(int(r["cand"]) for r in rows) total = float(len(rows)) report[tag] = { "frames": len(rows), "duration_s": round(rows[-1]["ts"] - rows[0]["ts"], 2) if rows else 0.0, "prod_frames": {LABELS[k]: v for k, v in sorted(cand.items())}, "prod_runs": len(prod_runs), "prod_state_frames": { LABELS[s]: sum(r["frames"] for r in prod_runs if r["state"] == s) for s in (0, 1, 2, 255) }, "prod_state_seconds": { LABELS[s]: round(sum(r["dur_s"] for r in prod_runs if r["state"] == s), 2) for s in (0, 1, 2, 255) }, "prod_longest": { LABELS[s]: max([r for r in prod_runs if r["state"] == s], key=lambda r: r["frames"], default=None) for s in (0, 1, 2, 255) }, "prod_stable_frames": sum(1 for r in rows if r["prod_stable"]), "commit_frames": sum(1 for r in rows if r["committed"]), "first_commit_ts": next((r["ts"] for r in rows if r["committed"]), None), "stable_ratio": round(sum(1 for r in rows if r["prod_stable"]) / max(1.0, total), 3), "abstain": dict(Counter(r["abstain_reason"] or "-" for r in rows).most_common(6)), "commit_reasons": dict(Counter( str(r["commit_reason"]).split(" ")[0] for r in rows).most_common(6)), "candidate_non255_seconds": round(sum( 1 for r in rows if int(r["cand"]) != 255) / 30.0, 1), "first_runs": prod_runs[:8], } out = os.path.join(REPO, "runs", "video_eval", "report.json") os.makedirs(os.path.dirname(out), exist_ok=True) with open(out, "w", encoding="utf-8") as fh: json.dump(report, fh, ensure_ascii=False, indent=2) print(json.dumps(report, ensure_ascii=False, indent=2)) return 0 if __name__ == "__main__": sys.exit(main())