"""Make a to-scale wall elevation from a photo (or the fused mosaic of several photos / a video), as a drawing a person can read and the matcher can read back. Pipeline: wall measurement (wall_measure.measure_wall) gives the wall's floor-to-ceiling quad and its width and height in feet WITH the source of that scale. The class mask is rectified onto that quad at a fixed millimetres-per-pixel, windows and doors are pulled out as rectangles in millimetres, and the sheet is written as JSON (the format drawing_match.drawing_from_json reads), SVG, DXF and a PNG. What the drawing says about itself (``provenance``), because a drawing made from a photo is a DRAFT and the numbers are only as good as their anchor: * ``scale_source`` / ``scale_assumed``: a measured width or height, a depth scale, or an ASSUMED ceiling height. With an assumed height every dimension on the sheet scales with that assumption, and the title block says so in capitals. * ``partial``: the wall runs past the edge of the frame on a side, so the drawing is only the visible part. * ``fixtures``: switches, AC units and obstacles are listed as marks, not as dimensioned objects. * Perspective is corrected for a flat wall only. Openings are axis-aligned rectangles (a bay window, an arch or a rotated frame is simplified; ``skipped`` lists any shape too far from a rectangle to draw honestly). Pure numpy + cv2: no model is needed (the labels come from predict.py). """ from __future__ import annotations import json import math import cv2 import numpy as np from scipy import ndimage from wall_measure import AC, DOOR, MM_PER_FT, OBSTACLE, SWITCH, WINDOW RECT_MM_PER_PX = 10.0 MIN_OPENING_MM = 250.0 MIN_RECT_FILL = 0.6 # an opening's mask must fill at least this much of its bounding rectangle DISCLAIMER = "DRAFT generated from photographs. Verify every dimension on site before ordering, cutting or programming anything." def rectify(labels, quad_px, width_mm, height_mm, mm_per_px=RECT_MM_PER_PX): """The class mask warped from the photo onto the wall plane: a (height/mm_per_px, width/mm_per_px) map, 255 outside the photo.""" w, h = int(round(width_mm / mm_per_px)), int(round(height_mm / mm_per_px)) if w < 20 or h < 20: raise ValueError(f"the wall is {width_mm:.0f} x {height_mm:.0f} mm: too small to draw") src = np.float32(quad_px) # TL, TR, BR, BL in the photo dst = np.float32([[0, 0], [w, 0], [w, h], [0, h]]) m = cv2.getPerspectiveTransform(src, dst) return cv2.warpPerspective(np.asarray(labels).astype(np.uint8), m, (w, h), flags=cv2.INTER_NEAREST, borderValue=255) def _components(rect, cls, mm_per_px, wall_h, min_mm=MIN_OPENING_MM): comp, n = ndimage.label(rect == cls) boxes, skipped = [], [] for i in range(1, n + 1): ys, xs = np.nonzero(comp == i) x0, x1, y0, y1 = xs.min(), xs.max() + 1, ys.min(), ys.max() + 1 w_mm, h_mm = (x1 - x0) * mm_per_px, (y1 - y0) * mm_per_px if w_mm < min_mm or h_mm < min_mm: continue fill = len(xs) / float((x1 - x0) * (y1 - y0)) if fill < MIN_RECT_FILL: skipped.append({"kind": "window" if cls == WINDOW else "door" if cls == DOOR else "fixture", "x_mm": round(x0 * mm_per_px), "reason": f"shape fills only {fill:.0%} of its bounding rectangle (not a rectangle)"}) continue boxes.append((x0 * mm_per_px, (rect.shape[0] - y1) * mm_per_px, w_mm, h_mm)) # x, y from the BOTTOM-left, as drawing_match reads return sorted(boxes), skipped def make_elevation(labels, face, name="wall", mm_per_px=RECT_MM_PER_PX, source_note=None): """Drawing spec for one wall. ``face`` is one entry of wall_measure.measure_wall(...)["faces"].""" width_mm, height_mm = face["width_ft"] * MM_PER_FT, face["height_ft"] * MM_PER_FT quad = face.get("corners_px") if not quad or len(quad) != 4: raise ValueError("the wall measurement has no floor-to-ceiling corners, so there is nothing to rectify") rect = rectify(labels, quad, width_mm, height_mm, mm_per_px) seen = float((rect != 255).mean()) openings, skipped = [], [] k = {"window": 0, "door": 0} for cls, kind in ((WINDOW, "window"), (DOOR, "door")): boxes, sk = _components(rect, cls, mm_per_px, height_mm) skipped += sk for x, y, w, h in boxes: k[kind] += 1 openings.append({"id": f"{'W' if kind == 'window' else 'D'}{k[kind]}", "kind": kind, "x_mm": round(x), "y_mm": round(y), "w_mm": round(w), "h_mm": round(h)}) fixtures = [] for cls, kind in ((SWITCH, "switch/outlet"), (AC, "air conditioner"), (OBSTACLE, "obstacle")): boxes, _ = _components(rect, cls, mm_per_px, height_mm, min_mm=60.0) fixtures += [{"kind": kind, "x_mm": round(x), "y_mm": round(y), "w_mm": round(w), "h_mm": round(h)} for x, y, w, h in boxes] src = face.get("scale_source", "unknown") assumed = str(src).startswith("assumed") clipped = list(face.get("clipped_sides") or []) notes = [] if assumed: notes.append("DIMENSIONS ASSUME " + src.replace("assumed_ceiling_", "a ceiling height of ").replace("ft", " ft") + ": every dimension scales with that assumption. Measure the wall height and re-run.") if clipped: notes.append("the wall continues past the " + " and ".join(clipped) + " of the photo: this drawing is only the part that was visible") if seen < 0.9: notes.append(f"only {seen:.0%} of the wall rectangle was inside the photo") if source_note: notes.append(source_note) return {"name": name, "wall": {"width_mm": round(width_mm), "height_mm": round(height_mm)}, "openings": openings, "fixtures": fixtures, "provenance": {"generated_from": "photo", "scale_source": src, "scale_assumed": assumed, "partial": bool(clipped), "clipped_sides": clipped, "seen_fraction": round(seen, 3), "rect_mm_per_px": mm_per_px, "skipped": skipped, "notes": notes, "disclaimer": DISCLAIMER}} def make_elevation_from_twin(twin, name=None, source_note=None): """Drawing spec for the wall a DIGITAL TWIN holds: its size is whatever the twin's best source gave (scan, laser meter, fused photos), and its openings are read from the twin's fused surface map, so the sheet shows the whole wall rather than the one frame a single photo caught. The provenance says which source the dimensions rest on; the DRAFT warning stays (a twin is still camera-derived unless a scan/laser fixed it).""" import twin_model as tmod acc = twin.accuracy() src = acc["dimension_source"] W, H = twin.width_mm, twin.height_mm openings, fixtures, k = [], [], {"window": 0, "door": 0} for o in tmod.openings(twin): if o["kind"] in ("window", "door"): k[o["kind"]] += 1 openings.append({"id": f"{'W' if o['kind'] == 'window' else 'D'}{k[o['kind']]}", "kind": o["kind"], "x_mm": o["x_mm"], "y_mm": round(H - o["y_mm"] - o["h_mm"]), "w_mm": o["w_mm"], "h_mm": o["h_mm"]}) else: fixtures.append({"kind": {"switch": "switch/outlet", "unit": "air conditioner"}.get(o["kind"], o["kind"]), "x_mm": o["x_mm"], "y_mm": round(H - o["y_mm"] - o["h_mm"]), "w_mm": o["w_mm"], "h_mm": o["h_mm"]}) assumed = bool(twin.provenance.get("scale_assumed")) complete = twin.provenance.get("wall_complete") notes = [f"drawn from the digital twin: dimensions from {src}" + (f", {acc['views_fused']} photo view(s) fused" if acc.get("views_fused", 1) > 1 else "")] if assumed: notes.append("DIMENSIONS ASSUME " + str(twin.provenance.get("scale_source", "an assumed ceiling height")).replace("assumed_ceiling_", "a ceiling height of ").replace("ft", " ft") + ": every dimension scales with that assumption. Measure the wall height and rebuild the twin.") if complete is False: notes.append("the wall runs past the edge of what was photographed: this drawing is only the part that was seen") known = float((twin.surface != 255).mean()) if known < 0.9: notes.append(f"only {known:.0%} of the wall has been observed") if source_note: notes.append(source_note) return {"name": name or twin.name, "wall": {"width_mm": round(W), "height_mm": round(H)}, "openings": openings, "fixtures": fixtures, "provenance": {"generated_from": "twin", "scale_source": {"laser scan": "laser_scan", "laser distance meter": "laser"}.get(src, str(twin.provenance.get("scale_source", src))), "scale_assumed": assumed, "partial": complete is False, "clipped_sides": list(twin.provenance.get("clipped_sides") or []), "seen_fraction": round(known, 3), "rect_mm_per_px": twin.cell_mm, "skipped": [], "notes": notes, "disclaimer": DISCLAIMER}} def schedule(spec): """Opening schedule lines (sill height, head height, distance from the left end) and the net wall area, for the sheet and for anyone reading the JSON.""" W, H = spec["wall"]["width_mm"], spec["wall"]["height_mm"] rows = [] for o in spec["openings"]: rows.append({"id": o["id"], "kind": o["kind"], "w_mm": o["w_mm"], "h_mm": o["h_mm"], "sill_mm": o["y_mm"], "head_mm": o["y_mm"] + o["h_mm"], "from_left_mm": o["x_mm"], "area_m2": round(o["w_mm"] * o["h_mm"] / 1e6, 2)}) gross = W * H / 1e6 openings_area = sum(r["area_m2"] for r in rows) return {"rows": rows, "gross_m2": round(gross, 2), "openings_m2": round(openings_area, 2), "net_m2": round(max(gross - openings_area, 0), 2)} # ------------------------------------------------------------------------------------------------ outputs def _fmt(mm): return f"{mm / 1000:.2f} m" if mm >= 1000 else f"{mm:.0f} mm" def to_svg(spec, px_per_mm=0.1): """A dimensioned elevation: wall, openings (window cross, door swing), fixtures, overall and opening dimensions, and a title block that repeats the scale assumption and the DRAFT warning. y is drawn downward from the top of the wall.""" W, H = spec["wall"]["width_mm"], spec["wall"]["height_mm"] import textwrap s = px_per_mm my = 70 vw = max(W * s + 180, 760) # a narrow wall still gets a sheet wide enough for its notes mx = (vw - W * s) / 2 p = spec["provenance"] src = p["scale_source"] src_text = {"measured_height": "the wall height entered", "measured_width": "the wall width entered", "depth_scale": "a saved scale (mm per pixel)", "laser_scan": "a laser scan", "laser": "a laser distance meter"}.get( src, src.replace("assumed_ceiling_", "an assumed ceiling height of ").replace("ft", " ft")) chars = int((vw - 2 * 40) / 5.9) sched = schedule(spec) sched_lines = ([f"SCHEDULE gross {sched['gross_m2']:.2f} m2, openings {sched['openings_m2']:.2f} m2, net wall {sched['net_m2']:.2f} m2"] + [f"{r['id']} {r['kind']} {_fmt(r['w_mm'])} x {_fmt(r['h_mm'])} sill {_fmt(r['sill_mm'])} head {_fmt(r['head_mm'])} {_fmt(r['from_left_mm'])} from left" for r in sched["rows"][:14]]) body = [f"Scale from {src_text}" + (" (ASSUMED: check it)" if p["scale_assumed"] else "")] + p["notes"][:3] + [p["disclaimer"]] wrapped = [(ln, i) for i, t in enumerate(body) for ln in textwrap.wrap(t, chars)] vh = H * s + my + 56 + 30 + 14 * (len(wrapped) + len(sched_lines) + 1) + 20 out = [f'', f'', f'', f''] for o in spec["openings"]: x, y, w, h = o["x_mm"] * s, (H - o["y_mm"] - o["h_mm"]) * s, o["w_mm"] * s, o["h_mm"] * s out.append(f'') if o["kind"] == "window": out.append(f'') else: out.append(f'') out.append(f'{o["id"]}') out.append(f'{_fmt(o["w_mm"])} x {_fmt(o["h_mm"])}') for f in spec.get("fixtures", []): x, y, w, h = f["x_mm"] * s, (H - f["y_mm"] - f["h_mm"]) * s, max(f["w_mm"] * s, 4), max(f["h_mm"] * s, 4) out.append(f'') by = H * s + 26 out += [f'', f'{_fmt(W)}', f'', f'{_fmt(H)}'] xs = sorted({0, W} | {o["x_mm"] for o in spec["openings"]} | {o["x_mm"] + o["w_mm"] for o in spec["openings"]}) ty = -16 out.append(f'') for a, b in zip(xs[:-1], xs[1:]): if (b - a) * s > 46: out.append(f'{_fmt(b - a)}') out.append(f'') out.append("") ty0 = my + H * s + 56 out.append(f'{spec["name"]} · ELEVATION · DRAFT') for k, (ln, i) in enumerate(wrapped): warn = (i == 0 and p["scale_assumed"]) or ln.startswith("DIMENSIONS ASSUME") or i == len(body) - 1 out.append(f'{ln}') sy = ty0 + 18 + 14 * len(wrapped) + 8 for k, ln in enumerate(sched_lines): out.append(f'{ln}') out.append("") return "\n".join(out) def to_dxf(spec): """A minimal ASCII DXF (R12) in millimetres, y up from the base of the wall: layers WALL, WINDOW, DOOR, FIXTURE, TEXT. Opens in any CAD viewer.""" ents = [] def line(layer, x0, y0, x1, y1): ents.append(f"0\nLINE\n8\n{layer}\n10\n{x0:.1f}\n20\n{y0:.1f}\n30\n0.0\n11\n{x1:.1f}\n21\n{y1:.1f}\n31\n0.0") def rect(layer, x, y, w, h): for a, b in (((x, y), (x + w, y)), ((x + w, y), (x + w, y + h)), ((x + w, y + h), (x, y + h)), ((x, y + h), (x, y))): line(layer, *a, *b) def text(layer, x, y, h, t): ents.append(f"0\nTEXT\n8\n{layer}\n10\n{x:.1f}\n20\n{y:.1f}\n30\n0.0\n40\n{h:.1f}\n1\n{t}") W, H = spec["wall"]["width_mm"], spec["wall"]["height_mm"] rect("WALL", 0, 0, W, H) for o in spec["openings"]: rect("WINDOW" if o["kind"] == "window" else "DOOR", o["x_mm"], o["y_mm"], o["w_mm"], o["h_mm"]) text("TEXT", o["x_mm"] + 40, o["y_mm"] + o["h_mm"] / 2, 90, f'{o["id"]} {o["w_mm"]}x{o["h_mm"]}') for f in spec.get("fixtures", []): rect("FIXTURE", f["x_mm"], f["y_mm"], f["w_mm"], f["h_mm"]) text("TEXT", 0, -260, 110, f'{spec["name"]} - DRAFT from photographs, scale: {spec["provenance"]["scale_source"]}') text("TEXT", 0, -420, 90, spec["provenance"]["disclaimer"][:110]) layers = "".join(f"0\nLAYER\n2\n{n}\n70\n0\n62\n{c}\n6\nCONTINUOUS\n" for n, c in (("WALL", 7), ("WINDOW", 5), ("DOOR", 3), ("FIXTURE", 30), ("TEXT", 8))) return ("0\nSECTION\n2\nHEADER\n9\n$ACADVER\n1\nAC1009\n9\n$INSUNITS\n70\n4\n0\nENDSEC\n" f"0\nSECTION\n2\nTABLES\n0\nTABLE\n2\nLAYER\n70\n5\n{layers}0\nENDTAB\n0\nENDSEC\n" "0\nSECTION\n2\nENTITIES\n" + "\n".join(ents) + "\n0\nENDSEC\n0\nEOF\n") def to_png(spec, mm_per_px=15.0, line=3): """The sheet as a raster the console and drawing_match.drawing_from_image can read: black lines on white, wall rectangle with openings inside it, a door standing on the base line. Returns (BGR image, mm_per_px).""" W, H = spec["wall"]["width_mm"], spec["wall"]["height_mm"] pad = 40 img = np.full((int(H / mm_per_px) + 2 * pad, int(W / mm_per_px) + 2 * pad, 3), 255, np.uint8) x0, y0 = pad, pad cv2.rectangle(img, (x0, y0), (x0 + int(W / mm_per_px), y0 + int(H / mm_per_px)), (0, 0, 0), line) for o in spec["openings"]: a = (x0 + int(o["x_mm"] / mm_per_px), y0 + int((H - o["y_mm"] - o["h_mm"]) / mm_per_px)) b = (x0 + int((o["x_mm"] + o["w_mm"]) / mm_per_px), y0 + int((H - o["y_mm"]) / mm_per_px)) cv2.rectangle(img, a, b, (0, 0, 0), line) return img, mm_per_px def dumps(spec): return json.dumps(spec, indent=1)