constructelligence commited on
Commit
599f09e
·
verified ·
1 Parent(s): 17d2de1

Upload safety_gate.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. safety_gate.py +181 -4
safety_gate.py CHANGED
@@ -5,7 +5,13 @@ A perception stack that always answers is dangerous. This gate combines the
5
  semantic prediction with the independent depth check and returns a single
6
  **approve / reject** decision with the exact reasons. It fails closed: missing
7
  evidence (no wall, high uncertainty, no depth confirmation, unaccounted
8
- protrusions) means *do not paint*, never "assume it is fine".
 
 
 
 
 
 
9
 
10
  Inputs are the JSON outputs already produced by the pipeline:
11
  ``predict.py --json`` (``--prediction``) and ``depth_validate.py --report``
@@ -15,6 +21,7 @@ Usage::
15
 
16
  python3 safety_gate.py --prediction result.json --depth-report validation.json
17
  python3 safety_gate.py --prediction result.json --no-require-depth
 
18
  """
19
  from __future__ import annotations
20
 
@@ -29,18 +36,118 @@ DEFAULT_THRESHOLDS = {
29
  "max_missed_protrusion_fraction": 0.01,
30
  "max_missed_recess_fraction": 0.01,
31
  "require_depth": True,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  }
33
 
34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  def _number(mapping, key, default=None):
36
  value = mapping.get(key, default)
37
  return value if isinstance(value, (int, float)) else default
38
 
39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  def assess(prediction, depth=None, thresholds=None):
41
  """Return ``{"decision", "reasons", "checks"}``; ``decision`` is approve/reject."""
42
  limits = {**DEFAULT_THRESHOLDS, **(thresholds or {})}
43
- reasons, checks = [], {}
44
 
45
  wall_pixels = int(_number(prediction, "wall_pixels", 0) or 0)
46
  checks["wall_pixels"] = wall_pixels
@@ -55,6 +162,22 @@ def assess(prediction, depth=None, thresholds=None):
55
  if prediction.get("inspection_status") == "reobserve_low_confidence":
56
  reasons.append("perception requested re-observation (low confidence)")
57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
  if depth is None:
59
  checks["depth_confirmed"] = False
60
  if limits["require_depth"]:
@@ -80,6 +203,36 @@ def assess(prediction, depth=None, thresholds=None):
80
  if fraction > limits[limit_key]:
81
  reasons.append(f"depth shows unaccounted {label} covering {fraction:.1%} of the wall keep-out")
82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83
  return {"decision": "reject" if reasons else "approve", "reasons": reasons, "checks": checks}
84
 
85
 
@@ -91,17 +244,41 @@ def main():
91
  help="do not reject when depth confirmation is missing (less safe)")
92
  parser.add_argument("--max-uncertain-fraction", type=float)
93
  parser.add_argument("--min-plane-fraction", type=float)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94
  args = parser.parse_args()
95
 
96
- thresholds = {"require_depth": not args.no_require_depth}
 
 
 
 
 
97
  if args.max_uncertain_fraction is not None:
98
  thresholds["max_uncertain_fraction"] = args.max_uncertain_fraction
99
  if args.min_plane_fraction is not None:
100
  thresholds["min_plane_fraction"] = args.min_plane_fraction
 
 
101
 
102
  prediction = json.loads(args.prediction.read_text(encoding="utf-8"))
103
  depth = json.loads(args.depth_report.read_text(encoding="utf-8")) if args.depth_report else None
104
- result = assess(prediction, depth, thresholds)
105
  print(json.dumps(result, indent=2))
106
  raise SystemExit(0 if result["decision"] == "approve" else 1)
107
 
 
5
  semantic prediction with the independent depth check and returns a single
6
  **approve / reject** decision with the exact reasons. It fails closed: missing
7
  evidence (no wall, high uncertainty, no depth confirmation, unaccounted
8
+ protrusions, possible wallpaper, peeling paint or rust, unassessed surface
9
+ condition) means *do not paint*, never "assume it is fine".
10
+
11
+ ``--signoff`` asks the other question: may the robot declare this wall finished
12
+ and move on? It approves only when ``predict.py`` reports ``completion.status ==
13
+ "complete"``, which needs a checkpoint whose completion threshold was validated
14
+ (``train.py`` stores it); otherwise a person inspects the wall.
15
 
16
  Inputs are the JSON outputs already produced by the pipeline:
17
  ``predict.py --json`` (``--prediction``) and ``depth_validate.py --report``
 
21
 
22
  python3 safety_gate.py --prediction result.json --depth-report validation.json
23
  python3 safety_gate.py --prediction result.json --no-require-depth
24
+ python3 safety_gate.py --prediction after_pass.json --signoff
25
  """
