Download app.py from 23f1000805/surface-defect-seg-extdata: direct link, hf CLI and curl.
- Browser
- Download file 1.32 kB
-
https://huggingface.co/23f1000805/surface-defect-seg-extdata/resolve/main/app.py
- Command line
-
hf download hf://23f1000805/surface-defect-seg-extdata/app.py
-
curl -L -o app.py https://huggingface.co/23f1000805/surface-defect-seg-extdata/resolve/main/app.py
1.32 kB
| import os | |
| from pathlib import Path | |
| import gradio as gr | |
| import numpy as np | |
| MODEL_REPO = os.environ.get("MODEL_REPO", "23f1000805/surface-defect-seg-extdata") | |
| root = Path(__file__).parent | |
| if not (root / "ensemble.json").exists(): | |
| from huggingface_hub import snapshot_download | |
| root = Path(snapshot_download(MODEL_REPO, token=os.environ.get("HF_TOKEN"))) | |
| import sys | |
| sys.path.insert(0, str(root)) | |
| from inference import DefectSegmenter, to_gray | |
| model = DefectSegmenter(root) | |
| COLORS = [(230, 57, 70), (42, 157, 143), (233, 196, 106), (69, 123, 157)] | |
| def run(image): | |
| if image is None: | |
| return None, "no image" | |
| gray = to_gray(image) | |
| masks = model.predict(gray) | |
| base = np.stack([gray] * 3, -1) | |
| lines = [] | |
| for (name, m), col in zip(masks.items(), COLORS): | |
| if m.any(): | |
| base[m > 0] = 0.5 * base[m > 0] + 0.5 * np.array(col, dtype=np.float32) | |
| lines.append(f"{name}: {int(m.sum())} pixels") | |
| return base.clip(0, 255).astype(np.uint8), "\n".join(lines) or "no defects found" | |
| demo = gr.Interface(run, gr.Image(type="numpy", label="surface image"), | |
| [gr.Image(label="predicted defects"), gr.Textbox(label="summary")], | |
| title="Industrial surface defect segmentation") | |
| if __name__ == "__main__": | |
| demo.launch() | |