Image Segmentation
PyTorch
English
painting-vision-robotics-kit
semantic-segmentation
robotics
edge-ai
construction-ai
autonomous-painting
wall-painting-robot
paint-coverage-estimation
building-facade
drywall
skirting-detection
window-detection
lidar
depth-validation
segformer
deeplabv3
Eval Results (legacy)
Upload work_order.py with huggingface_hub
Browse files- work_order.py +364 -0
work_order.py
ADDED
|
@@ -0,0 +1,364 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""One frame's prediction -> an ordered robot work order: prep, prime, rescan, paint, touch up.
|
| 3 |
+
|
| 4 |
+
``robot_planner.py`` plans the coat; it does not know that peeling paint must be
|
| 5 |
+
scraped and rust treated first, or that a nearly finished wall only needs its
|
| 6 |
+
touch-up patches. This sequences the whole job from what ``predict.py`` writes:
|
| 7 |
+
|
| 8 |
+
1. **gate** - ``safety_gate.assess`` with surface defects allowed (they become prep
|
| 9 |
+
tasks here). Anything else it rejects on (no wall, wallpaper, uncertainty, no
|
| 10 |
+
depth confirmation) blocks the whole order: no tasks are emitted.
|
| 11 |
+
2. **prep** - one task per damage region (``surface_condition.regions``), largest
|
| 12 |
+
first in travel order: method by type, the prep-zone polygon (the damage plus a
|
| 13 |
+
feather margin) and a serpentine path for the sanding/scraping tool over it.
|
| 14 |
+
3. **spot_prime** - the same zones with the primer that type needs, then a hold.
|
| 15 |
+
4. **rescan** - required after any prep: the damage keep-out only lifts when a new
|
| 16 |
+
frame shows sound wall, so the paint plan below is provisional until then.
|
| 17 |
+
5. **paint** - ``robot_planner.build_plan`` over the safe paintable mask (skipped
|
| 18 |
+
when the wall is already signed off complete).
|
| 19 |
+
6. **touch_up** - trim-tool strokes over the touch-up mask (remaining unconfirmed
|
| 20 |
+
patches), only when the wall is signed off (otherwise the coat covers them).
|
| 21 |
+
|
| 22 |
+
Units are image pixels; with a scale (``--mm-per-px``, or derived from the
|
| 23 |
+
measured face) tool sizes are given in mm and paths/areas are also reported in mm.
|
| 24 |
+
Hold times are placeholders: use the product data sheet.
|
| 25 |
+
|
| 26 |
+
Usage::
|
| 27 |
+
|
| 28 |
+
python3 predict.py frame.jpg --checkpoint artifacts/best.pt --json result.json
|
| 29 |
+
python3 work_order.py --prediction result.json --paintable safe_paintable_mask.png \\
|
| 30 |
+
--keepout keepout_mask.png --touchup touch_up_mask.png --depth-report validation.json \\
|
| 31 |
+
--out work_order.json --overlay work_order.png
|
| 32 |
+
"""
|
| 33 |
+
from __future__ import annotations
|
| 34 |
+
|
| 35 |
+
import argparse
|
| 36 |
+
import json
|
| 37 |
+
from pathlib import Path
|
| 38 |
+
|
| 39 |
+
import numpy as np
|
| 40 |
+
from PIL import Image, ImageDraw
|
| 41 |
+
|
| 42 |
+
from robot_planner import build_plan, path_length, plan_strokes, sequence_waypoints
|
| 43 |
+
from safety_gate import assess
|
| 44 |
+
from site_objects import required_objects
|
| 45 |
+
|
| 46 |
+
MM_PER_FT = 304.8
|
| 47 |
+
MM2_PER_FT2 = MM_PER_FT ** 2
|
| 48 |
+
PREP_METHOD = {
|
| 49 |
+
"paint_failure": {"method": "scrape loose paint, sand edges smooth, dust off",
|
| 50 |
+
"primer": "bonding primer over bare and feathered areas"},
|
| 51 |
+
"rust": {"method": "wire-brush/sand to sound metal or substrate, treat with rust converter",
|
| 52 |
+
"primer": "rust-inhibiting / stain-blocking primer"},
|
| 53 |
+
}
|
| 54 |
+
DEFAULT_PX = {"roller": 40.0, "trim": 12.0, "prep": 16.0, "margin": 6.0}
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
def scale_from_prediction(prediction):
|
| 58 |
+
"""``(mm_per_px, source)`` from the first measured face (height in ft over its height in px)."""
|
| 59 |
+
faces = ((prediction.get("measurements") or {}).get("faces")) or []
|
| 60 |
+
if not faces:
|
| 61 |
+
return None, None
|
| 62 |
+
face = faces[0]
|
| 63 |
+
quad = np.asarray(face.get("corners_px") or [], float)
|
| 64 |
+
if quad.shape != (4, 2) or not face.get("height_ft"):
|
| 65 |
+
return None, None
|
| 66 |
+
height_px = 0.5 * (np.linalg.norm(quad[3] - quad[0]) + np.linalg.norm(quad[2] - quad[1]))
|
| 67 |
+
if height_px <= 0:
|
| 68 |
+
return None, None
|
| 69 |
+
return face["height_ft"] * MM_PER_FT / height_px, f"measured face ({face.get('scale_source')})"
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
def polygon_mask(points, shape):
|
| 73 |
+
canvas = Image.new("1", (shape[1], shape[0]), 0)
|
| 74 |
+
if len(points) >= 3:
|
| 75 |
+
ImageDraw.Draw(canvas).polygon([tuple(p) for p in points], fill=1, outline=1)
|
| 76 |
+
return np.array(canvas, bool) # writable: the bbox fallback fills it
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
def travel_order(regions):
|
| 80 |
+
"""Greedy nearest-neighbour order of region centres, starting top-left."""
|
| 81 |
+
remaining = list(range(len(regions)))
|
| 82 |
+
order, here = [], np.array([0.0, 0.0])
|
| 83 |
+
while remaining:
|
| 84 |
+
nearest = min(remaining, key=lambda i: float(np.linalg.norm(np.asarray(regions[i]["center_xy"]) - here)))
|
| 85 |
+
order.append(nearest)
|
| 86 |
+
remaining.remove(nearest)
|
| 87 |
+
here = np.asarray(regions[nearest]["center_xy"], float)
|
| 88 |
+
return [regions[i] for i in order]
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
def strokes_json(segments):
|
| 92 |
+
return [[[round(float(x), 1), round(float(y), 1)] for x, y in s] for s in segments]
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
# Checks an on-site operator may sign for after looking at the wall. Each maps to a gate
|
| 96 |
+
# relaxation; the confirmation (who, when) is recorded in the order, never silently assumed.
|
| 97 |
+
CONFIRMATIONS = {
|
| 98 |
+
"not_wallpaper": ({"allow_wallpaper": True}, "operator checked: the surface is not wallpaper"),
|
| 99 |
+
"depth_waived": ({"require_depth": False}, "operator waived depth confirmation (no LiDAR on this run)"),
|
| 100 |
+
"uncertainty_reviewed": ({"max_uncertain_fraction": 1.0},
|
| 101 |
+
"operator reviewed the uncertain wall areas in the image"),
|
| 102 |
+
"colour_checked": ({"max_color_delta_e": 1e9},
|
| 103 |
+
"operator checked: the colour on the wall is the intended one"),
|
| 104 |
+
}
|
| 105 |
+
EDIT_ADD, EDIT_EXCLUDE = 1, 2
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
def apply_edits(paintable, keepout, edits, wall=None):
|
| 109 |
+
"""Operator corrections: ``edits`` is 0 (none), 1 (paint here), 2 (do not paint).
|
| 110 |
+
|
| 111 |
+
"Paint here" lifts keep-out under it; "do not paint" becomes keep-out. Returns
|
| 112 |
+
``(paintable, keepout, stats)``; ``stats`` counts what changed so the order can
|
| 113 |
+
say how much of the plan is the operator's call rather than the model's.
|
| 114 |
+
"""
|
| 115 |
+
edits = np.asarray(edits)
|
| 116 |
+
if edits.shape != paintable.shape:
|
| 117 |
+
raise ValueError(f"edit layer {edits.shape} does not match the masks {paintable.shape}")
|
| 118 |
+
add, exclude = edits == EDIT_ADD, edits == EDIT_EXCLUDE
|
| 119 |
+
stats = {"added_px": int((add & ~paintable).sum()), "excluded_px": int((exclude & paintable).sum()),
|
| 120 |
+
"keepout_lifted_px": int((add & keepout).sum())}
|
| 121 |
+
if wall is not None:
|
| 122 |
+
stats["added_off_wall_px"] = int((add & ~np.asarray(wall, bool)).sum())
|
| 123 |
+
return (paintable | add) & ~exclude, (keepout & ~add) | exclude, stats
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
def build_work_order(prediction, paintable, keepout, touchup=None, depth=None, require_depth=True,
|
| 127 |
+
mm_per_px=None, scale_source=None, tools_mm=None, tools_px=None, overlap=0.2,
|
| 128 |
+
orientation="horizontal", prime_hold_min=60, prep_overlap=0.3, confirmations=None,
|
| 129 |
+
edit_stats=None):
|
| 130 |
+
"""Return ``(order, debug)``; ``order`` is JSON-safe.
|
| 131 |
+
|
| 132 |
+
``confirmations``: {name: {"by": operator, "at": iso time}} for names in ``CONFIRMATIONS``.
|
| 133 |
+
"""
|
| 134 |
+
tools_mm = tools_mm or {}
|
| 135 |
+
px = dict(DEFAULT_PX, **(tools_px or {}))
|
| 136 |
+
if mm_per_px:
|
| 137 |
+
for key, value in tools_mm.items():
|
| 138 |
+
if value:
|
| 139 |
+
px[key] = value / mm_per_px
|
| 140 |
+
warnings = []
|
| 141 |
+
if not mm_per_px:
|
| 142 |
+
warnings.append("no scale: tool sizes are pixel placeholders and nothing is reported in mm")
|
| 143 |
+
|
| 144 |
+
def mm(value_px, power=1):
|
| 145 |
+
return round(value_px * mm_per_px ** power, 1) if mm_per_px else None
|
| 146 |
+
|
| 147 |
+
limits = {"allow_surface_defects": True, "require_depth": require_depth}
|
| 148 |
+
confirmed = []
|
| 149 |
+
for name, record in (confirmations or {}).items():
|
| 150 |
+
if name not in CONFIRMATIONS:
|
| 151 |
+
raise ValueError(f"unknown confirmation {name!r}; expected one of {sorted(CONFIRMATIONS)}")
|
| 152 |
+
if not (record or {}).get("by"):
|
| 153 |
+
raise ValueError(f"confirmation {name!r} needs the operator's name ('by')")
|
| 154 |
+
limits.update(CONFIRMATIONS[name][0])
|
| 155 |
+
confirmed.append({"name": name, "meaning": CONFIRMATIONS[name][1], **record})
|
| 156 |
+
gate = assess(prediction, depth, limits)
|
| 157 |
+
order = {"status": None, "gate": gate, "scale": {"mm_per_px": round(mm_per_px, 4) if mm_per_px else None,
|
| 158 |
+
"source": scale_source},
|
| 159 |
+
"tool_width_px": {k: round(v, 1) for k, v in px.items()}, "warnings": warnings + gate.get("warnings", []),
|
| 160 |
+
"confirmations": confirmed, "operator_edits": edit_stats, "steps": []}
|
| 161 |
+
if edit_stats and (edit_stats.get("added_px") or edit_stats.get("keepout_lifted_px")):
|
| 162 |
+
order["warnings"].append(f"operator added {edit_stats['added_px']} px to the paint area and lifted "
|
| 163 |
+
f"{edit_stats['keepout_lifted_px']} px of keep-out; those areas are the "
|
| 164 |
+
"operator's call, not the model's")
|
| 165 |
+
debug = {"prep": [], "paint": None, "touch_up": []}
|
| 166 |
+
paint_px = int((paintable & ~keepout).sum())
|
| 167 |
+
paint_ft2 = round(paint_px * mm_per_px ** 2 / MM2_PER_FT2, 1) if mm_per_px else None
|
| 168 |
+
if gate["decision"] != "approve":
|
| 169 |
+
order["status"] = "blocked"
|
| 170 |
+
order["summary"] = {"paint_area_px": paint_px, "paint_area_ft2": paint_ft2, "prep_regions": 0,
|
| 171 |
+
"prep_area_px": 0, "touch_up_patches": 0, "steps": []}
|
| 172 |
+
return order, debug
|
| 173 |
+
|
| 174 |
+
shape = paintable.shape
|
| 175 |
+
condition = prediction.get("surface_condition") or {}
|
| 176 |
+
|
| 177 |
+
# What has to be on site before any paint goes on: the tools the plan will use, the colour
|
| 178 |
+
# the job specifies, and nothing standing where the coat is going.
|
| 179 |
+
plan_tools = ["roller"] + (["trim"] if px["trim"] else [])
|
| 180 |
+
objects = prediction.get("site_objects") or {}
|
| 181 |
+
colour = prediction.get("paint_color") or {}
|
| 182 |
+
spec = {"plan_tools": plan_tools, "needs_on_site": required_objects(plan_tools),
|
| 183 |
+
"tools_present": objects.get("present"), "missing_tools": objects.get("missing_tools"),
|
| 184 |
+
"coat_colour": colour.get("dominant"), "specified": (colour.get("reference") or {}).get("reference"),
|
| 185 |
+
"colour_verdict": (colour.get("reference") or {}).get("verdict"),
|
| 186 |
+
"tin_check": (objects.get("reference_check") or {}).get("status")}
|
| 187 |
+
if any(value is not None for key, value in spec.items() if key not in ("plan_tools", "needs_on_site")):
|
| 188 |
+
order["paint_spec"] = spec
|
| 189 |
+
setup_tasks = []
|
| 190 |
+
if spec.get("missing_tools"):
|
| 191 |
+
setup_tasks.append({"task": "stage-tools", "missing": spec["missing_tools"],
|
| 192 |
+
"detail": f"the plan uses {', '.join(plan_tools)}; "
|
| 193 |
+
f"{', '.join(spec['missing_tools'])} could not be seen in this frame"})
|
| 194 |
+
for item in objects.get("in_paint_zone") or []:
|
| 195 |
+
setup_tasks.append({"task": "clear-paint-area", "what": item["class"], "bbox_px": item["bbox_xyxy"],
|
| 196 |
+
"detail": "inside the paint area: move it or work around it before the pass"})
|
| 197 |
+
if spec.get("colour_verdict") and spec["colour_verdict"] != "match":
|
| 198 |
+
setup_tasks.append({"task": "check-colour", "verdict": spec["colour_verdict"],
|
| 199 |
+
"coat": spec.get("coat_colour"), "specified": spec.get("specified"),
|
| 200 |
+
"detail": "the coat does not match the specified colour: confirm the tin first"})
|
| 201 |
+
if setup_tasks:
|
| 202 |
+
order["steps"].append({"step": "setup", "tasks": setup_tasks,
|
| 203 |
+
"note": "this is what the camera could see, not a stock check: a tool that is "
|
| 204 |
+
"out of frame cannot be judged"})
|
| 205 |
+
regions = condition.get("regions") or []
|
| 206 |
+
if condition.get("status") == "defects_detected" and not regions:
|
| 207 |
+
order["warnings"].append("defects detected but no regions reported (older predict.py?): "
|
| 208 |
+
"prep the areas in surface_condition_mask.png by hand")
|
| 209 |
+
if condition.get("regions_total", len(regions)) > len(regions):
|
| 210 |
+
order["warnings"].append(f"{condition['regions_total'] - len(regions)} smaller damage regions not listed; "
|
| 211 |
+
"they stay in the keep-out")
|
| 212 |
+
prep_tasks, prime_tasks = [], []
|
| 213 |
+
spacing = max(1.0, px["prep"] * (1 - prep_overlap))
|
| 214 |
+
for n, region in enumerate(travel_order(regions), 1):
|
| 215 |
+
zone = polygon_mask(region.get("prep_zone_xy") or [], shape)
|
| 216 |
+
if not zone.any(): # degenerate prep polygon: damage outline, then the box
|
| 217 |
+
zone = polygon_mask(region.get("outline_xy") or [], shape)
|
| 218 |
+
if not zone.any():
|
| 219 |
+
x0, y0, x1, y1 = region["bbox_xyxy"]
|
| 220 |
+
zone[y0:y1 + 1, x0:x1 + 1] = True
|
| 221 |
+
segments = plan_strokes(zone, spacing, orientation, min_run=1)
|
| 222 |
+
debug["prep"].append((zone, segments))
|
| 223 |
+
kind = region.get("type", "paint_failure")
|
| 224 |
+
area = int(zone.sum())
|
| 225 |
+
task = {"task": f"prep-{n}", "type": kind, "method": PREP_METHOD.get(kind, PREP_METHOD["paint_failure"])["method"],
|
| 226 |
+
"zone_polygon_px": region.get("prep_zone_xy"), "damage_polygon_px": region.get("outline_xy"),
|
| 227 |
+
"center_px": region["center_xy"], "zone_area_px": area, "zone_area_mm2": mm(area, 2),
|
| 228 |
+
"confidence": {"mean": region.get("mean_confidence"), "peak": region.get("peak_confidence")},
|
| 229 |
+
"tool": "sander/scraper", "passes": len(segments), "strokes_px": strokes_json(segments),
|
| 230 |
+
"path_length_px": round(path_length(segments), 1), "path_length_mm": mm(path_length(segments))}
|
| 231 |
+
prep_tasks.append(task)
|
| 232 |
+
prime_tasks.append({"task": f"prime-{n}", "after": task["task"], "type": kind,
|
| 233 |
+
"primer": PREP_METHOD.get(kind, PREP_METHOD["paint_failure"])["primer"],
|
| 234 |
+
"zone_polygon_px": task["zone_polygon_px"], "zone_area_px": area,
|
| 235 |
+
"zone_area_mm2": task["zone_area_mm2"], "tool": "trim brush/roller"})
|
| 236 |
+
if prep_tasks:
|
| 237 |
+
order["steps"].append({"step": "prep", "tasks": prep_tasks,
|
| 238 |
+
"waypoints": sequence_waypoints([(t["task"], s) for t, (_, s) in
|
| 239 |
+
zip(prep_tasks, debug["prep"])])})
|
| 240 |
+
order["steps"].append({"step": "spot_prime", "tasks": prime_tasks, "hold_minutes": prime_hold_min,
|
| 241 |
+
"note": "hold time is a placeholder: follow the primer's data sheet"})
|
| 242 |
+
order["steps"].append({"step": "rescan", "required": True,
|
| 243 |
+
"reason": "the damage keep-out lifts only when a new frame shows sound, primed wall; "
|
| 244 |
+
"re-run predict.py and rebuild this order before the coat"})
|
| 245 |
+
|
| 246 |
+
completion = prediction.get("completion") or {}
|
| 247 |
+
signed_off = completion.get("status") == "complete"
|
| 248 |
+
if signed_off:
|
| 249 |
+
order["steps"].append({"step": "paint", "skipped": "wall signed off complete"})
|
| 250 |
+
elif paintable.any():
|
| 251 |
+
try:
|
| 252 |
+
plan, plan_debug = build_plan(paintable, keepout, px["roller"], overlap, px["margin"], orientation,
|
| 253 |
+
None, mm_per_px, 8.0, trim_width=px["trim"])
|
| 254 |
+
debug["paint"] = plan_debug
|
| 255 |
+
order["steps"].append({"step": "paint", "provisional": bool(prep_tasks), "plan": plan})
|
| 256 |
+
order["warnings"].extend(plan["warnings"])
|
| 257 |
+
except ValueError as exc:
|
| 258 |
+
order["steps"].append({"step": "paint", "skipped": str(exc)})
|
| 259 |
+
else:
|
| 260 |
+
order["steps"].append({"step": "paint", "skipped": "no safe paintable wall"})
|
| 261 |
+
|
| 262 |
+
if touchup is not None and touchup.any() and signed_off:
|
| 263 |
+
from scipy import ndimage
|
| 264 |
+
labels, count = ndimage.label(touchup & ~keepout, structure=np.ones((3, 3), bool))
|
| 265 |
+
tasks = []
|
| 266 |
+
for i in range(1, count + 1):
|
| 267 |
+
patch = labels == i
|
| 268 |
+
segments = plan_strokes(patch, max(1.0, px["trim"] * (1 - overlap)), orientation, min_run=1)
|
| 269 |
+
if not segments:
|
| 270 |
+
continue
|
| 271 |
+
debug["touch_up"].append((patch, segments))
|
| 272 |
+
ys, xs = np.nonzero(patch)
|
| 273 |
+
tasks.append({"task": f"touch-{len(tasks) + 1}", "bbox_px": [int(xs.min()), int(ys.min()),
|
| 274 |
+
int(xs.max()), int(ys.max())],
|
| 275 |
+
"area_px": int(patch.sum()), "area_mm2": mm(int(patch.sum()), 2), "tool": "trim",
|
| 276 |
+
"strokes_px": strokes_json(segments)})
|
| 277 |
+
if tasks:
|
| 278 |
+
order["steps"].append({"step": "touch_up", "tasks": tasks,
|
| 279 |
+
"note": "small patches the sign-off could not confirm as painted"})
|
| 280 |
+
|
| 281 |
+
order["status"] = ("prep_required" if prep_tasks else
|
| 282 |
+
"touch_up_only" if signed_off and debug["touch_up"] else
|
| 283 |
+
"complete" if signed_off else "ready_to_paint")
|
| 284 |
+
order["summary"] = {"paint_area_px": paint_px, "paint_area_ft2": paint_ft2,
|
| 285 |
+
"prep_regions": len(prep_tasks),
|
| 286 |
+
"prep_area_px": sum(t["zone_area_px"] for t in prep_tasks),
|
| 287 |
+
"touch_up_patches": len(debug["touch_up"]),
|
| 288 |
+
"steps": [s["step"] for s in order["steps"]]}
|
| 289 |
+
return order, debug
|
| 290 |
+
|
| 291 |
+
|
| 292 |
+
def render(image_shape, paintable, keepout, debug, path, background=None):
|
| 293 |
+
h, w = image_shape
|
| 294 |
+
canvas = np.zeros((h, w, 3), np.uint8) if background is None else (np.asarray(background) * 0.5).astype(np.uint8)
|
| 295 |
+
canvas[paintable] = canvas[paintable] // 2 + np.array([20, 90, 20], np.uint8)
|
| 296 |
+
canvas[keepout] = canvas[keepout] // 2 + np.array([90, 20, 20], np.uint8)
|
| 297 |
+
image = Image.fromarray(canvas)
|
| 298 |
+
draw = ImageDraw.Draw(image)
|
| 299 |
+
for zone, segments in debug["prep"]:
|
| 300 |
+
ys, xs = np.nonzero(zone)
|
| 301 |
+
if len(xs):
|
| 302 |
+
draw.rectangle([int(xs.min()), int(ys.min()), int(xs.max()), int(ys.max())], outline=(255, 140, 0), width=2)
|
| 303 |
+
for (x0, y0), (x1, y1) in segments:
|
| 304 |
+
draw.line([(x0, y0), (x1, y1)], fill=(255, 200, 0), width=1)
|
| 305 |
+
if debug["paint"] is not None:
|
| 306 |
+
for (x0, y0), (x1, y1) in debug["paint"]["roller"]:
|
| 307 |
+
draw.line([(x0, y0), (x1, y1)], fill=(80, 200, 255), width=2)
|
| 308 |
+
for (x0, y0), (x1, y1) in debug["paint"]["trim"]:
|
| 309 |
+
draw.line([(x0, y0), (x1, y1)], fill=(200, 120, 255), width=1)
|
| 310 |
+
for _, segments in debug["touch_up"]:
|
| 311 |
+
for (x0, y0), (x1, y1) in segments:
|
| 312 |
+
draw.line([(x0, y0), (x1, y1)], fill=(0, 255, 255), width=1)
|
| 313 |
+
Path(path).parent.mkdir(parents=True, exist_ok=True)
|
| 314 |
+
image.save(path)
|
| 315 |
+
|
| 316 |
+
|
| 317 |
+
def load_binary(path):
|
| 318 |
+
return np.asarray(Image.open(path).convert("L")) > 127
|
| 319 |
+
|
| 320 |
+
|
| 321 |
+
def main():
|
| 322 |
+
ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
|
| 323 |
+
ap.add_argument("--prediction", type=Path, required=True, help="predict.py --json output")
|
| 324 |
+
ap.add_argument("--paintable", type=Path, required=True, help="safe paintable mask from predict.py")
|
| 325 |
+
ap.add_argument("--keepout", type=Path, required=True, help="keep-out mask from predict.py")
|
| 326 |
+
ap.add_argument("--touchup", type=Path, help="touch-up mask from predict.py")
|
| 327 |
+
ap.add_argument("--depth-report", type=Path, help="depth_validation.py report (required unless --no-require-depth)")
|
| 328 |
+
ap.add_argument("--no-require-depth", action="store_true")
|
| 329 |
+
ap.add_argument("--mm-per-px", type=float, help="scale at the wall; default: derived from the measured face")
|
| 330 |
+
ap.add_argument("--roller-mm", type=float, default=230.0)
|
| 331 |
+
ap.add_argument("--trim-mm", type=float, default=60.0)
|
| 332 |
+
ap.add_argument("--prep-tool-mm", type=float, default=125.0, help="sanding pad / scraper width")
|
| 333 |
+
ap.add_argument("--margin-mm", type=float, default=40.0)
|
| 334 |
+
ap.add_argument("--overlap", type=float, default=0.2)
|
| 335 |
+
ap.add_argument("--orientation", choices=("horizontal", "vertical"), default="horizontal")
|
| 336 |
+
ap.add_argument("--prime-hold-min", type=float, default=60.0)
|
| 337 |
+
ap.add_argument("--image", type=Path, help="frame to draw the overlay on")
|
| 338 |
+
ap.add_argument("--out", type=Path, help="write the work order JSON here")
|
| 339 |
+
ap.add_argument("--overlay", type=Path, help="write a visualisation PNG here")
|
| 340 |
+
args = ap.parse_args()
|
| 341 |
+
|
| 342 |
+
prediction = json.loads(args.prediction.read_text())
|
| 343 |
+
paintable, keepout = load_binary(args.paintable), load_binary(args.keepout)
|
| 344 |
+
touchup = load_binary(args.touchup) if args.touchup else None
|
| 345 |
+
depth = json.loads(args.depth_report.read_text()) if args.depth_report else None
|
| 346 |
+
mm_per_px, source = (args.mm_per_px, "flag") if args.mm_per_px else scale_from_prediction(prediction)
|
| 347 |
+
order, debug = build_work_order(
|
| 348 |
+
prediction, paintable, keepout, touchup, depth, require_depth=not args.no_require_depth,
|
| 349 |
+
mm_per_px=mm_per_px, scale_source=source, overlap=args.overlap, orientation=args.orientation,
|
| 350 |
+
tools_mm={"roller": args.roller_mm, "trim": args.trim_mm, "prep": args.prep_tool_mm, "margin": args.margin_mm},
|
| 351 |
+
prime_hold_min=args.prime_hold_min)
|
| 352 |
+
text = json.dumps(order, indent=2)
|
| 353 |
+
if args.out:
|
| 354 |
+
args.out.write_text(text + "\n")
|
| 355 |
+
print(json.dumps({"status": order["status"], "summary": order.get("summary"),
|
| 356 |
+
"gate_reasons": order["gate"]["reasons"], "warnings": order["warnings"]}, indent=2))
|
| 357 |
+
if args.overlay:
|
| 358 |
+
background = np.asarray(Image.open(args.image).convert("RGB")) if args.image else None
|
| 359 |
+
render(paintable.shape, paintable, keepout, debug, args.overlay, background)
|
| 360 |
+
raise SystemExit(0 if order["status"] != "blocked" else 1)
|
| 361 |
+
|
| 362 |
+
|
| 363 |
+
if __name__ == "__main__":
|
| 364 |
+
main()
|