26
  from __future__ import annotations
27
 
 
36
  "max_missed_protrusion_fraction": 0.01,
37
  "max_missed_recess_fraction": 0.01,
38
  "require_depth": True,
39
+ # Painting over wallpaper is a different job (strip or seal first). A model
40
+ # detection or a pattern/seam cue both stop the robot until someone confirms.
41
+ "allow_wallpaper": False,
42
+ # Peeling paint and rust need preparing (scrape, treat, prime) before a coat.
43
+ "allow_surface_defects": False,
44
+ # A checkpoint without a condition head cannot see paint failure: unknown is not "fine".
45
+ "require_condition": True,
46
+ # The colour-only rust cue scored near chance on 117 reviewed real photos (AUROC
47
+ # ~0.6; it cannot tell a rusted close-up from brick), so on its own it warns
48
+ # rather than stops. The learned condition head is the detector.
49
+ "strict_rust_cue": False,
50
+ # The colour on the wall, and the colour on the tins on site, against the specified one.
51
+ # A wrong colour found after a coat has dried is a repaint, so a mismatch is a reason to
52
+ # stop; a loose job can widen --color-tolerance in predict.py, and this gate honours the
53
+ # verdict predict.py reached. Set max_color_delta_e here to apply the gate's own limit.
54
+ "max_color_delta_e": None,
55
+ # Equipment standing in the paint area. Painting over a customer's tin is a call-back, but
56
+ # the cue detector has known false positives, so by default it warns instead of stopping.
57
+ "strict_objects_in_paint_zone": False,
58
+ # Are the tools the plan needs on site? A missing roller wastes a setup, not a wall.
59
+ "require_tools_on_site": False,
60
  }
61
 
62
 
63
+ def _condition_reasons(prediction, limits, checks, warnings):
64
+ condition = prediction.get("surface_condition")
65
+ status = (condition or {}).get("status")
66
+ checks["surface_condition_status"] = status
67
+ if limits["allow_surface_defects"]:
68
+ return []
69
+ if status == "defects_detected":
70
+ parts = [f"{name.replace('_', ' ')} on {condition[name]['fraction_of_wall']:.1%} of the wall"
71
+ for name in condition.get("defects", []) if name in condition]
72
+ return [f"surface needs preparation before painting: {', '.join(parts) or 'defects detected'}"]
73
+ weak = (condition or {}).get("weak_damage_signal")
74
+ if weak:
75
+ warnings.append(f"faint paint-failure/rust signal on {weak:.1%} of the wall, below the calibrated "
76
+ "threshold; look before painting")
77
+ if status == "possible_rust":
78
+ score = (condition.get("cue") or {}).get("score")
79
+ message = f"possible rust (colour cue {score}); inspect and treat before painting"
80
+ if limits["strict_rust_cue"]:
81
+ return [message]
82
+ warnings.append(message)
83
+ return []
84
+ if limits["require_condition"] and status in (None, "not_assessed"):
85
+ return ["surface condition not assessed (no condition head in this checkpoint); "
86
+ "inspect for peeling paint and rust"]
87
+ return []
88
+
89
+
90
  def _number(mapping, key, default=None):
91
  value = mapping.get(key, default)
92
  return value if isinstance(value, (int, float)) else default
93
 
94
 
