"""Model-free rust cue: orange-brown, saturated, mid-dark pixels that form stains, not a whole wall. The condition head is learned from synthetic damage; this cue is a second opinion that needs no checkpoint, so older checkpoints without a condition head still flag likely rust. It looks for rust-coloured pixels (hue roughly 5-35 degrees, saturation >= 0.4, value 0.15-0.75) on the predicted wall and scores how much of the wall they stain and whether they form compact patches or vertical streaks. Rust colour on *most* of the wall is far more often brick, terracotta paint or wood than corrosion, so coverage above ``WHOLE_WALL`` is discounted rather than rewarded. Known false positives: orange/brown paint in small areas, wood trim, and brick seen in small patches. numpy + scipy only. """ import numpy as np from scipy import ndimage HUE_RANGE = (5.0, 35.0) # degrees MIN_SATURATION = 0.4 VALUE_RANGE = (0.15, 0.75) MIN_FRACTION = 0.002 # of the wall, before the cue speaks at all WHOLE_WALL = 0.35 # rust-coloured share above which it is the wall's own colour def rgb_to_hsv(rgb): """``rgb`` uint8 or float (H, W, 3) -> hue in degrees, saturation and value in [0, 1].""" x = np.asarray(rgb, np.float32) if x.max() > 1.0: x = x / 255.0 r, g, b = x[..., 0], x[..., 1], x[..., 2] v = x.max(-1) c = v - x.min(-1) s = np.where(v > 0, c / np.maximum(v, 1e-6), 0) safe = np.maximum(c, 1e-6) h = np.where(v == r, ((g - b) / safe) % 6, np.where(v == g, (b - r) / safe + 2, (r - g) / safe + 4)) * 60 return np.where(c > 0, h, 0), s, v def rust_mask(rgb): h, s, v = rgb_to_hsv(rgb) return (h >= HUE_RANGE[0]) & (h <= HUE_RANGE[1]) & (s >= MIN_SATURATION) & (v >= VALUE_RANGE[0]) & (v <= VALUE_RANGE[1]) def rust_cue(rgb, wall): """Score in [0, 1] plus the evidence behind it; 0 when there is no wall.""" wall = np.asarray(wall, bool) total = int(wall.sum()) if not total: return {"score": 0.0, "fraction_of_wall": 0.0, "components": 0, "streak_share": 0.0, "whole_wall_colour": False} rusty = rust_mask(rgb) & wall rusty = ndimage.binary_opening(rusty, iterations=1) # drop single-pixel speckle fraction = float(rusty.sum()) / total labels, count = ndimage.label(rusty) streak_px = 0 if count: for index, box in enumerate(ndimage.find_objects(labels), start=1): ys, xs = box height, width = ys.stop - ys.start, xs.stop - xs.start if height >= 3 * max(width, 1) and height >= 8: # tall and thin: a run-off streak streak_px += int((labels[box] == index).sum()) streak_share = streak_px / max(int(rusty.sum()), 1) whole_wall = fraction > WHOLE_WALL if fraction < MIN_FRACTION: score = 0.0 elif whole_wall: score = 0.2 * streak_share # the wall's colour, not a stain else: # Saturates around 3% of the wall; streaks are the most rust-specific shape. score = min(1.0, fraction / 0.03) * (0.6 + 0.4 * streak_share) return {"score": round(float(score), 4), "fraction_of_wall": round(fraction, 5), "components": int(count), "streak_share": round(float(streak_share), 4), "whole_wall_colour": bool(whole_wall)} def _regions(mask, limit=10): labels, count = ndimage.label(mask) if not count: return [] sizes = np.bincount(labels.ravel())[1:] out = [] for index in np.argsort(-sizes)[:limit] + 1: ys, xs = ndimage.find_objects(labels)[index - 1] out.append({"bbox_xyxy": [xs.start, ys.start, xs.stop - 1, ys.stop - 1], "pixels": int(sizes[index - 1])}) return out DEFECTS = ((1, "paint_failure"), (2, "rust")) def defect_masks(known_wall, condition_class, condition_confidence, min_confidence): """Per defect type, the confident wall pixels the condition head calls damaged (speckle removed).""" known_wall = np.asarray(known_wall, bool) confident = np.asarray(condition_confidence) >= min_confidence return {name: ndimage.binary_opening((np.asarray(condition_class) == index) & confident & known_wall, iterations=1) for index, name in DEFECTS} def defect_fraction_curve(known_wall, condition_class, condition_confidence, confidences): """Largest single-type defect share of the wall at each confidence: the quantity the verdict thresholds. An image is flagged at ``(c, f)`` exactly when ``curve[c] >= f`` (see ``condition_summary``), so one pass per image scores every operating point. """ total = int(np.asarray(known_wall, bool).sum()) if not total or condition_class is None: return {c: 0.0 for c in confidences} return {c: max(float(m.sum()) / total for m in defect_masks(known_wall, condition_class, condition_confidence, c).values()) for c in confidences} def condition_summary(known_wall, condition_class, condition_confidence, cue, min_confidence=0.5, min_fraction=0.005, cue_threshold=0.5, min_region_px=32): """One surface-condition verdict from the condition head (if any) and the rust cue. ``condition_class``/``condition_confidence`` are None for checkpoints without a condition head. Status, most severe first: * ``defects_detected`` - the model sees paint failure or rust on at least ``min_fraction`` of the decided wall: prepare the surface before painting; * ``possible_rust`` - only the model-free cue fired; * ``not_assessed`` - no condition head and no cue: paint failure is unknown; * ``none_detected`` / ``no_wall_detected``. Returns ``(summary, defect_mask)``; the mask marks pixels to prepare (keep-out for paint). """ known_wall = np.asarray(known_wall, bool) total = int(known_wall.sum()) defect = np.zeros_like(known_wall) summary = {"model_supports_condition": condition_class is not None, "cue": cue, "min_confidence": min_confidence, "min_fraction": min_fraction, "cue_threshold": cue_threshold} fractions = {} if condition_class is not None and total: for name, found in defect_masks(known_wall, condition_class, condition_confidence, min_confidence).items(): fractions[name] = float(found.sum()) / total summary[name] = {"fraction_of_wall": round(fractions[name], 5), "regions": [r for r in _regions(found) if r["pixels"] >= min_region_px]} if fractions[name] >= min_fraction: defect |= found if not total: status = "no_wall_detected" elif any(f >= min_fraction for f in fractions.values()): status = "defects_detected" elif cue.get("score", 0) >= cue_threshold: status = "possible_rust" elif condition_class is None: status = "not_assessed" else: status = "none_detected" summary["status"] = status summary["defects"] = sorted(name for name, f in fractions.items() if f >= min_fraction) return summary, defect