Spaces:
Running
Running
File size: 3,787 Bytes
fdb0069 925248f fdb0069 05d99b1 4877588 fdb0069 05d99b1 fdb0069 4877588 fdb0069 4877588 fdb0069 925248f fdb0069 4877588 fdb0069 05d99b1 fdb0069 4877588 925248f fdb0069 |
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 |
#######################################################################################
#
# 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"""
<center>
Demo based on <a href='https://github.com/danielgatis/rembg'>Rembg</a>
</center>
"""
with gr.Blocks(title="Rembg") as app:
gr.Markdown("## Remove Background Tool")
with gr.Row():
with gr.Column(scale=1):
inp = gr.Image(type="pil", label="Upload Image")
sess = gr.Dropdown(choices=list(MODELS.items()), label="Model Segment", value="u2net")
btn_predict = gr.Button("Remove background")
with gr.Column(scale=2):
with gr.Row():
with gr.Column(scale=1):
out = gr.Image(type="pil", label="Output image")
with gr.Accordion("See intermediates", open=False):
out_mask = gr.Image(type="pil", label="Mask")
btn_predict.click(predict, inputs=[inp, sess], outputs=[out, out_mask])
with gr.Row():
gr.HTML(footer)
app.launch(share=False, debug=True, show_error=True, mcp_server=True, pwa=True)
app.queue() |