Spaces:
Sleeping
Sleeping
Download app.py from DILEEPJAAT/bg-remover: direct link, hf CLI and curl.
- Browser
- Download file 9.05 kB
-
https://huggingface.co/spaces/DILEEPJAAT/bg-remover/resolve/main/app.py
- Command line
-
hf download hf://spaces/DILEEPJAAT/bg-remover/app.py
-
curl -L -o app.py https://huggingface.co/spaces/DILEEPJAAT/bg-remover/resolve/main/app.py
9.05 kB
| from __future__ import annotations | |
| import hashlib | |
| import os | |
| import threading | |
| from pathlib import Path | |
| import gradio as gr | |
| import numpy as np | |
| import onnxruntime as ort | |
| import spaces | |
| from huggingface_hub import hf_hub_download | |
| from PIL import Image, ImageOps | |
| from matting_refine import build_vitmatte_inputs, finalise_cutout, restore_vitmatte_alpha | |
| APP_TITLE = "BG Remover" | |
| RMBG_REPO = "briaai/RMBG-2.0" | |
| RMBG_FILE = "onnx/model_fp16.onnx" | |
| RMBG_SHA256 = "9dc47db40d113090ba5d7a13d8fcfd9ee4eda510ce92613219b2fe19da4746f6" | |
| # Public Apache-2.0 ViTMatte ONNX export, used only as a second-stage matting refiner. | |
| VITMATTE_REPO = "Xenova/vitmatte-small-distinctions-646" | |
| VITMATTE_FILE = "onnx/model.onnx" | |
| # SHA-256 of the current Xenova/vitmatte-small-distinctions-646/onnx/model.onnx file. | |
| VITMATTE_SHA256 = "a1cf48234c369faa3ea1711981d961fe1ec71f51e593f9d6553aa5a0e7d557e3" | |
| TARGET_SIZE = 1024 | |
| MAX_PIXELS = 20_000_000 | |
| MEAN = np.array([0.485, 0.456, 0.406], dtype=np.float32) | |
| STD = np.array([0.229, 0.224, 0.225], dtype=np.float32) | |
| _RMBG_PATH: Path | None = None | |
| _VIT_PATH: Path | None = None | |
| _RMBG_SESSION: ort.InferenceSession | None = None | |
| _VIT_SESSION: ort.InferenceSession | None = None | |
| _LOCK = threading.Lock() | |
| def _sha256(path: Path) -> str: | |
| digest = hashlib.sha256() | |
| with path.open("rb") as handle: | |
| for chunk in iter(lambda: handle.read(1024 * 1024), b""): | |
| digest.update(chunk) | |
| return digest.hexdigest() | |
| def _download_models() -> tuple[Path, Path]: | |
| token = os.getenv("HF_TOKEN") | |
| if not token: | |
| raise RuntimeError( | |
| "HF_TOKEN is missing. Add your Hugging Face READ token as the " | |
| "HF_TOKEN Space Secret." | |
| ) | |
| rmbg = Path( | |
| hf_hub_download( | |
| repo_id=RMBG_REPO, | |
| filename=RMBG_FILE, | |
| repo_type="model", | |
| token=token, | |
| ) | |
| ) | |
| vit = Path( | |
| hf_hub_download( | |
| repo_id=VITMATTE_REPO, | |
| filename=VITMATTE_FILE, | |
| repo_type="model", | |
| ) | |
| ) | |
| for path, expected, label in ( | |
| (rmbg, RMBG_SHA256, "RMBG-2.0"), | |
| (vit, VITMATTE_SHA256, "ViTMatte"), | |
| ): | |
| actual = _sha256(path) | |
| if actual.lower() != expected.lower(): | |
| raise RuntimeError(f"{label} integrity check failed: expected {expected}, got {actual}") | |
| print(f"{label} ready: {path}") | |
| print(f"{label} SHA-256: {actual}") | |
| return rmbg, vit | |
| _RMBG_PATH, _VIT_PATH = _download_models() | |
| def _session_options() -> ort.SessionOptions: | |
| opts = ort.SessionOptions() | |
| opts.graph_optimization_level = ort.GraphOptimizationLevel.ORT_ENABLE_ALL | |
| opts.execution_mode = ort.ExecutionMode.ORT_SEQUENTIAL | |
| opts.intra_op_num_threads = max(1, min(os.cpu_count() or 2, 4)) | |
| opts.inter_op_num_threads = 1 | |
| opts.enable_mem_pattern = True | |
| opts.enable_cpu_mem_arena = True | |
| return opts | |
| def _get_sessions() -> tuple[ort.InferenceSession, ort.InferenceSession]: | |
| global _RMBG_SESSION, _VIT_SESSION | |
| if _RMBG_SESSION is not None and _VIT_SESSION is not None: | |
| return _RMBG_SESSION, _VIT_SESSION | |
| with _LOCK: | |
| available = ort.get_available_providers() | |
| print(f"Available ORT providers: {available}") | |
| if "CUDAExecutionProvider" not in available: | |
| raise RuntimeError( | |
| "CUDAExecutionProvider is unavailable. Make sure this Space is running on ZeroGPU." | |
| ) | |
| providers = ["CUDAExecutionProvider", "CPUExecutionProvider"] | |
| if _RMBG_SESSION is None: | |
| _RMBG_SESSION = ort.InferenceSession( | |
| str(_RMBG_PATH), sess_options=_session_options(), providers=providers | |
| ) | |
| if _VIT_SESSION is None: | |
| _VIT_SESSION = ort.InferenceSession( | |
| str(_VIT_PATH), sess_options=_session_options(), providers=providers | |
| ) | |
| print("RMBG providers:", _RMBG_SESSION.get_providers()) | |
| print("ViTMatte providers:", _VIT_SESSION.get_providers()) | |
| return _RMBG_SESSION, _VIT_SESSION | |
| def _prepare_rmbg_tensor(image_rgb: Image.Image) -> np.ndarray: | |
| resized = image_rgb.resize((TARGET_SIZE, TARGET_SIZE), Image.Resampling.BILINEAR) | |
| arr = np.asarray(resized, dtype=np.float32) / 255.0 | |
| arr = (arr - MEAN) / STD | |
| return np.transpose(arr, (2, 0, 1))[None].astype(np.float32, copy=False) | |
| def _extract_alpha(outputs: list[np.ndarray], session: ort.InferenceSession) -> np.ndarray: | |
| names = [x.name for x in session.get_outputs()] | |
| raw = outputs[names.index("alphas")] if "alphas" in names else outputs[0] | |
| return np.clip(np.asarray(raw).squeeze(), 0.0, 1.0).astype(np.float32, copy=False) | |
| def _predict(session: ort.InferenceSession, tensor: np.ndarray) -> np.ndarray: | |
| name = session.get_inputs()[0].name | |
| return _extract_alpha(session.run(None, {name: tensor}), session) | |
| def _prepare_source(image: Image.Image) -> tuple[Image.Image, np.ndarray, tuple[int, int]]: | |
| image = ImageOps.exif_transpose(image).convert("RGBA") | |
| original_size = image.size | |
| if image.width * image.height > MAX_PIXELS: | |
| scale = (MAX_PIXELS / float(image.width * image.height)) ** 0.5 | |
| image = image.resize( | |
| (max(1, round(image.width * scale)), max(1, round(image.height * scale))), | |
| Image.Resampling.LANCZOS, | |
| ) | |
| return image.convert("RGB"), np.asarray(image.getchannel("A"), dtype=np.uint8), original_size | |
| def remove_background(image: Image.Image): | |
| if image is None: | |
| return None | |
| source_rgb, source_alpha, _ = _prepare_source(image) | |
| rmbg_session, vit_session = _get_sessions() | |
| # Stage 1: semantic foreground segmentation. | |
| rmbg_tensor = _prepare_rmbg_tensor(source_rgb) | |
| raw = _predict(rmbg_session, rmbg_tensor) | |
| model_alpha = np.asarray( | |
| Image.fromarray(np.rint(raw * 255).astype(np.uint8), mode="L").resize( | |
| source_rgb.size, Image.Resampling.LANCZOS | |
| ), | |
| dtype=np.float32, | |
| ) / 255.0 | |
| # Stage 2: automatic trimap + learned alpha matting on a subject-focused | |
| # crop. This is the important edge-quality layer missing from simple RMBG use. | |
| vit_input, bbox, _, _ = build_vitmatte_inputs( | |
| np.asarray(source_rgb, dtype=np.uint8), model_alpha | |
| ) | |
| vit_raw = _predict(vit_session, vit_input) | |
| vit_alpha = restore_vitmatte_alpha(vit_raw, bbox, source_rgb.size) | |
| # Stage 3: final alpha/colour finishing outside the neural models. | |
| out_rgb, out_a = finalise_cutout( | |
| np.asarray(source_rgb, dtype=np.uint8), model_alpha, vit_alpha, source_alpha | |
| ) | |
| result = Image.fromarray(out_rgb, mode="RGB") | |
| result.putalpha(Image.fromarray(out_a, mode="L")) | |
| return result | |
| DESCRIPTION = "" | |
| CSS = """ | |
| .gradio-container { max-width: 1180px !important; margin: 0 auto !important; } | |
| #hero { text-align: center; padding: 30px 12px 12px; } | |
| #hero h1 { font-size: 42px; margin-bottom: 8px; letter-spacing: -1px; } | |
| #hero p { opacity: .72; font-size: 16px; } | |
| /* Desktop: keep input/output panels side-by-side. Gradio 6 can otherwise | |
| collapse Columns when a component reports a large intrinsic min-width. */ | |
| #io-row { | |
| display: flex !important; | |
| flex-direction: row !important; | |
| flex-wrap: nowrap !important; | |
| align-items: stretch !important; | |
| gap: 18px !important; | |
| } | |
| #io-row > div { | |
| min-width: 0 !important; | |
| width: 0 !important; | |
| flex: 1 1 0 !important; | |
| } | |
| #io-row .gradio-column { | |
| min-width: 0 !important; | |
| } | |
| #io-row .gradio-image { | |
| width: 100% !important; | |
| } | |
| #remove-btn { min-height: 54px; font-size: 18px; font-weight: 700; } | |
| .footer-note { text-align: center; opacity: .6; font-size: 12px; margin-top: 4px; } | |
| /* Phones/tablets: stack the panels only when the viewport is genuinely narrow. */ | |
| @media (max-width: 760px) { | |
| #io-row { | |
| flex-direction: column !important; | |
| flex-wrap: nowrap !important; | |
| } | |
| #io-row > div { | |
| width: 100% !important; | |
| flex: 1 1 auto !important; | |
| } | |
| #hero h1 { font-size: 34px; } | |
| } | |
| """ | |
| with gr.Blocks(title=APP_TITLE) as demo: | |
| gr.Markdown(f"<div id='hero'><h1>BG Remover</h1><p>{DESCRIPTION}</p></div>") | |
| with gr.Row(equal_height=True, elem_id="io-row"): | |
| with gr.Column(scale=1): | |
| input_image = gr.Image( | |
| label="Your image", type="pil", sources=["upload", "clipboard"], height=430 | |
| ) | |
| with gr.Column(scale=1): | |
| output_image = gr.Image(label="Background removed", type="pil", format="png", height=430) | |
| remove_button = gr.Button("Remove Background", variant="primary", elem_id="remove-btn") | |
| remove_button.click( | |
| fn=remove_background, | |
| inputs=input_image, | |
| outputs=output_image, | |
| concurrency_limit=1, | |
| show_progress="full", | |
| ) | |
| gr.Markdown( | |
| "<div class='footer-note'>AI Engine · Custom Engine · automatic trimap · Matting · edge colour decontamination</div>" | |
| ) | |
| demo.queue(max_size=8) | |
| if __name__ == "__main__": | |
| demo.launch(css=CSS, theme=gr.themes.Soft()) | |