"""Before/after gallery on held-out RAQEEB test images, with a per-image check against the ground truth.
python make_gallery.py --weights ... --thresholds ... --out
Images are chosen by class content (most distinct annotated classes per source), never by model performance.
Each image is scored only on the classes its source fully annotates (dataset/label_sets.json): a predicted box
counts as correct when it overlaps a ground-truth box of the same class with IoU >= 0.5 (one-to-one).
"""
import argparse
import glob
import json
from pathlib import Path
from types import SimpleNamespace
import numpy as np
from PIL import Image
from annotate_raqeeb import NAMES, before_after, boxes_from_detections, draw_boxes
RAQ = Path(r'D:\CCTV_AI_FLOORPLAN_data\RAQEEB CCTV')
GALLERY = [ # (id prefix, file name, source description, licence)
('RES-01__high_quality_architectural_416', '01_cubicasa_architectural_plan',
'CubiCasa5K (high_quality_architectural/416)', 'CC BY-NC 4.0'),
('RES-02__train-01_1031-0029', '02_floorplancad_cad_sheet', 'FloorPlanCAD (CAD drawing crop)', 'CC BY-NC 4.0'),
('RES-02__test-00_0067-0021', '03_floorplancad_escalator', 'FloorPlanCAD (CAD drawing crop)', 'CC BY-NC 4.0'),
('RF-09__test_ROW_FP_SPA_843441212', '04_lifts_stairs_spaces', 'Roboflow Universe kt/floor-spac', 'CC BY 4.0'),
('RF-12__test_evctmap-413', '05_evacuation_plan_exits', 'Roboflow Universe tw/evacuation-plan', 'CC BY 4.0'),
('RF-02__train_Working-drawings_stamped-pdf_14', '06_working_drawing_sheet', 'Roboflow Universe Doxle/floor-plan',
'CC BY 4.0'),
('RF-01__test_1235_png_jpg', '07_residential_plan', 'Roboflow Universe Floorplanes/training-ai', 'CC BY 4.0'),
('RF-14__test_FloorPlan-13-', '08_simple_floor_plan', 'Roboflow Universe simple-floor-plan/floor-plan',
'CC BY 4.0'),
]
def gt_boxes(label_path, w, h):
out = []
for line in Path(label_path).read_text().splitlines():
v = line.split()
if len(v) < 7:
continue
xy = np.array(v[1:], float).reshape(-1, 2) * [w, h]
out.append((int(v[0]), [*xy.min(0), *xy.max(0)]))
return out
def iou(a, b):
ix = max(0, min(a[2], b[2]) - max(a[0], b[0]))
iy = max(0, min(a[3], b[3]) - max(a[1], b[1]))
i = ix * iy
return i / max((a[2] - a[0]) * (a[3] - a[1]) + (b[2] - b[0]) * (b[3] - b[1]) - i, 1e-9)
def main():
ap = argparse.ArgumentParser()
ap.add_argument('--weights', type=Path, required=True)
ap.add_argument('--thresholds', type=Path, required=True)
ap.add_argument('--out', type=Path, required=True)
ap.add_argument('--max-side', type=int, default=1400)
ap.add_argument('--device', default='0')
a = ap.parse_args()
from ultralytics import YOLO
from infer_raqeeb import infer_page
a.out.mkdir(parents=True, exist_ok=True)
sets = json.loads((RAQ / 'dataset' / 'label_sets.json').read_text())
model = YOLO(str(a.weights))
config = json.loads(a.thresholds.read_text())
args = SimpleNamespace(tile=0, overlap=256, imgsz=1024, device=a.device)
report = []
for prefix, fname, source, licence in GALLERY:
img = sorted(glob.glob(str(RAQ / 'dataset' / 'images' / 'test' / f'{prefix}*')))[0]
iid = Path(img).stem
im = Image.open(img).convert('RGB')
dets, _ = infer_page(model, im, config, args)
boxes = boxes_from_detections(dets)
full = sets['image_full_overrides'].get(iid, sets['sources'][iid.split('__')[0]]['full'])
gts = gt_boxes(RAQ / 'dataset' / 'labels' / 'test' / f'{iid}.txt', *im.size)
per = {}
for c in sorted(full):
g = [b for k, b in gts if k == c]
p = sorted([b for b in boxes if b['class_id'] == c], key=lambda b: -b['confidence'])
if not g and not p:
continue
used, tp = set(), 0
for b in p:
best = max(((iou(b['box_xyxy'], x), j) for j, x in enumerate(g) if j not in used), default=(0, None))
if best[0] >= 0.5:
used.add(best[1])
tp += 1
per[NAMES[c]] = dict(ground_truth=len(g), predicted=len(p), correct_iou50=tp)
s = min(1.0, a.max_side / max(im.size))
small = im.resize((round(im.width * s), round(im.height * s)), Image.LANCZOS) if s < 1 else im
after = draw_boxes(small, [dict(b, box_xyxy=[v * s for v in b['box_xyxy']]) for b in boxes])
before_after(small, after).save(a.out / f'{fname}.jpg', quality=88)
report.append(dict(file=f'{fname}.jpg', image_id=iid, source=source, licence=licence, split='test',
scored_classes=[NAMES[c] for c in sorted(full)], per_class=per,
all_predictions={n: sum(1 for b in boxes if b['label'] == n)
for n in sorted({b['label'] for b in boxes})}))
print(fname, json.dumps(per), flush=True)
(a.out / 'gallery.json').write_text(json.dumps(report, indent=1))
if __name__ == '__main__':
main()