####################################################################################### # # MIT License # # Copyright (c) [2025] [leonelhs@gmail.com] # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in all # copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. # ####################################################################################### # This file implements an API endpoint for DIS background image removal system. # [Self space] - [https://huggingface.co/spaces/leonelhs/removebg] # # Source code is based on or inspired by several projects. # For more details and proper attribution, please refer to the following resources: # # - [DIS] - [https://github.com/xuebinqin/DIS] # - [removebg] - [https://huggingface.co/spaces/gaviego/removebg] # https://github.com/gaurav0651/dis-bg-remover from itertools import islice import cv2 import gradio as gr import numpy as np import onnxruntime as ort from PIL import Image from huggingface_hub import hf_hub_download REPO_ID = "leonelhs/removators" # Load the ONNX model model_path = hf_hub_download(repo_id=REPO_ID, filename='isnet.onnx') session = ort.InferenceSession(model_path) def normalize(image, mean, std): """Normalize a numpy image with mean and standard deviation.""" return (image / 255.0 - mean) / std def predict(image_path): input_size = (1024, 1024) img = cv2.imread(image_path, cv2.IMREAD_COLOR) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # Convert from BGR to RGB if using OpenCV # If image is grayscale, convert to RGB if len(img.shape) == 2: img = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB) # Normalize the image using NumPy img = img.astype(np.float32) # Convert to float im_normalized = normalize(img, mean=[0.5, 0.5, 0.5], std=[1.0, 1.0, 1.0]) # Resize the image img_resized = cv2.resize(im_normalized, input_size, interpolation=cv2.INTER_LINEAR) img_resized = np.transpose(img_resized, (2, 0, 1)) # CHW format img_resized = np.expand_dims(img_resized, axis=0) # Add batch dimension # Run inference img_resized = img_resized.astype(np.float32) ort_inputs = {session.get_inputs()[0].name: img_resized} prediction = session.run(None, ort_inputs) # Process the model output result = prediction[0][0] # Assuming single output and single batch result = np.clip(result, 0, 1) # Assuming you want to clip the result to [0, 1] result = (result * 255).astype(np.uint8) # Rescale to [0, 255] result = np.transpose(result, (1, 2, 0)) # HWC format # Resize to original shape original_shape = img.shape[:2] return cv2.resize(result, (original_shape[1], original_shape[0]), interpolation=cv2.INTER_LINEAR) def cuts(image): mask = predict(image) mask = Image.fromarray(mask).convert('L') cutted = Image.open(image).convert("RGB") cutted.putalpha(mask) return [image, cutted], mask with gr.Blocks(title="DIS") as app: navbar = gr.Navbar(visible=True, main_page_name="Workspace") gr.Markdown("## Dichotomous Image Segmentation") with gr.Row(): with gr.Column(scale=1): inp_image = gr.Image(type="filepath", label="Upload Image") btn_predict = gr.Button(variant="primary", value="Remove background") with gr.Column(scale=2): with gr.Row(): preview = gr.ImageSlider(type="filepath", label="Comparer") btn_predict.click(cuts, inputs=[inp_image], outputs=[preview, inp_image]) with app.route("Readme", "/readme"): with open("README.md") as f: for line in islice(f, 12, None): gr.Markdown(line.strip()) app.launch(share=False, debug=True, show_error=True, mcp_server=True, pwa=True) app.queue()