95
+ def _color_reasons(prediction, limits, checks, warnings):
96
+ """The coat (and the tins on site) against the specified colour, when one was given."""
97
+ colour = prediction.get("paint_color") or {}
98
+ objects = prediction.get("site_objects") or {}
99
+ reference = colour.get("reference")
100
+ checks["paint_color_status"] = colour.get("status")
101
+ checks["paint_color_reference"] = (reference or {}).get("verdict")
102
+ reasons = []
103
+ if reference:
104
+ measured = colour.get("dominant") or {}
105
+ delta = _number(reference, "delta_e")
106
+ message = (f"the coat is {measured.get('name')} {measured.get('hex')}, ΔE00 {delta} from the "
107
+ f"specified {(reference.get('reference') or {}).get('name')} "
108
+ f"{(reference.get('reference') or {}).get('hex')}")
109
+ limit = limits["max_color_delta_e"]
110
+ if reference.get("passes"):
111
+ if colour.get("multi_colour"):
112
+ warnings.append(f"{message}: a second colour is also on the wall, check the faces")
113
+ elif limit is not None and delta is not None and delta <= limit:
114
+ warnings.append(message)
115
+ else:
116
+ reasons.append(f"{message}: check the tin before more coat goes on")
117
+ tin = (objects.get("reference_check") or {})
118
+ checks["tins_on_site"] = tin.get("status")
119
+ if tin.get("status") == "tin_differs_from_spec":
120
+ closest = min(tin.get("tins") or [{"delta_e": None}], key=lambda item: item.get("delta_e") or 1e9)
121
+ reasons.append(f"the tins on site do not match the specified colour "
122
+ f"{(tin.get('reference') or {}).get('name')} (closest ΔE00 {closest.get('delta_e')}): "
123
+ "check the product before painting")
124
+ return reasons
125
+
126
+
127
+ def _object_reasons(prediction, limits, checks, warnings):
128
+ """Equipment found on site: what is in the way, and what the plan needs but cannot see."""
129
+ objects = prediction.get("site_objects") or {}
130
+ checks["site_objects_status"] = objects.get("status")
131
+ checks["equipment_present"] = objects.get("present")
132
+ checks["missing_tools"] = objects.get("missing_tools")
133
+ reasons = []
134
+ clutter = objects.get("in_paint_zone") or []
135
+ if clutter:
136
+ named = ", ".join(sorted({str(item["class"]).replace("_", " ") for item in clutter}))
137
+ message = (f"equipment is standing in the paint area ({named}); it stays out of the plan but "
138
+ "check the way through")
139
+ (reasons if limits["strict_objects_in_paint_zone"] else warnings).append(message)
140
+ missing = objects.get("missing_tools") or []
141
+ if missing:
142
+ message = f"the plan needs {', '.join(missing)} and none is visible on site"
143
+ (reasons if limits["require_tools_on_site"] else warnings).append(message)
144
+ return reasons
145
+
146
+
147
  def assess(prediction, depth=None, thresholds=None):
148
  """Return ``{"decision", "reasons", "checks"}``; ``decision`` is approve/reject."""
149
  limits = {**DEFAULT_THRESHOLDS, **(thresholds or {})}
150
+ reasons, checks, warnings = [], {}, []
151
 
152
  wall_pixels = int(_number(prediction, "wall_pixels", 0) or 0)
153
  checks["wall_pixels"] = wall_pixels
 
162
  if prediction.get("inspection_status") == "reobserve_low_confidence":
163
  reasons.append("perception requested re-observation (low confidence)")
164
 
165
+ wallpaper = prediction.get("wallpaper") or {}
166
+ status = wallpaper.get("status")
167
+ checks["wallpaper_status"] = status
168
+ if not limits["allow_wallpaper"]:
169
+ if status == "wallpaper_detected":
170
+ fraction = _number(wallpaper, "fraction_of_wall", 0.0) or 0.0
171
+ reasons.append(f"wallpaper covers {fraction:.0%} of the wall; strip or seal before painting")
172
+ elif status == "possible_wallpaper":
173
+ cue = _number(wallpaper.get("cue") or {}, "score")
174
+ reasons.append(f"possible wallpaper (pattern/seam cue {cue if cue is not None else '?'}); "
175
+ f"confirm the surface before painting")
176
+
177
+ reasons.extend(_condition_reasons(prediction, limits, checks, warnings))
178
+ reasons.extend(_color_reasons(prediction, limits, checks, warnings))
179
+ reasons.extend(_object_reasons(prediction, limits, checks, warnings))
180
+
181
  if depth is None:
182
  checks["depth_confirmed"] = False
183
  if limits["require_depth"]:
 
203
  if fraction > limits[limit_key]:
204
  reasons.append(f"depth shows unaccounted {label} covering {fraction:.1%} of the wall keep-out")
205
 
