commit 64821f04767b04b92ac157f3c02f28d269dba5d8 Author: AUTOMATIC <16777216c@gmail.com> Date: Sun Mar 12 18:32:00 2023 +0300 first! diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bee8a64 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +__pycache__ diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..99d862f --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2023 AUTOMATIC1111 + +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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..47c2ab1 --- /dev/null +++ b/README.md @@ -0,0 +1,15 @@ +# Rembg + +Extension for [webui](https://github.com/AUTOMATIC1111/stable-diffusion-webui). Removes backgrounds from pictures. + +![](preview.png) + +Find the UI for rembg in the Extras tab after installing the extension. + +# Installation + +Install from webui's Extensions tab. + +# Credits + +* rembg library that does all the work: https://github.com/danielgatis/rembg diff --git a/install.py b/install.py new file mode 100644 index 0000000..5c13936 --- /dev/null +++ b/install.py @@ -0,0 +1,8 @@ +import launch + +if not launch.is_installed("rembg"): + launch.run_pip("install rembg==2.0.30 --no-deps", "rembg") + +for dep in ['onnxruntime', 'pymatting', 'pooch']: + if not launch.is_installed(dep): + launch.run_pip(f"install {dep}", f"{dep} for REMBG extension") diff --git a/preview.png b/preview.png new file mode 100644 index 0000000..c1af3d1 Binary files /dev/null and b/preview.png differ diff --git a/scripts/postprocessing_rembg.py b/scripts/postprocessing_rembg.py new file mode 100644 index 0000000..da80e86 --- /dev/null +++ b/scripts/postprocessing_rembg.py @@ -0,0 +1,61 @@ +from modules import scripts_postprocessing +import gradio as gr + +from modules.ui_components import FormRow +import rembg + +models = [ + "None", + "u2net", + "u2netp", + "u2net_human_seg", + "u2net_cloth_seg", + "silueta", +] + +class ScriptPostprocessingUpscale(scripts_postprocessing.ScriptPostprocessing): + name = "Rembg" + order = 20000 + 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 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) + + alpha_matting.change( + fn=lambda x: gr.update(visible=x), + inputs=[alpha_matting], + outputs=[alpha_mask_row], + ) + + return { + "model": model, + "return_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, + } + + 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": + return + + pp.image = rembg.remove( + pp.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, + ) + + pp.info["Rembg"] = model