BlueprintParse-R50 / blueprintparse.py
thaki-AI's picture
BlueprintParse-R50: FP32 ONNX (5 sanitary fixture classes), inference + export code, cards
c227dfe verified
Raw History Blame Contribute Delete
7.81 kB
#!/usr/bin/env python3
"""BlueprintParse-R50 — detect five sanitary fixture classes in Korean architectural floor-plan images (ONNX, CPU/GPU).
Classes (model label ids 1..5): gas_range, toilet, washbasin, kitchen_sink, bathtub. Nothing else is detected or claimed.
Per-drawing counts are a DRAFT for human review: automatic quantity takeoff did not pass our gate (COUNT_NO_GO).
Pre-processing = training: grayscale, shrink so the long side <= max_side (2048, read from model metadata),
scale to 0..1, replicate to 3 channels. Boxes are mapped back to original pixel coordinates.
Usage:
python blueprintparse.py plan.png # JSON to stdout
python blueprintparse.py plan.png --providers CUDAExecutionProvider,CPUExecutionProvider
python blueprintparse.py --selftest # synthetic drawing, no data needed
"""
from __future__ import annotations
import argparse
import hashlib
import io
import json
import sys
from pathlib import Path
HERE = Path(__file__).resolve().parent
MODEL = HERE / "bp_fp32.onnx"
MODEL_SHA256 = "5ed19b8bccc6d5abfdeb5dd26f153c28379115b100a9a4ce479479e10bffb773"
INPUT_NAME, OUTPUT_NAMES = "image", ("boxes", "labels", "scores")
CLASS_EN = {"객체_가스레인지": "gas_range", "객체_변기": "toilet", "객체_세면대": "washbasin",
"객체_싱크대": "kitchen_sink", "객체_욕조": "bathtub"}
SCORE_THRESHOLD, CANDIDATE_FLOOR, REVIEW_THRESHOLD = 0.5, 0.3, 0.8
def sha256_file(p) -> str:
h = hashlib.sha256()
with open(p, "rb") as f:
for b in iter(lambda: f.read(1 << 22), b""):
h.update(b)
return h.hexdigest()
def preprocess(data: bytes, max_side: int):
"""-> (x [3,h,w] float32, scale, (W, H))."""
import numpy as np
from PIL import Image
with Image.open(io.BytesIO(data)) as im:
im = im.convert("L"); W, H = im.size; s = min(1.0, max_side / max(W, H))
small = im.resize((max(1, round(W * s)), max(1, round(H * s))), Image.BILINEAR) if s < 1.0 else im.copy()
a = np.asarray(small, dtype=np.uint8).astype(np.float32) / 255.0
return np.repeat(a[None], 3, axis=0), s, (W, H)
def to_original(box, scale: float, size) -> list:
W, H = size
x0, y0, x1, y1 = (float(v) / scale for v in box)
return [round(max(0., min(W, x0)), 2), round(max(0., min(H, y0)), 2),
round(max(0., min(W, x1)), 2), round(max(0., min(H, y1)), 2)]
class BlueprintParse:
def __init__(self, model_path=MODEL, providers=None, threads: int | None = None, verify_sha: bool = True):
import onnxruntime as ort
self.path = Path(model_path)
if verify_sha and self.path.resolve() == MODEL.resolve() and sha256_file(self.path) != MODEL_SHA256:
raise ValueError("bp_fp32.onnx sha256 mismatch")
so = ort.SessionOptions()
if threads:
so.intra_op_num_threads = int(threads)
self.sess = ort.InferenceSession(str(self.path), so, providers=providers or ["CPUExecutionProvider"])
meta = {}
for k, v in self.sess.get_modelmeta().custom_metadata_map.items():
try:
meta[k] = json.loads(v)
except (ValueError, TypeError):
meta[k] = v
self.meta = meta
cid = meta.get("class_ids") or {c: i + 1 for i, c in enumerate(CLASS_EN)}
self.names = {int(v): k for k, v in cid.items()}
self.max_side = int(meta.get("max_side", 2048))
def raw(self, x):
import numpy as np
b, l, s = self.sess.run(list(OUTPUT_NAMES), {INPUT_NAME: np.ascontiguousarray(x, dtype=np.float32)})
return np.asarray(b), np.asarray(l), np.asarray(s)
def __call__(self, data: bytes, score_threshold: float = SCORE_THRESHOLD, candidate_floor: float = CANDIDATE_FLOOR) -> dict:
x, scale, size = preprocess(data, self.max_side)
dets, cands = [], []
for b, l, s in zip(*self.raw(x)):
s = float(s)
if s < candidate_floor:
continue
name = self.names.get(int(l), f"unknown:{int(l)}")
d = {"class": CLASS_EN.get(name, "out_of_scope"), "score": round(s, 4), "box_xyxy": to_original(b, scale, size)}
(dets if s >= score_threshold else cands).append(d)
dets.sort(key=lambda d: -d["score"]); cands.sort(key=lambda d: -d["score"])
counts = {c: 0 for c in CLASS_EN.values()}
for d in dets:
if d["class"] in counts:
counts[d["class"]] += 1
review = bool(cands) or any(d["score"] < REVIEW_THRESHOLD for d in dets) or not dets
return {"input": {"sha256": hashlib.sha256(data).hexdigest(), "width": size[0], "height": size[1],
"resize_scale": round(scale, 6)},
"counts_draft": counts, "detections": dets, "uncertain_candidates": cands,
"thresholds": {"score": score_threshold, "candidate_floor": candidate_floor},
"review_required": True, "low_confidence_present": review,
"note": "Counts are a draft for human review (automatic takeoff COUNT_NO_GO). Zero detections is not proof of absence."}
def synthetic_drawing(h: int = 1448, w: int = 2048, seed: int = 0) -> bytes:
"""Deterministic synthetic line drawing (white background, black walls / boxes / rings). Not dataset data."""
import numpy as np
from PIL import Image
rng = np.random.default_rng(seed)
a = np.ones((h, w), dtype=np.float32)
for _ in range(60):
if rng.random() < .5:
y = int(rng.integers(0, h)); x0, x1 = sorted(rng.integers(0, w, 2)); a[max(0, y - 2):y + 2, x0:x1] = 0
else:
x = int(rng.integers(0, w)); y0, y1 = sorted(rng.integers(0, h, 2)); a[y0:y1, max(0, x - 2):x + 2] = 0
for _ in range(25):
cx, cy = int(rng.integers(40, w - 40)), int(rng.integers(40, h - 40)); r = int(rng.integers(10, 35))
a[cy - r:cy + r, cx - r] = a[cy - r:cy + r, cx + r - 1] = 0
a[cy - r, cx - r:cx + r] = a[cy + r - 1, cx - r:cx + r] = 0
yy, xx = np.ogrid[-r:r, -r:r]; ring = np.abs(np.sqrt(yy ** 2 + xx ** 2) - r * .6) < 1.2
a[cy - r:cy + r, cx - r:cx + r][ring] = 0
buf = io.BytesIO(); Image.fromarray((a * 255).astype("uint8")).save(buf, format="PNG")
return buf.getvalue()
def selftest(threads=None) -> dict:
import onnxruntime as ort
m = BlueprintParse(threads=threads)
out = m(synthetic_drawing())
assert out["review_required"] is True and set(out["counts_draft"]) == set(CLASS_EN.values())
return {"ok": True, "onnxruntime": ort.__version__, "inputs": [i.name for i in m.sess.get_inputs()],
"outputs": [o.name for o in m.sess.get_outputs()], "max_side": m.max_side,
"class_names_en": [CLASS_EN.get(m.names[k], "?") for k in sorted(m.names)],
"metadata_precision": m.meta.get("precision"), "n_detections": len(out["detections"]),
"n_candidates": len(out["uncertain_candidates"])}
def main(argv=None) -> int:
ap = argparse.ArgumentParser(description=__doc__.splitlines()[0])
ap.add_argument("image", nargs="?")
ap.add_argument("--model", default=str(MODEL))
ap.add_argument("--providers", default="CPUExecutionProvider")
ap.add_argument("--threads", type=int, default=None)
ap.add_argument("--selftest", action="store_true")
a = ap.parse_args(argv)
if a.selftest:
print(json.dumps(selftest(a.threads), ensure_ascii=False, indent=1)); return 0
if not a.image:
ap.error("image path required (or --selftest)")
m = BlueprintParse(a.model, providers=[p for p in a.providers.split(",") if p], threads=a.threads)
print(json.dumps(m(Path(a.image).read_bytes()), ensure_ascii=False, indent=1))
return 0
if __name__ == "__main__":
sys.exit(main())