206
+ return {"decision": "reject" if reasons else "approve", "reasons": reasons, "warnings": warnings,
207
+ "checks": checks}
208
+
209
+
210
+ def assess_signoff(prediction):
211
+ """May the robot declare the wall finished? Fails closed like :func:`assess`."""
212
+ reasons, checks = [], {}
213
+ completion = prediction.get("completion") or {}
214
+ status = completion.get("status")
215
+ checks["completion_status"] = status
216
+ checks["confident_painted_fraction"] = _number(completion, "confident_painted_fraction")
217
+ checks["completion_threshold"] = completion.get("completion_threshold")
218
+ if status is None:
219
+ reasons.append("prediction has no completion block (predict.py predates completion checks)")
220
+ elif status == "not_calibrated":
221
+ reasons.append("checkpoint has no validated completion threshold; a person must sign the wall off")
222
+ elif status == "no_wall_detected":
223
+ reasons.append("no wall detected")
224
+ elif status != "complete":
225
+ fraction = checks["confident_painted_fraction"]
226
+ regions = completion.get("touch_up_regions_total", 0)
227
+ reasons.append(f"only {fraction:.0%} of the wall is confidently painted; "
228
+ f"{regions} touch-up region(s) remain" if fraction is not None else f"completion status {status}")
229
+ if prediction.get("inspection_status") == "reobserve_low_confidence":
230
+ reasons.append("perception requested re-observation (low confidence)")
231
+ # A finished coat over rust or failing paint is not finished: it will not last.
232
+ condition = (prediction.get("surface_condition") or {}).get("status")
233
+ checks["surface_condition_status"] = condition
234
+ if condition in ("defects_detected", "possible_rust"):
235
+ reasons.append(f"surface condition {condition.replace('_', ' ')}; the wall cannot be signed off")
236
  return {"decision": "reject" if reasons else "approve", "reasons": reasons, "checks": checks}
237
 
238
 
 
244
  help="do not reject when depth confirmation is missing (less safe)")
245
  parser.add_argument("--max-uncertain-fraction", type=float)
246
  parser.add_argument("--min-plane-fraction", type=float)
247
+ parser.add_argument("--allow-surface-defects", action="store_true",
248
+ help="surface already prepared: do not reject on detected paint failure/rust")
249
+ parser.add_argument("--strict-rust-cue", action="store_true",
250
+ help="reject on the colour-only rust cue too (it is near chance on real photos)")
251
+ parser.add_argument("--no-require-condition", action="store_true",
252
+ help="do not reject when the checkpoint cannot assess surface condition (less safe)")
253
+ parser.add_argument("--signoff", action="store_true",
254
+ help="decide whether the wall may be declared finished instead of whether to paint")
255
+ parser.add_argument("--allow-wallpaper", action="store_true",
256
+ help="surface confirmed paintable: do not reject on detected/possible wallpaper")
257
+ parser.add_argument("--max-color-delta-e", type=float,
258
+ help="apply the gate's own colour limit: ΔE00 above it is a mismatch, below it a warning")
259
+ parser.add_argument("--strict-objects-in-paint-zone", action="store_true",
260
+ help="reject on equipment standing in the paint area (the cue detector has known "
261
+ "false positives, so by default this only warns)")
262
+ parser.add_argument("--require-tools-on-site", action="store_true",
263
+ help="reject when a tool the plan needs cannot be seen on site")
264
  args = parser.parse_args()
265
 
266
+ thresholds = {"require_depth": not args.no_require_depth, "allow_wallpaper": args.allow_wallpaper,
267
+ "allow_surface_defects": args.allow_surface_defects,
268
+ "require_condition": not args.no_require_condition,
269
+ "strict_rust_cue": args.strict_rust_cue,
270
+ "strict_objects_in_paint_zone": args.strict_objects_in_paint_zone,
271
+ "require_tools_on_site": args.require_tools_on_site}
272
  if args.max_uncertain_fraction is not None:
273
  thresholds["max_uncertain_fraction"] = args.max_uncertain_fraction
274
  if args.min_plane_fraction is not None:
275
  thresholds["min_plane_fraction"] = args.min_plane_fraction
276
+ if args.max_color_delta_e is not None:
277
+ thresholds["max_color_delta_e"] = args.max_color_delta_e
278
 
279
  prediction = json.loads(args.prediction.read_text(encoding="utf-8"))
280
  depth = json.loads(args.depth_report.read_text(encoding="utf-8")) if args.depth_report else None
281
+ result = assess_signoff(prediction) if args.signoff else assess(prediction, depth, thresholds)
282
  print(json.dumps(result, indent=2))
283
  raise SystemExit(0 if result["decision"] == "approve" else 1)
284