Implement selective cleanup and region stats in app.py
Browse filesAdded selective cleanup features for image processing, including region statistics and recoloring options.
33 unmodified lines343536373839427 unmodified lines46746846947047147247347447515 unmodified lines491492493494495496497171 unmodified lines66967067167267367467567667767867968068168268368468568668798 unmodified lines78678778878979079179279379479579679733 unmodified linesif "fringe_val" not in st.session_state:st.session_state.fringe_val = 0def step_fringe(delta: int):st.session_state.fringe_val = max(0, min(5, st.session_state.fringe_val + delta))427 unmodified linesinterpolation=cv2.INTER_NEAREST) > 0def selective_cleanup(png_bytes: bytes, key_hex: str, region: np.ndarray, shadow: int, trim: int) -> bytes:"""Trim shadow and border residue inside the selected region only."""if (shadow <= 0 and trim <= 0) or region is None or not region.any():return png_bytesimg = Image.open(io.BytesIO(png_bytes)).convert("RGBA")15 unmodified linestrimmed = cv2.erode(alpha, np.ones((3, 3), np.uint8), iterations=trim)alpha = np.where(region, trimmed, alpha)out = Image.fromarray(np.dstack([arr[:, :, :3], alpha]).astype(np.uint8), "RGBA")buf = io.BytesIO()out.save(buf, format="PNG")return buf.getvalue()171 unmodified linesbox_x = st.slider("Region \u2014 left / right %", 0, 100, (0, 100))box_y = st.slider("Region \u2014 top / bottom %", 0, 100, (60, 100))shadow_strength = st.slider("Shadow removal", 0, 60, 25,help="Cuts pixels inside the region that share the backdrop's hue but ""are darker. Black fabric is unaffected \u2014 it is dark but neutral, ""not a dark version of the backdrop. Raise until the shadow goes; ""if real fabric starts disappearing, you have gone too far.",)extra_trim = st.slider("Extra trim (px)", 0, 6, 0,help="Blunt erosion inside the region only, for border residue that ""isn't shadow.",)col1, col2 = st.columns(2, gap="large")with col1:98 unmodified lineselse:region = box_region(shape, (box_x, box_y))out_bytes = selective_cleanup(out_bytes,detect_background_hex(img_array),region,shadow_strength,extra_trim,)extracted_image = Image.open(io.BytesIO(out_bytes))33 unmodified lines343536373839404142434445464748495051427 unmodified lines47948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458515 unmodified lines601602603604605606607608609610611171 unmodified lines78378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183298 unmodified lines93193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797833 unmodified linesif "fringe_val" not in st.session_state:st.session_state.fringe_val = 0# Selective-cleanup widgets are keyed so the Auto panel can write to them. They# are seeded here, once, rather than through each widget's value= argument:# passing both a default and a session-state value makes Streamlit warn and# discard one of them.for _key, _default in {"sel_shadow": 25, "sel_trim": 0,"sel_recolour": "Off", "sel_recolour_hex": "#1a1a1a", "sel_recolour_strength": 100,}.items():st.session_state.setdefault(_key, _default)RECOLOUR_MODES = ["Off", "Neutralise cast", "Tint to colour"]def step_fringe(delta: int):st.session_state.fringe_val = max(0, min(5, st.session_state.fringe_val + delta))427 unmodified linesinterpolation=cv2.INTER_NEAREST) > 0def _region_stats(rgb: np.ndarray, alpha: np.ndarray, region: np.ndarray, key_hex: str) -> dict:"""Measure what is actually wrong inside the selection.Every number here is a count of pixels, not a judgement. The recommendationbuilt from them is shown to the user before anything is applied, because ametric that cannot tell garment from background has already cost this projectonce: ranking engines by "% of frame kept" picked chroma key, which had lefta green wash over the whole garment, over the near-perfect subject cutout."""key = np.array([int(key_hex.lstrip("#")[i:i + 2], 16) for i in (0, 2, 4)], np.float32)visible = region & (alpha > 10)total = max(int(visible.sum()), 1)chroma = rgb / (rgb.sum(-1, keepdims=True) + 1e-6)key_chroma = key / (key.sum() + 1e-6)hue_gap = np.linalg.norm(chroma - key_chroma, axis=-1) * 255ratio = rgb.sum(-1) / (key.sum() + 1e-6)# Backdrop-hue pixels ABOVE the brightness floor are shadow: the backdrop# seen dimmer. The same hue BELOW it is fabric that happens to be dark, and# cutting it is how black trousers went from 87.4% surviving to 6.0%.shadowy = visible & (hue_gap < 40) & (ratio < 1.0) & (ratio > BRIGHTNESS_FLOOR)# Spill is different: garment pixels carrying the backdrop's colour cast. It# is a COLOUR defect, so trimming or cutting alpha is the wrong tool.spill = visible & (hue_gap < 40) & (ratio <= BRIGHTNESS_FLOOR)soft = region & (alpha > 10) & (alpha < 245)gaps = hue_gap[shadowy]return {"visible": total,"shadow_pct": 100.0 * int(shadowy.sum()) / total,"spill_pct": 100.0 * int(spill.sum()) / total,"soft_pct": 100.0 * int(soft.sum()) / total,"shadow_gap_p75": float(np.percentile(gaps, 75)) if gaps.size else 0.0,}def suggest_settings(stats: dict) -> dict:"""Turn the measurements into slider values. Deliberately conservative."""shadow = 0if stats["shadow_pct"] >= 1.0:# Sit just above the 75th percentile of the hue gaps actually measured,# so the bulk of what was detected is covered without opening the gate# wider than this photo needs.shadow = int(min(60, max(10, round(stats["shadow_gap_p75"] + 5))))recolour = "Neutralise cast" if stats["spill_pct"] >= 3.0 else "Off"# Trim is blunt and unrecoverable, so it is only suggested when there is a# soft fringe left AND no colour defect that recolouring would fix better.trim = 2 if (stats["soft_pct"] >= 12.0 and recolour == "Off") else 0return {"shadow": shadow, "trim": trim, "recolour": recolour}def recolour_region(rgb: np.ndarray, alpha: np.ndarray, region: np.ndarray,mode: str, target_hex: str, strength: int) -> np.ndarray:"""Repaint colour inside the selection. Alpha is never touched.Two different jobs:"Neutralise cast" is the right tool for a stubborn coloured halo. It is thesame colour-grow used at the silhouette edge: take the nearest pixel that isOUTSIDE the selection and solidly opaque, and copy its colour in. That isreal garment colour sampled from the same garment, so it matches weave andlighting instead of flattening them."Tint to colour" is a product change, not a repair — a colourway. Each pixelkeeps its own luminance relative to the region mean, so folds, creases andsheen survive; only the hue is replaced. Painting the region flat insteadwould delete the fabric texture entirely."""if mode == "Off" or strength <= 0 or region is None or not region.any():return rgbout = rgb.copy()target_area = region & (alpha > 10)if not target_area.any():return rgbblend = np.clip(strength / 100.0, 0.0, 1.0)if mode == "Neutralise cast":donor = (~region) & (alpha > 245)if not donor.any():return rgbindices = distance_transform_edt(~donor, return_indices=True)[1]sampled = rgb[indices[0], indices[1]]out[target_area] = (rgb[target_area] * (1 - blend) + sampled[target_area] * blend)return outtarget = np.array([int(target_hex.lstrip("#")[i:i + 2], 16) for i in (0, 2, 4)], np.float32)lum = rgb @ np.array([0.299, 0.587, 0.114], np.float32)mean_lum = float(lum[target_area].mean()) or 1.0scaled = np.clip(target[None, None, :] * (lum / mean_lum)[..., None], 0, 255)out[target_area] = (rgb[target_area] * (1 - blend) + scaled[target_area] * blend)return outdef selective_cleanup(png_bytes: bytes, key_hex: str, region: np.ndarray, shadow: int, trim: int,recolour: str = "Off", recolour_hex: str = "#000000",recolour_strength: int = 100) -> bytes:"""Trim shadow and border residue, and recolour, inside the selection only."""nothing_to_do = shadow <= 0 and trim <= 0 and recolour == "Off"if nothing_to_do or region is None or not region.any():return png_bytesimg = Image.open(io.BytesIO(png_bytes)).convert("RGBA")15 unmodified linestrimmed = cv2.erode(alpha, np.ones((3, 3), np.uint8), iterations=trim)alpha = np.where(region, trimmed, alpha)# Recolour last, against the alpha that survived the two steps above, so it# never repaints pixels that are about to be cut away.rgb = recolour_region(rgb, alpha, region, recolour, recolour_hex, recolour_strength)out = Image.fromarray(np.dstack([rgb, alpha]).astype(np.uint8), "RGBA")buf = io.BytesIO()out.save(buf, format="PNG")return buf.getvalue()171 unmodified linesbox_x = st.slider("Region \u2014 left / right %", 0, 100, (0, 100))box_y = st.slider("Region \u2014 top / bottom %", 0, 100, (60, 100))# The Auto panel measures the region and writes its suggestion here,# then reruns. Adopting it must happen BEFORE the widgets exist \u2014# Streamlit refuses to let a widget's value be set after it is drawn.pending = st.session_state.pop("pending_auto", None)if pending:st.session_state["sel_shadow"] = pending["shadow"]st.session_state["sel_trim"] = pending["trim"]st.session_state["sel_recolour"] = pending["recolour"]shadow_strength = st.slider("Shadow removal", 0, 60, key="sel_shadow",help="Cuts pixels inside the region that share the backdrop's hue but ""are darker. Black fabric is unaffected \u2014 it is dark but neutral, ""not a dark version of the backdrop. Raise until the shadow goes; ""if real fabric starts disappearing, you have gone too far.",)extra_trim = st.slider("Extra trim (px)", 0, 6, key="sel_trim",help="Blunt erosion inside the region only, for border residue that ""isn't shadow.",)recolour_mode = st.radio("Recolour", RECOLOUR_MODES, key="sel_recolour",help="Repairs or replaces COLOUR inside the region. Alpha is never ""touched, so the silhouette cannot change. Use it when the defect ""is a tint rather than something that should be cut away \u2014 ""trimming a coloured halo removes garment, recolouring it does not.",)recolour_hex = "#1a1a1a"recolour_strength = 100if recolour_mode == "Tint to colour":recolour_hex = st.color_picker("Target colour", key="sel_recolour_hex")st.caption("Each pixel keeps its own brightness, so folds and sheen survive ""\u2014 only the hue is replaced.")if recolour_mode != "Off":recolour_strength = st.slider("Recolour strength %", 0, 100, key="sel_recolour_strength",help="Blend against the original. Below 100 the old colour shows ""through, which is often what you want on a partial cast.",)col1, col2 = st.columns(2, gap="large")with col1:98 unmodified lineselse:region = box_region(shape, (box_x, box_y))key_hex = detect_background_hex(img_array)if region.any():with st.expander("Auto — analyse this selection", expanded=False):pre = np.array(Image.open(io.BytesIO(out_bytes)).convert("RGBA"))stats = _region_stats(pre[:, :, :3].astype(np.float32), pre[:, :, 3], region, key_hex)tip = suggest_settings(stats)left, mid, right = st.columns(3)left.metric("Shadow", f"{stats['shadow_pct']:.1f}%",help="Backdrop hue, dimmer than the backdrop, bright ""enough not to be dark fabric. Cuttable.")mid.metric("Colour cast", f"{stats['spill_pct']:.1f}%",help="Backdrop hue but too dark to be shadow — garment ""carrying the backdrop's colour. Recolour, do not cut.")right.metric("Soft edge", f"{stats['soft_pct']:.1f}%",help="Partly transparent pixels in the selection.")st.caption(f"Suggests shadow {tip['shadow']}, trim {tip['trim']} px, "f"recolour “{tip['recolour']}” over "f"{stats['visible']:,} visible pixels.")st.caption("These are pixel counts, not a verdict — nothing is applied ""until you press the button, and the sliders stay yours afterwards.")if st.button("Apply suggestion", width="stretch"):st.session_state["pending_auto"] = tipst.rerun()out_bytes = selective_cleanup(out_bytes,key_hex,region,shadow_strength,extra_trim,recolour_mode,recolour_hex,recolour_strength,)extracted_image = Image.open(io.BytesIO(out_bytes))
app.py+188-7