Elffuss BraidHub

kikocis/VibeCodeViewer public

main · 3.4 KiB · executableRawPermalink
1#!/usr/bin/env python3
2import argparse
3import json
4from pathlib import Path
5
6import numpy as np
7from PIL import Image, ImageDraw
8
9
10def components(mask):
11 h, w = mask.shape
12 seen = np.zeros_like(mask, dtype=bool)
13 out = []
14 for y0, x0 in np.argwhere(mask & ~seen):
15 if seen[y0, x0]:
16 continue
17 stack = [(int(y0), int(x0))]
18 seen[y0, x0] = True
19 area = 0
20 minx = maxx = int(x0)
21 miny = maxy = int(y0)
22 while stack:
23 y, x = stack.pop()
24 area += 1
25 minx = min(minx, x); maxx = max(maxx, x)
26 miny = min(miny, y); maxy = max(maxy, y)
27 for ny, nx in ((y - 1, x), (y + 1, x), (y, x - 1), (y, x + 1)):
28 if 0 <= ny < h and 0 <= nx < w and mask[ny, nx] and not seen[ny, nx]:
29 seen[ny, nx] = True
30 stack.append((ny, nx))
31 out.append((area, minx, miny, maxx + 1, maxy + 1))
32 return out
33
34
35def main():
36 p = argparse.ArgumentParser()
37 p.add_argument("candidate_dir")
38 p.add_argument("baseline_dir")
39 p.add_argument("--overlay-dir")
40 p.add_argument("--min-area-ratio", type=float, default=0.018)
41 args = p.parse_args()
42
43 cand_files = sorted(Path(args.candidate_dir).glob("*.png"))
44 base_files = sorted(Path(args.baseline_dir).glob("*.png"))
45 if len(cand_files) != len(base_files):
46 raise SystemExit(f"frame count mismatch: {len(cand_files)} vs {len(base_files)}")
47
48 overlay_dir = Path(args.overlay_dir) if args.overlay_dir else None
49 if overlay_dir:
50 overlay_dir.mkdir(parents=True, exist_ok=True)
51
52 bad = []
53 for cf, bf in zip(cand_files, base_files):
54 cand = np.asarray(Image.open(cf).convert("RGB"), dtype=np.int16)
55 base = np.asarray(Image.open(bf).convert("RGB"), dtype=np.int16)
56 h, w = cand.shape[:2]
57 cand_l = cand[:, :, 0] * 0.2126 + cand[:, :, 1] * 0.7152 + cand[:, :, 2] * 0.0722
58 base_l = base[:, :, 0] * 0.2126 + base[:, :, 1] * 0.7152 + base[:, :, 2] * 0.0722
59 mask = (cand_l < 18) & (base_l > 42) & ((base_l - cand_l) > 35)
60 mask[: int(h * 0.09), :] = False
61 mask[int(h * 0.92) :, :] = False
62 mask[:, : int(w * 0.08)] = False
63 mask[:, int(w * 0.92) :] = False
64
65 hits = []
66 for area, x1, y1, x2, y2 in components(mask):
67 bw, bh = x2 - x1, y2 - y1
68 area_ratio = area / (w * h)
69 rect_fill = area / max(1, bw * bh)
70 if area_ratio >= args.min_area_ratio and bw >= w * 0.12 and bh >= h * 0.12 and rect_fill > 0.55:
71 hits.append({
72 "bbox": [x1, y1, x2, y2],
73 "area_ratio": round(area_ratio, 4),
74 "fill": round(rect_fill, 4),
75 })
76 if hits:
77 item = {"file": str(cf), "baseline": str(bf), "hits": hits}
78 bad.append(item)
79 print(json.dumps(item, ensure_ascii=False))
80 if overlay_dir:
81 overlay = Image.open(cf).convert("RGB")
82 d = ImageDraw.Draw(overlay)
83 for hit in hits:
84 d.rectangle(hit["bbox"], outline=(255, 0, 0), width=5)
85 overlay.save(overlay_dir / f"{cf.stem}-regression.png")
86
87 print(json.dumps({"frames": len(cand_files), "bad_frames": len(bad)}, ensure_ascii=False))
88 raise SystemExit(1 if bad else 0)
89
90
91if __name__ == "__main__":
92 main()