This commit is contained in:
AUTOMATIC
2023-03-12 18:32:00 +03:00
commit 64821f0476
6 changed files with 106 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
__pycache__

21
LICENSE Normal file
View File

@@ -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.

15
README.md Normal file
View File

@@ -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

8
install.py Normal file
View File

@@ -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")

BIN
preview.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1017 KiB

View File

@@ -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