Upload eval_mirage.py with huggingface_hub
Browse files- eval_mirage.py +139 -0
eval_mirage.py
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Score Mirage (Cactus) on the synthetic poker table dataset.
|
| 2 |
+
|
| 3 |
+
Modes: "describe" (objects + OCR text lines) and, if the Mirage API supports it,
|
| 4 |
+
"forms" scoring over card enums. Reports: card-read accuracy (OCR text that
|
| 5 |
+
matches a card glyph set), card-bbox recall via IoU matching to ground truth,
|
| 6 |
+
pot-text read rate, and per-image timing.
|
| 7 |
+
"""
|
| 8 |
+
import argparse
|
| 9 |
+
import json
|
| 10 |
+
import re
|
| 11 |
+
import time
|
| 12 |
+
from pathlib import Path
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def iou(a, b):
|
| 16 |
+
ax0, ay0, ax1, ay1 = a
|
| 17 |
+
bx0, by0, bx1, by1 = b
|
| 18 |
+
ix = max(0, min(ax1, bx1) - max(ax0, bx0))
|
| 19 |
+
iy = max(0, min(ay1, by1) - max(ay0, by0))
|
| 20 |
+
inter = ix * iy
|
| 21 |
+
if inter <= 0:
|
| 22 |
+
return 0.0
|
| 23 |
+
return inter / (((ax1 - ax0) * (ay1 - ay0)) + ((bx1 - bx0) * (by1 - by0)) - inter)
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def norm_rank(r):
|
| 27 |
+
return {"T": "10", "A": "A", "K": "K", "Q": "Q", "J": "J"}.get(r, r)
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def card_matches(text, card):
|
| 31 |
+
"""Tolerant: rank (3..10,J..A) and suit symbol both present, any order."""
|
| 32 |
+
rank, suit = card[:-1], card[-1]
|
| 33 |
+
t = text.replace(" ", "").upper()
|
| 34 |
+
r_ok = rank.upper() in t if rank != "10" else ("10" in t or "T" in t.upper().replace("10", ""))
|
| 35 |
+
s_ok = suit in text or suit in t
|
| 36 |
+
return r_ok and s_ok
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def main():
|
| 40 |
+
ap = argparse.ArgumentParser()
|
| 41 |
+
ap.add_argument("--images", default="dataset/images")
|
| 42 |
+
ap.add_argument("--labels", default="dataset/labels")
|
| 43 |
+
ap.add_argument("--limit", type=int, default=0, help="0 = all")
|
| 44 |
+
ap.add_argument("--out", default="results_mirage.json")
|
| 45 |
+
ap.add_argument("--scale", type=float, default=1.0,
|
| 46 |
+
help="upscale factor for the image before inference; "
|
| 47 |
+
"ground-truth bboxes are scaled to match for IoU")
|
| 48 |
+
ap.add_argument("--sections", default="objects,text")
|
| 49 |
+
args = ap.parse_args()
|
| 50 |
+
|
| 51 |
+
from cactus import Mirage
|
| 52 |
+
model = Mirage()
|
| 53 |
+
print("mirage loaded", flush=True)
|
| 54 |
+
|
| 55 |
+
sections = [s for s in args.sections.split(",") if s]
|
| 56 |
+
img_dir, lbl_dir = Path(args.images), Path(args.labels)
|
| 57 |
+
names = sorted(p.name for p in img_dir.glob("*.png"))
|
| 58 |
+
if args.limit:
|
| 59 |
+
names = names[: args.limit]
|
| 60 |
+
|
| 61 |
+
# ground truth card strings
|
| 62 |
+
per_image = []
|
| 63 |
+
card_hits = card_true = iou_hits = iou_true = pot_hits = pot_true = 0
|
| 64 |
+
reads = []
|
| 65 |
+
t_start = time.time()
|
| 66 |
+
for i, name in enumerate(names):
|
| 67 |
+
lab = json.loads((lbl_dir / name.replace(".png", ".json")).read_text())
|
| 68 |
+
from PIL import Image as PILImage
|
| 69 |
+
img = PILImage.open(img_dir / name).convert("RGB")
|
| 70 |
+
sf = args.scale
|
| 71 |
+
if sf != 1.0:
|
| 72 |
+
img = img.resize((int(img.width * sf), int(img.height * sf)),
|
| 73 |
+
PILImage.LANCZOS)
|
| 74 |
+
t0 = time.time()
|
| 75 |
+
try:
|
| 76 |
+
res = model.describe(img, opts={
|
| 77 |
+
"sections": sections, "conf": 0.4, "text_conf": 0.3,
|
| 78 |
+
"max_objects": 30, "max_text_lines": 30, "max_tokens": 700,
|
| 79 |
+
"nms": True})
|
| 80 |
+
except TypeError:
|
| 81 |
+
res = model.describe(img, {"sections": sections, "conf": 0.4,
|
| 82 |
+
"text_conf": 0.3, "max_objects": 30,
|
| 83 |
+
"max_text_lines": 30, "max_tokens": 700,
|
| 84 |
+
"nms": True})
|
| 85 |
+
dt = time.time() - t0
|
| 86 |
+
ocr_lines = [l.get("string", "") for l in (res.get("text") or [])]
|
| 87 |
+
obj_names = [o.get("name", "") for o in (res.get("objects") or [])]
|
| 88 |
+
obj_boxes = [o.get("bbox") for o in (res.get("objects") or []) if o.get("bbox")]
|
| 89 |
+
ocr_boxes = [l.get("bbox") for l in (res.get("text") or []) if l.get("bbox")]
|
| 90 |
+
|
| 91 |
+
# card read: each GT card matched by some OCR object or text line
|
| 92 |
+
gt_cards = [c["card"] for c in lab["card_bboxes"]]
|
| 93 |
+
img_hits = 0
|
| 94 |
+
img_ious = []
|
| 95 |
+
matched = set()
|
| 96 |
+
for gt in gt_cards:
|
| 97 |
+
hit = any(card_matches(t, gt) for t in ocr_lines) or \
|
| 98 |
+
any(card_matches(t, gt) for t in obj_names)
|
| 99 |
+
img_hits += hit
|
| 100 |
+
# bbox side: best IoU of any detection against any GT card box
|
| 101 |
+
best = 0.0
|
| 102 |
+
best_j = -1
|
| 103 |
+
gt_box = next(c["bbox"] for c in lab["card_bboxes"] if c["card"] == gt)
|
| 104 |
+
if sf != 1.0:
|
| 105 |
+
gt_box = [v * sf for v in gt_box]
|
| 106 |
+
for j, ob in enumerate(obj_boxes + ocr_boxes):
|
| 107 |
+
v = iou(gt_box, ob) if ob else 0.0
|
| 108 |
+
if v > best:
|
| 109 |
+
best, best_j = v, j
|
| 110 |
+
img_ious.append(best)
|
| 111 |
+
card_hits += img_hits
|
| 112 |
+
card_true += len(gt_cards)
|
| 113 |
+
iou_hits += sum(1 for v in img_ious if v > 0.4)
|
| 114 |
+
iou_true += len(gt_cards)
|
| 115 |
+
pot_true += 1
|
| 116 |
+
if any(str(lab["pot"]) in t for t in ocr_lines):
|
| 117 |
+
pot_hits += 1
|
| 118 |
+
per_image.append({"image": name, "cards": len(gt_cards), "hits": img_hits,
|
| 119 |
+
"iou": round(sum(img_ious) / len(img_ious), 3) if img_ious else 0,
|
| 120 |
+
"sec": round(dt, 1), "ocr": ocr_lines[:8]})
|
| 121 |
+
if i < 3:
|
| 122 |
+
print(f"[{name}] ocr={ocr_lines[:10]} objs={obj_names[:10]} "
|
| 123 |
+
f"({dt:.1f}s)", flush=True)
|
| 124 |
+
wall = time.time() - t_start
|
| 125 |
+
summary = {
|
| 126 |
+
"names": names[:5], "sections": sections, "scale": args.scale,
|
| 127 |
+
"card_read_acc": round(card_hits / max(card_true, 1), 3),
|
| 128 |
+
"card_bbox_recall_iou40": round(iou_hits / max(iou_true, 1), 3),
|
| 129 |
+
"pot_read_acc": round(pot_hits / max(pot_true, 1), 3),
|
| 130 |
+
"cards_total": card_true, "sec_per_image": round(wall / len(names), 1),
|
| 131 |
+
"wall_min": round(wall / 60, 1),
|
| 132 |
+
}
|
| 133 |
+
Path(args.out).write_text(json.dumps(
|
| 134 |
+
{"summary": summary, "per_image": per_image}, indent=1))
|
| 135 |
+
print(json.dumps(summary, indent=2), flush=True)
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
if __name__ == "__main__":
|
| 139 |
+
main()
|