From 7970e27901515a7e33ce37a96baa1737b52980da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thireus=20=E2=98=A0?= Date: Sun, 2 Apr 2023 20:03:59 +0100 Subject: [PATCH 1/5] Fixes #10 --- scripts/postprocessing_rembg.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/postprocessing_rembg.py b/scripts/postprocessing_rembg.py index da80e86..50b3fec 100644 --- a/scripts/postprocessing_rembg.py +++ b/scripts/postprocessing_rembg.py @@ -45,7 +45,7 @@ class ScriptPostprocessingUpscale(scripts_postprocessing.ScriptPostprocessing): } def process(self, pp: scripts_postprocessing.PostprocessedImage, model, return_mask, alpha_matting, alpha_matting_foreground_threshold, alpha_matting_background_threshold, alpha_matting_erode_size): - if model == "None": + if not model or model == "None": return pp.image = rembg.remove( From c9c30cf3bbcfea3c2a800c9effcd2eb0549bd3fb Mon Sep 17 00:00:00 2001 From: catboxanon <122327233+catboxanon@users.noreply.github.com> Date: Tue, 30 May 2023 00:26:12 +0000 Subject: [PATCH 2/5] Bump to 2.0.38, support DIS --- install.py | 2 +- scripts/postprocessing_rembg.py | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/install.py b/install.py index 5c13936..1a73e3c 100644 --- a/install.py +++ b/install.py @@ -1,7 +1,7 @@ import launch if not launch.is_installed("rembg"): - launch.run_pip("install rembg==2.0.30 --no-deps", "rembg") + launch.run_pip("install rembg==2.0.38 --no-deps", "rembg") for dep in ['onnxruntime', 'pymatting', 'pooch']: if not launch.is_installed(dep): diff --git a/scripts/postprocessing_rembg.py b/scripts/postprocessing_rembg.py index 50b3fec..8d64332 100644 --- a/scripts/postprocessing_rembg.py +++ b/scripts/postprocessing_rembg.py @@ -11,6 +11,8 @@ models = [ "u2net_human_seg", "u2net_cloth_seg", "silueta", + "isnet-general-use", + "isnet-anime", ] class ScriptPostprocessingUpscale(scripts_postprocessing.ScriptPostprocessing): From 88bd36ab34e05c10fb0cc4d1e95163fc6ae30f35 Mon Sep 17 00:00:00 2001 From: ccfco Date: Fri, 2 Jun 2023 11:16:38 +0800 Subject: [PATCH 3/5] feat: add rembg api --- scripts/api.py | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 scripts/api.py diff --git a/scripts/api.py b/scripts/api.py new file mode 100644 index 0000000..0a0abd3 --- /dev/null +++ b/scripts/api.py @@ -0,0 +1,52 @@ +from fastapi import FastAPI, Body + +from modules.api.models import * +from modules.api import api +import gradio as gr + +import rembg + +# models = [ +# "None", +# "u2net", +# "u2netp", +# "u2net_human_seg", +# "u2net_cloth_seg", +# "silueta", +# ] + + +def rembg_api(_: gr.Blocks, app: FastAPI): + @app.post("/rembg") + async def rembg_remove( + input_image: str = Body("", title='rembg input image'), + model: str = Body("u2net", title='rembg model'), + return_mask: bool = Body(False, title='return mask'), + alpha_matting: bool = Body(False, title='alpha matting'), + alpha_matting_foreground_threshold: int = Body(240, title='alpha matting foreground threshold'), + alpha_matting_background_threshold: int = Body(10, title='alpha matting background threshold'), + alpha_matting_erode_size: int = Body(10, title='alpha matting erode size') + ): + if not model or model == "None": + return + + input_image = api.decode_base64_to_image(input_image) + + image = rembg.remove( + input_image, + session=rembg.new_session(model), + only_mask=return_mask, + alpha_matting=alpha_matting, + alpha_matting_foreground_threshold=alpha_matting_foreground_threshold, + alpha_matting_background_threshold=alpha_matting_background_threshold, + alpha_matting_erode_size=alpha_matting_erode_size, + ) + + return {"image": api.encode_pil_to_base64(image).decode("utf-8")} + +try: + import modules.script_callbacks as script_callbacks + + script_callbacks.on_app_started(rembg_api) +except: + pass From 1c4a698cc829e760509121ffa6fd6e1d0a05a011 Mon Sep 17 00:00:00 2001 From: h3rmit Date: Mon, 28 Aug 2023 01:45:27 +0300 Subject: [PATCH 4/5] Changed the default download location of u2net models to /models/u2net --- scripts/postprocessing_rembg.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/scripts/postprocessing_rembg.py b/scripts/postprocessing_rembg.py index 8d64332..9e8eaee 100644 --- a/scripts/postprocessing_rembg.py +++ b/scripts/postprocessing_rembg.py @@ -2,7 +2,9 @@ from modules import scripts_postprocessing import gradio as gr from modules.ui_components import FormRow +from modules.paths_internal import models_path import rembg +import os models = [ "None", @@ -50,6 +52,9 @@ class ScriptPostprocessingUpscale(scripts_postprocessing.ScriptPostprocessing): if not model or model == "None": return + if "U2NET_HOME" not in os.environ: + os.environ["U2NET_HOME"] = os.path.join(models_path, "u2net") + pp.image = rembg.remove( pp.image, session=rembg.new_session(model), From 47165253bc9b9735f3adc4b2180a1410507fd351 Mon Sep 17 00:00:00 2001 From: AUTOMATIC <-> Date: Sat, 30 Dec 2023 15:59:59 +0300 Subject: [PATCH 5/5] use InputAccordion from 1.6.0 --- scripts/postprocessing_rembg.py | 35 +++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/scripts/postprocessing_rembg.py b/scripts/postprocessing_rembg.py index 8d64332..c293a5c 100644 --- a/scripts/postprocessing_rembg.py +++ b/scripts/postprocessing_rembg.py @@ -1,4 +1,4 @@ -from modules import scripts_postprocessing +from modules import scripts_postprocessing, ui_components import gradio as gr from modules.ui_components import FormRow @@ -21,23 +21,25 @@ class ScriptPostprocessingUpscale(scripts_postprocessing.ScriptPostprocessing): model = None def ui(self): - with FormRow(): - model = gr.Dropdown(label="Remove background", choices=models, value="None") - return_mask = gr.Checkbox(label="Return mask", value=False) - alpha_matting = gr.Checkbox(label="Alpha matting", value=False) + with ui_components.InputAccordion(False, label="Remove background") as enable: + with gr.Row(): + model = gr.Dropdown(label="Remove background", choices=models, value="None") + return_mask = gr.Checkbox(label="Return mask", value=False) + alpha_matting = gr.Checkbox(label="Alpha matting", value=False) - with FormRow(visible=False) as alpha_mask_row: - alpha_matting_erode_size = gr.Slider(label="Erode size", minimum=0, maximum=40, step=1, value=10) - alpha_matting_foreground_threshold = gr.Slider(label="Foreground threshold", minimum=0, maximum=255, step=1, value=240) - alpha_matting_background_threshold = gr.Slider(label="Background threshold", minimum=0, maximum=255, step=1, value=10) + with gr.Row(visible=False) as alpha_mask_row: + alpha_matting_erode_size = gr.Slider(label="Erode size", minimum=0, maximum=40, step=1, value=10) + alpha_matting_foreground_threshold = gr.Slider(label="Foreground threshold", minimum=0, maximum=255, step=1, value=240) + alpha_matting_background_threshold = gr.Slider(label="Background threshold", minimum=0, maximum=255, step=1, value=10) - alpha_matting.change( - fn=lambda x: gr.update(visible=x), - inputs=[alpha_matting], - outputs=[alpha_mask_row], - ) + alpha_matting.change( + fn=lambda x: gr.update(visible=x), + inputs=[alpha_matting], + outputs=[alpha_mask_row], + ) return { + "enable": enable, "model": model, "return_mask": return_mask, "alpha_matting": alpha_matting, @@ -46,7 +48,10 @@ class ScriptPostprocessingUpscale(scripts_postprocessing.ScriptPostprocessing): "alpha_matting_erode_size": alpha_matting_erode_size, } - def process(self, pp: scripts_postprocessing.PostprocessedImage, model, return_mask, alpha_matting, alpha_matting_foreground_threshold, alpha_matting_background_threshold, alpha_matting_erode_size): + def process(self, pp: scripts_postprocessing.PostprocessedImage, enable, model, return_mask, alpha_matting, alpha_matting_foreground_threshold, alpha_matting_background_threshold, alpha_matting_erode_size): + if not enable: + return + if not model or model == "None": return