Spaces:
Paused
Paused
Download python_files/_combined_overlay.py from Rachel7/hvac_fable: direct link, hf CLI and curl.
- Browser
- Download file 4.77 kB
-
https://huggingface.co/spaces/Rachel7/hvac_fable/resolve/main/python_files/_combined_overlay.py
- Command line
-
hf download hf://spaces/Rachel7/hvac_fable/python_files/_combined_overlay.py
-
curl -L -o _combined_overlay.py https://huggingface.co/spaces/Rachel7/hvac_fable/resolve/main/python_files/_combined_overlay.py
4.77 kB
| """ | |
| Draw all model detections on a single page image, each model a distinct color, | |
| with a legend. Featherless excluded (127 hallucinations would obscure everything). | |
| """ | |
| import json | |
| from pathlib import Path | |
| import fitz | |
| from PIL import Image, ImageDraw, ImageFont | |
| DET_DIR = Path(__file__).parent / "output_floor_iter" / "cellar_allunits_compare" | |
| PDF_PATH = Path(__file__).parent.parent / "raw_pdfs" / "355 E 86th St Mechanical 100% CD Set_flattened.pdf" | |
| # One color per model (BGR-ish but we're in PIL so RGB) | |
| MODELS = [ | |
| ("Gemini 2.5 Pro", "gemini_gemini-2.5-pro_detections.json", (0, 200, 50)), | |
| ("Gemini 2.5 Flash", "gemini_gemini-2.5-flash_detections.json", (0, 160, 255)), | |
| ("GPT-4o", "openrouter_openai_gpt-4o_detections.json", (255, 80, 0)), | |
| ("Claude S4.5", "openrouter_anthropic_claude-sonnet-4-5_detections.json", (220, 200, 0)), | |
| # Featherless skipped β 127 detections would flood the image | |
| ] | |
| GOLD_COLOR = (255, 255, 255) # white outline = gold ground-truth boxes | |
| GOLD = [ | |
| {"tag": "AC-P", "x": 7050, "y": 1300, "w": 500, "h": 550}, | |
| {"tag": "AC-S", "x": 3970, "y": 2200, "w": 500, "h": 500}, | |
| {"tag": "AC-Q", "x": 2530, "y": 2800, "w": 500, "h": 500}, | |
| {"tag": "AC-P", "x": 2530, "y": 3650, "w": 900, "h": 650}, | |
| {"tag": "AC-P", "x": 2530, "y": 4600, "w": 900, "h": 650}, | |
| {"tag": "AC-P", "x": 5750, "y": 4450, "w": 700, "h": 500}, | |
| {"tag": "AC-T", "x": 1800, "y": 6050, "w": 500, "h": 500}, | |
| {"tag": "AC-T", "x": 3300, "y": 7220, "w": 500, "h": 500}, | |
| {"tag": "AC-U", "x": 6600, "y": 6950, "w": 500, "h": 500}, | |
| {"tag": "AC-U", "x": 9050, "y": 7310, "w": 500, "h": 500}, | |
| {"tag": "AC-T", "x": 8300, "y": 5700, "w": 500, "h": 500}, | |
| {"tag": "AC-T", "x": 9600, "y": 6200, "w": 500, "h": 500}, | |
| {"tag": "unit", "x": 8200, "y": 400, "w": 600, "h": 600}, | |
| ] | |
| # render at 100 DPI for a bigger image (~2000Γ1500) so boxes are readable | |
| print("Rendering page at 100 DPI...") | |
| doc = fitz.open(PDF_PATH) | |
| pix = doc.load_page(1).get_pixmap(matrix=fitz.Matrix(100/72, 100/72), alpha=False) | |
| img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples) | |
| S = pix.width / 14400 | |
| print(f"Image: {img.width}x{img.height} scale={S:.4f}") | |
| draw = ImageDraw.Draw(img) | |
| try: | |
| font_big = ImageFont.truetype("arial.ttf", 28) | |
| font_med = ImageFont.truetype("arial.ttf", 20) | |
| font_sm = ImageFont.truetype("arial.ttf", 16) | |
| except Exception: | |
| font_big = font_med = font_sm = ImageFont.load_default() | |
| # ββ gold boxes (white, thin) βββββββββββββββββββββββββββββββββββββββββββββββββ | |
| for g in GOLD: | |
| x1, y1 = int(g["x"]*S), int(g["y"]*S) | |
| x2, y2 = int((g["x"]+g["w"])*S), int((g["y"]+g["h"])*S) | |
| draw.rectangle([x1, y1, x2, y2], outline=GOLD_COLOR, width=2) | |
| draw.text((x1+2, y1+2), g["tag"], fill=GOLD_COLOR, font=font_sm) | |
| # ββ model detections βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| for label, fname, color in MODELS: | |
| f = DET_DIR / fname | |
| if not f.exists(): | |
| print(f" MISSING: {fname}") | |
| continue | |
| dets = json.loads(f.read_text(encoding="utf-8"))["detections"] | |
| print(f" {label}: {len(dets)} dets") | |
| for det in dets: | |
| x1, y1 = int(det["x"]*S), int(det["y"]*S) | |
| x2, y2 = int((det["x"]+det["w"])*S), int((det["y"]+det["h"])*S) | |
| draw.rectangle([x1, y1, x2, y2], outline=color, width=4) | |
| # ββ legend ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| LEG_X, LEG_Y = img.width - 420, 30 | |
| LEG_PAD = 10 | |
| box_h = 36 | |
| entries = [("White boxes = gold (ground truth)", GOLD_COLOR)] + \ | |
| [(label, color) for label, _, color in MODELS] | |
| leg_h = LEG_PAD * 2 + box_h * len(entries) + 4 * (len(entries) - 1) | |
| leg_w = 410 | |
| # semi-transparent dark background | |
| overlay = Image.new("RGBA", img.size) | |
| bg = Image.new("RGBA", (leg_w, leg_h), (20, 20, 20, 200)) | |
| overlay.paste(bg, (LEG_X - LEG_PAD, LEG_Y - LEG_PAD)) | |
| img = img.convert("RGBA") | |
| img = Image.alpha_composite(img, overlay).convert("RGB") | |
| draw = ImageDraw.Draw(img) | |
| for i, (text, color) in enumerate(entries): | |
| y = LEG_Y + i * (box_h + 4) | |
| # color swatch | |
| draw.rectangle([LEG_X, y, LEG_X + 40, y + box_h - 2], outline=color, fill=color, width=3) | |
| # label | |
| draw.text((LEG_X + 50, y + 6), text, fill=(255, 255, 255), font=font_med) | |
| out = DET_DIR / "combined_all_models.png" | |
| img.save(out) | |
| print(f"\nSaved: {out}") | |