#!/usr/bin/env python3 """A/B verification: Python appearance_processor vs rust_appearance. Compares two appearance.json outputs structurally: - frame_count, fps - per frame: persons, facing, bbox, body parts (name/bbox), histograms, dominant colors Done for the P0 validation gate of the Rust processor migration. """ import json import sys def load(p): with open(p) as f: return json.load(f) def compare_bbox(a, b, tol=1.0): if abs(a["x"] - b["x"]) > tol or abs(a["y"] - b["y"]) > tol: return False if abs(a["width"] - b["width"]) > tol or abs(a["height"] - b["height"]) > tol: return False return True def hist_diff(h1, h2): """Mean absolute difference of normalized histograms.""" if len(h1) != len(h2): return 1.0 if len(h1) == 0: return 0.0 total = 0.0 for a, b in zip(h1, h2): total += abs(a - b) return total / len(h1) def colors_close(c1, c2, tol=30.0): """Dominant colors are k-means outputs (random init in Python) — compare loosely.""" if len(c1) != len(c2): return len(c1) == 0 and len(c2) == 0 if len(c1) == 0: return True matched = 0 used = [False] * len(c2) for a in c1: best = None bi = -1 for i, b in enumerate(c2): if used[i]: continue d = sum((x - y) ** 2 for x, y in zip(a, b)) ** 0.5 if best is None or d < best: best = d bi = i if best is not None and best <= tol: matched += 1 used[bi] = True return matched >= max(1, len(c1) // 2) def main(): py = load(sys.argv[1]) rs = load(sys.argv[2]) issues = [] if py["frame_count"] != rs["frame_count"]: issues.append(f"frame_count mismatch: {py['frame_count']} vs {rs['frame_count']}") if abs(py["fps"] - rs["fps"]) > 0.01: issues.append(f"fps mismatch: {py['fps']} vs {rs['fps']}") py_frames = {f["frame"]: f for f in py["frames"]} rs_frames = {f["frame"]: f for f in rs["frames"]} if set(py_frames) != set(rs_frames): issues.append(f"frame sets differ: py={len(py_frames)} rs={len(rs_frames)}") n_frames = 0 n_persons = 0 n_parts = 0 facing_mismatch = 0 hist_fail = 0 color_fail = 0 bbox_fail = 0 missing_parts = 0 for fnum, pf in sorted(py_frames.items()): rf = rs_frames.get(fnum) if rf is None: continue n_frames += 1 if len(pf["persons"]) != len(rf["persons"]): issues.append(f"frame {fnum}: person count {len(pf['persons'])} vs {len(rf['persons'])}") continue for pp, rp in zip(pf["persons"], rf["persons"]): n_persons += 1 if pp["facing"] != rp["facing"]: facing_mismatch += 1 if not compare_bbox(pp["bbox"], rp["bbox"], tol=1.0): bbox_fail += 1 # histograms (full bbox) if len(pp["hsv_histogram"]) == 3 and len(rp["hsv_histogram"]) == 3: for ch in range(3): if hist_diff(pp["hsv_histogram"][ch], rp["hsv_histogram"][ch]) > 0.02: hist_fail += 1 break elif pp["hsv_histogram"] != rp["hsv_histogram"]: hist_fail += 1 if not colors_close(pp["dominant_colors"], rp["dominant_colors"]): color_fail += 1 py_parts = {bp["name"]: bp for bp in pp["body_parts"]} rs_parts = {bp["name"]: bp for bp in rp["body_parts"]} if set(py_parts) != set(rs_parts): missing_parts += 1 continue for name, pb in py_parts.items(): rb = rs_parts[name] n_parts += 1 if not compare_bbox(pb["bbox"], rb["bbox"], tol=1.0): bbox_fail += 1 for ch in range(3): if hist_diff(pb["hsv_histogram"][ch], rb["hsv_histogram"][ch]) > 0.02: hist_fail += 1 break if not colors_close(pb["dominant_colors"], rb["dominant_colors"]): color_fail += 1 print(f"frames : {n_frames}") print(f"persons : {n_persons}") print(f"body parts : {n_parts}") print(f"facing : {facing_mismatch} mismatch") print(f"bbox : {bbox_fail} mismatch (>1px)") print(f"histogram : {hist_fail} fail (>2% diff)") print(f"colors : {color_fail} fail (loose match)") print(f"missing parts: {missing_parts}") if issues: print("ISSUES:") for i in issues[:20]: print(" -", i) else: print("OK: no structural issues") if __name__ == "__main__": main()