#!/usr/bin/env python3
"""Analyse per-frame box counts in the 4 hand-annotation jobs (read-only)."""
import json, os, collections

BASE = "/home/agent/.claude/workspace/project/data/out"
JOBS = {
    "shipin1": "c3de403cfd15",
    "shipin2": "4e8924d45d4b",
    "shipin3": "fe165602b721",
    "shipin4": "e9ff7e814d64",
}

for stem, jid in JOBS.items():
    d = os.path.join(BASE, jid)
    bj = None
    for f in os.listdir(d):
        if f.endswith(".boxes.json"):
            bj = os.path.join(d, f)
    if not bj:
        print(stem, "NO boxes.json")
        continue
    rows = json.load(open(bj))
    dist = collections.Counter()
    lowconf = 0
    total_boxes = 0
    longest_single_run = 0
    cur_run = 0
    for r in rows:
        n = len(r.get("boxes", []))
        dist[n] += 1
        total_boxes += n
        for c in r.get("conf", []):
            if c < 0.70:
                lowconf += 1
        if n == 1:
            cur_run += 1
            longest_single_run = max(longest_single_run, cur_run)
        else:
            cur_run = 0
    n_frames = len(rows)
    print(f"== {stem} ({jid}) frames={n_frames} total_boxes={total_boxes} "
          f"mean={total_boxes / max(n_frames,1):.2f} lowconf<0.70={lowconf}")
    for k in sorted(dist):
        print(f"   {k} boxes: {dist[k]} frames ({100.0 * dist[k] / n_frames:.1f}%)")
    print(f"   longest consecutive 1-box run: {longest_single_run} frames "
          f"({longest_single_run / 30.0:.1f}s)")
