constructelligence's picture
Update code: real-machine G-code adapter, commissioning gate, machine_job runner, pump watchdog firmware, twin completion gate, realism and masking modules (card and weights unchanged)
4eecd03 verified
Raw History Blame Contribute Delete
4.42 kB
#!/usr/bin/env python3
"""Stress the phone demo on many real photos: does it plan, how fast, and is the plan sane?
python3 demo_batch.py --data real_data --split test --checkpoint artifacts/candidates/ft4/best.pt --out demo_batch.json
Per photo: seconds for the model, seconds for the plan, ok / reason, and sanity checks that catch a plan a viewer
would laugh at -- tape longer than the perimeter of what it protects, tape off the image, paint outside the wall,
a bill with zero paint. Failures are listed by name; nothing is averaged away.
"""
from __future__ import annotations
import argparse
import json
import time
from pathlib import Path
import numpy as np
from PIL import Image, ImageOps
from demo_plan import CLASS_IDS, WALL_IDS, build_demo_plan
def checks(plan, classes):
problems = []
if not plan.get("ok"):
return problems
h, w = classes.shape
for r in plan["tape"]:
if any(not (-0.02 <= x <= 1.02 and -0.02 <= y <= 1.02) for x, y in r["points"]):
problems.append("tape leaves the image")
break
mm = plan["scale"]["mm_per_px"]
perim = sum(2 * ((i["bbox"][2] - i["bbox"][0]) * w + (i["bbox"][3] - i["bbox"][1]) * h) * mm / 1000.0
for i in plan["items"] if i["treatment"] == "tape")
got = sum(r["length_mm"] for r in plan["tape"]) / 1000.0
if got > perim * 1.6 + 1.0:
problems.append(f"{got:.1f} m of tape for {perim:.1f} m of perimeter to protect")
if plan["bill"]["paint_m2"] <= 0:
problems.append("zero paint area")
if plan["bill"]["paint_m2"] > plan["bill"]["wall_m2"] * 1.02:
problems.append("paints more than the wall")
if len(plan["strokes"]) > 2000:
problems.append(f"{len(plan['strokes'])} roller strokes (animation would crawl)")
return problems
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--data", type=Path, default=Path("real_data"))
ap.add_argument("--split", default="test")
ap.add_argument("--checkpoint", default="artifacts/candidates/ft4/best.pt")
ap.add_argument("--limit", type=int, default=0)
ap.add_argument("--out", type=Path)
a = ap.parse_args()
from console_server import model_analyzer
analyze, info = model_analyzer(a.checkpoint, "cpu")
paths = sorted((a.data / a.split / "images").glob("*.jpg"))
if a.limit:
paths = paths[:a.limit]
rows = []
for p in paths:
image = ImageOps.exif_transpose(Image.open(p)).convert("RGB")
image.thumbnail((2048, 2048))
rgb = np.asarray(image)
t = time.time()
summary, masks = analyze(rgb)
t_model = time.time() - t
classes = np.asarray(masks["mask"], np.uint8)
t = time.time()
try:
plan = build_demo_plan(classes, None, None, None, rgb.mean(axis=2))
err = None
except Exception as exc: # a crash is the worst outcome: record it by name
plan, err = {"ok": False, "reason": "CRASH"}, f"{type(exc).__name__}: {exc}"
t_plan = time.time() - t
problems = checks(plan, classes) + ([err] if err else [])
rows.append({"image": p.name[:40], "model_s": round(t_model, 2), "plan_s": round(t_plan, 2), "ok": plan.get("ok"),
"reason": plan.get("reason"), "problems": problems,
"tape_m": (plan.get("bill") or {}).get("tape_m"), "strokes": len(plan.get("strokes") or [])})
print(f"{len(rows):3d} {'ok ' if plan.get('ok') else 'no '} model {t_model:4.1f}s plan {t_plan:4.1f}s "
f"{'; '.join(problems) or (plan.get('reason') or '')}", flush=True)
n = len(rows)
ok = [r for r in rows if r["ok"]]
summary = {"photos": n, "planned": len(ok), "refused": sum(1 for r in rows if not r["ok"] and r["reason"] != "CRASH"),
"crashes": sum(1 for r in rows if r["reason"] == "CRASH"),
"with_problems": sum(1 for r in rows if r["problems"]),
"model_s_median": float(np.median([r["model_s"] for r in rows])) if rows else None,
"plan_s_median": float(np.median([r["plan_s"] for r in rows])) if rows else None,
"plan_s_max": max((r["plan_s"] for r in rows), default=None)}
print(json.dumps(summary, indent=1))
if a.out:
a.out.write_text(json.dumps({"summary": summary, "rows": rows}, indent=1))
if __name__ == "__main__":
main()