####################################################################################### # # 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. # ####################################################################################### # Implements an API endpoint for background image removal. # # This project is one of several repositories exploring image segmentation techniques. # All related projects and interactive demos can be found at: # https://huggingface.co/spaces/leonelhs/removatorsau # Self app: https://huggingface.co/spaces/leonelhs/rembg # # Source code is based on or inspired by several projects. # For more details and proper attribution, please refer to the following resources: # # - [Rembg] - [https://github.com/danielgatis/rembg] # - [huggingface] [https://huggingface.co/spaces/KenjieDec/RemBG] # import gradio as gr import numpy as np from PIL import Image from rembg import new_session from rembg.bg import post_process MODELS = { "General segmentation": "u2net", "Human segmentation": "u2net_human_seg", "Cloth segmentation": "u2net_cloth_seg" } def predict(image, session="u2net"): """ Remove the background from an image. The function extracts the foreground and generates both a background-removed image and a binary mask. Parameters: image (pil): File path to the input image. session (string): Model for generate cutting mask. Returns: paths (tuple): paths for background-removed image and cutting mask. """ session = new_session(session) mask = session.predict(image)[0] smoot_mask = Image.fromarray(post_process(np.array(mask))) image.putalpha(smoot_mask) return image, smoot_mask footer = r"""