File size: 11,574 Bytes
c5bce9d a208ded c5bce9d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
import gradio as gr
import numpy as np
import torch
from pathlib import Path
from PIL import Image
from inference import load_model, predict
# -------- Hub / Weights Configuration --------
HUB_REPO_ID = "Suzyloubna/bridge-unetpp" # Hugging Face model repo (change if you renamed it)
WEIGHTS_FILENAME = "MILESTONE_090_ACHIEVED_iou_0.9077.pth"
WEIGHTS_PATH = Path(WEIGHTS_FILENAME)
# Try to fetch weights from Hub if not present locally
# (Requires 'huggingface-hub' in requirements.txt)
try:
if not WEIGHTS_PATH.exists():
print(f"Weights file {WEIGHTS_FILENAME} not found locally. Downloading from {HUB_REPO_ID} ...")
from huggingface_hub import hf_hub_download
hf_hub_download(
repo_id=HUB_REPO_ID,
filename=WEIGHTS_FILENAME,
local_dir=".", # place file in current working directory
local_dir_use_symlinks=False # make a real copy (Spaces friendly)
)
if WEIGHTS_PATH.exists():
print("Download complete.")
else:
print("Download attempted but file still not found.")
except Exception as dl_err:
print(f"WARNING: Could not download weights automatically: {dl_err}")
# ---------------- Runtime / Device ----------------
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
CLASS_INFO = [
{"id": 0, "name": "background", "color": (0, 0, 0)},
{"id": 1, "name": "beton", "color": (0, 114, 189)},
{"id": 2, "name": "steel", "color": (200, 30, 30)},
]
COLOR_MAP = np.array([c["color"] for c in CLASS_INFO], dtype=np.uint8)
# ---------------- Load Model (defensive) ----------------
model_load_error = None
model = None
try:
if WEIGHTS_PATH.exists():
model = load_model(str(WEIGHTS_PATH), DEVICE)
else:
model_load_error = f"Weight file {WEIGHTS_FILENAME} not found after download attempt."
except Exception as e:
model_load_error = f"Model failed to load: {e}"
# ---------------- Utility Functions ----------------
def resize_mask_to_original(mask_np_small: np.ndarray, original_shape):
H, W = original_shape[:2]
if mask_np_small.shape[:2] == (H, W):
return mask_np_small
pil_small = Image.fromarray(mask_np_small.astype(np.uint8))
pil_big = pil_small.resize((W, H), resample=Image.NEAREST)
return np.array(pil_big)
def overlay_mask(original_np: np.ndarray, mask_np: np.ndarray, alpha: float = 0.5):
color_mask = COLOR_MAP[mask_np]
blended = (1 - alpha) * original_np.astype(np.float32) + alpha * color_mask
return blended.clip(0, 255).astype(np.uint8)
def compute_class_stats(mask_np: np.ndarray):
total = mask_np.size
counts = np.bincount(mask_np.flatten(), minlength=len(COLOR_MAP))
stats = []
for info in CLASS_INFO:
cid = info["id"]
count = int(counts[cid]) if cid < len(counts) else 0
pct = (count / total * 100.0) if total else 0.0
stats.append({**info, "count": count, "pct": pct})
return stats
def build_legend_html(stats):
rows = []
for s in stats:
r, g, b = s["color"]
rows.append(f"""
<div class="legend-item" aria-label="Class {s['name']}">
<div class="legend-color" style="--c: rgb({r},{g},{b});"></div>
<div class="legend-meta">
<div class="legend-name">{s['id']}: {s['name']}</div>
<div class="legend-stats">
<span class="legend-count">{s['count']} px</span>
<span class="legend-pct">{s['pct']:.2f}%</span>
</div>
</div>
</div>
""")
return f"""
<div class="legend-wrapper" id="legend-wrapper">
<div class="legend-header">
<span>Segmentation Legend</span>
<button onclick="toggleLegend()" class="legend-toggle-btn" aria-label="Collapse legend">⤢</button>
</div>
<div id="legend-body" class="legend-body expanded">
{''.join(rows)}
</div>
</div>
"""
def raw_mask_download(mask_np: np.ndarray):
from io import BytesIO
import base64
img = Image.fromarray(mask_np.astype(np.uint8))
bio = BytesIO()
img.save(bio, format="PNG")
bio.seek(0)
return "data:image/png;base64," + base64.b64encode(bio.read()).decode()
def make_colored_mask_rgba(mask_np: np.ndarray, bg_opacity: float):
"""
Return an RGBA image where background class (0) has adjustable opacity.
bg_opacity in [0,1].
"""
rgb = COLOR_MAP[mask_np] # (H,W,3)
H, W = mask_np.shape
alpha_channel = np.full((H, W), 255, dtype=np.uint8)
alpha_channel[mask_np == 0] = int(bg_opacity * 255)
rgba = np.dstack([rgb, alpha_channel]).astype(np.uint8)
return Image.fromarray(rgba, mode="RGBA")
def run_segmentation(image, view_mode, alpha, show_colored, return_small, bg_opacity):
if model is None:
return (None, None, "<p class='legend-empty'>Model not loaded.</p>",
f"<span style='color:#ff8080'>{model_load_error or 'Model error.'}</span>")
if image is None:
return (None, None, "<p class='legend-empty'>No image yet.</p>",
"<span style='opacity:0.6'>No mask.</span>")
pred_mask = predict(image, model, DEVICE)
mask_small = pred_mask.numpy()
H, W = image.shape[:2]
if return_small:
mask_np = mask_small
if view_mode == "Overlay":
pil_orig = Image.fromarray(image.astype(np.uint8))
base_img = np.array(pil_orig.resize(mask_small.shape[::-1], resample=Image.BILINEAR))
else:
base_img = image
else:
mask_np = resize_mask_to_original(mask_small, (H, W))
base_img = image
if view_mode == "Colored Mask":
out_img = make_colored_mask_rgba(mask_np, bg_opacity)
elif view_mode == "Overlay":
blended = overlay_mask(base_img, mask_np, alpha=alpha)
out_img = Image.fromarray(blended)
else: # Raw Class Indices
max_id = len(COLOR_MAP) - 1
norm = (mask_np / max_id * 255).astype(np.uint8)
gray_rgb = np.stack([norm, norm, norm], axis=-1)
out_img = Image.fromarray(gray_rgb)
if show_colored:
colored_only = make_colored_mask_rgba(mask_np, bg_opacity)
else:
colored_only = None
stats = compute_class_stats(mask_np)
legend_html = build_legend_html(stats)
download_link = raw_mask_download(mask_np)
download_html = f"<a class='download-anchor' href='{download_link}' download='raw_mask.png'>Download Raw Mask (PNG)</a>"
return out_img, colored_only, legend_html, download_html
def clear_outputs():
return None, None, "<p class='legend-empty'>Cleared.</p>", "<div id='download-link'>Cleared.</div>"
# ---------------- Load CSS ----------------
css_path = Path(__file__).parent / "style.css"
css_text = css_path.read_text(encoding="utf-8")
# ---------------- Interface Layout ----------------
with gr.Blocks(css=css_text, title="Hey Inspector • Drone Bridge Image Segmentation") as demo:
gr.HTML("""
<div class="hero-banner floating">
<h1 class="hero-title">Hey Inspector • Drone Bridge Image Segmentation</h1>
</div>
""")
if model_load_error:
gr.HTML(f"<div style='color:#ff4d4d; font-weight:600; margin-bottom:10px;'>{model_load_error}</div>")
gr.HTML("<p class='intro-tagline'>Upload an image and choose how you want to visualize the segmentation.</p>")
with gr.Row():
with gr.Column(scale=5, elem_classes="panel glass left-panel"):
input_image = gr.Image(
label="Input Image",
type="numpy",
image_mode="RGB",
sources=["upload", "clipboard", "webcam"]
)
view_mode = gr.Radio(
["Colored Mask", "Overlay", "Raw Class Indices"],
value="Colored Mask",
label="View Mode",
elem_id="view-mode-radio"
)
alpha = gr.Slider(
0.0, 1.0, value=0.5, step=0.05,
label="Overlay Opacity",
elem_id="alpha-slider"
)
bg_opacity = gr.Slider(
0.0, 1.0, value=1.0, step=0.05,
label="Background Opacity (Colored Mask)",
elem_id="bg-opacity-slider"
)
show_colored = gr.Checkbox(value=True, label="Show 'Colored Mask (Always)' panel")
return_small = gr.Checkbox(value=False, label="Return downsized (256x256) mask instead of original size")
with gr.Row():
run_btn = gr.Button("Run Segmentation", elem_id="run-btn", variant="primary")
clear_btn = gr.Button("Clear", elem_id="clear-btn")
with gr.Column(scale=7, elem_classes="panel glass right-panel"):
gr.Markdown("#### Results")
output_image = gr.Image(label="Result View", type="pil")
color_mask_output = gr.Image(label="Colored Mask (Always)", type="pil")
legend_html = gr.HTML("<p class='legend-empty'>Legend will appear here after segmentation.</p>")
download_html = gr.HTML("<div id='download-link'>No mask yet.</div>")
gr.Markdown("""
**Tips**
- Background Opacity affects only Colored Mask outputs (main and the 'always' panel).
- Set it to 0 to hide background and emphasize target classes.
- Overlay mode ignores the background opacity slider (uses original image + colored mask).
- Raw Class Indices is a grayscale class map.
""")
gr.HTML("""
<script>
function toggleLegend(){
const b = document.getElementById('legend-body');
if(b){ b.classList.toggle('collapsed'); }
}
function syncAlphaVisibility(){
const radios = document.querySelectorAll("#view-mode-radio input");
let mode = "Colored Mask";
radios.forEach(r => { if(r.checked) mode = r.value; });
const overlayWrap = document.querySelector("#alpha-slider")?.closest(".gr-form");
const overlayRange = document.querySelector("#alpha-slider input[type=range]");
const bgWrap = document.querySelector("#bg-opacity-slider")?.closest(".gr-form");
if(overlayRange){
if(mode === "Overlay"){
overlayRange.disabled = false;
if(overlayWrap) overlayWrap.style.opacity = "1";
} else {
overlayRange.disabled = true;
if(overlayWrap) overlayWrap.style.opacity = "0.35";
}
}
const bgRange = document.querySelector("#bg-opacity-slider input[type=range]");
if(bgRange){
if(mode === "Colored Mask"){
bgRange.disabled = false;
if(bgWrap) bgWrap.style.opacity = "1";
} else {
bgRange.disabled = true;
if(bgWrap) bgWrap.style.opacity = "0.35";
}
}
}
document.addEventListener("change", e => {
if(e.target && e.target.closest("#view-mode-radio")) syncAlphaVisibility();
});
window.addEventListener("load", syncAlphaVisibility);
</script>
""")
run_btn.click(
fn=run_segmentation,
inputs=[input_image, view_mode, alpha, show_colored, return_small, bg_opacity],
outputs=[output_image, color_mask_output, legend_html, download_html]
)
clear_btn.click(
fn=clear_outputs,
inputs=None,
outputs=[output_image, color_mask_output, legend_html, download_html]
)
if __name__ == "__main__":
demo.launch() |