#!/usr/bin/env python3 """Replay the recorded drives of park-lab through the judging code. recordings/.jsonl holds, for one entry-camera visit, the box of the followed car on every processed frame ({"n", "t", "box"}), as park-lab recorded it on 2026-11-21 and 2026-11-22. recordings/index.json says which zone and line were drawn at the time, what the code decides today (verdict, close), and, where the driver said what he did, the truth. ../../apps/vision/.venv/bin/python replay_recordings.py # check ../../apps/vision/.venv/bin/python replay_recordings.py --update # accept today's verdicts It fails when a verdict differs from the stored one, or when a visit with a known truth is judged wrong and is not marked as a known miss. Limits: only the followed car's box was recorded (no other vehicles, no pictures), and a gap between recorded frames is replayed as frames with no car. Needs no camera, model or relay.""" from __future__ import annotations import json import sys import tempfile import threading from pathlib import Path import numpy as np HERE = Path(__file__).parent sys.path.insert(0, str(HERE)) import zone_anpr as z # noqa: E402 z.log = lambda *a: None REC = HERE / "recordings" TRUTH_AS_VERDICT = {"passed": "passed", "abandoned": "abandoned", "out": "abandoned"} # a drive-out is B → A class Det: now: list = [] def detect(self, img): return list(self.now) class Rel: def __init__(self): self.asked = [] def pulse(self, kind, ch, why, valid=None): self.asked.append(kind) def replay(frames, setup): """(verdict, last crossing, how the zone became free, close asked) for one recorded visit.""" out = Path(tempfile.mkdtemp(prefix="zone-anpr-replay-")) (out / "saves").mkdir() class App: pass a = App() a.cfg = dict(detect_fps=5, min_plate_conf=0.9, votes_needed=3, agree_share=0.75, visit_gap_s=3, zone_overlap=0.2, clip_keep=0, close_on_lost=True) # as park-lab runs it a.out, a.io_lock, a.lane_lock, a.relays, a.yolox = out, threading.Lock(), threading.Lock(), Rel(), Det() cam = z.Camera(dict(name="entry", url="rtsp://x", zone=setup["zone"], line=setup["line"], relay_open=1, relay_close=2), a) cam.read_busy = True # no plate reads a.cams = [cam] blank, t, prev = np.zeros((720, 1280, 3), np.uint8), frames[0]["t"], None for r in frames: if prev and r["t"] - prev > 0.3: # frames in which the recorder had no box for the car for k in range(1, int(round((r["t"] - prev) / 0.2))): a.yolox.now = [] cam.step(blank, prev + 0.2 * k) a.yolox.now = [(*r["box"], 0.8, 2)] cam.step(blank, r["t"]) prev = t = r["t"] if cam.track is None and (out / "visits.jsonl").exists(): break for _ in range(20): if cam.track is None: break t += 0.2 a.yolox.now = [] cam.step(blank, t) p = out / "visits.jsonl" if not p.exists(): return dict(verdict="none", last_crossing=None, end=None, close=False) v = json.loads(p.read_text().splitlines()[0]) return dict(verdict=v["verdict"], last_crossing=v["last_crossing"], end=v["end"], close="close" in a.relays.asked) def main(): idx = json.loads((REC / "index.json").read_text()) update, changed, wrong, known = "--update" in sys.argv, [], [], [] for r in idx["recordings"]: frames = [json.loads(x) for x in (REC / r["file"]).read_text().splitlines()] got = replay(frames, idx["setups"][r["setup"]]) if any(r.get(k) != got[k] for k in got): changed.append((r["file"], {k: r.get(k) for k in got}, got)) if update: r.update(got) if r.get("truth") and TRUTH_AS_VERDICT[r["truth"]] != got["verdict"]: (known if r.get("known_miss") else wrong).append((r["file"], r["truth"], got["verdict"], r.get("note", ""))) n, t = len(idx["recordings"]), sum(1 for r in idx["recordings"] if r.get("truth")) for f, old, new in changed: print(f"{'UPDATED' if update else 'CHANGED'} {f}\n stored: {old}\n now: {new}") for f, truth, got, note in known: print(f"known miss {f}: the driver {truth}, judged {got}. {note}") for f, truth, got, note in wrong: print(f"WRONG {f}: the driver {truth}, judged {got}. {note}") print(f"\n{n} recorded visits: {n - len(changed)} as stored, {len(changed)} changed | " f"{t} with a known truth: {t - len(known) - len(wrong)} judged right, {len(known)} known misses, {len(wrong)} wrong") if update: (REC / "index.json").write_text(json.dumps(idx, indent=1, ensure_ascii=False) + "\n") sys.exit(1 if (changed and not update) or wrong else 0) if __name__ == "__main__": main()