init app
Browse files- .gitignore +3 -0
- app.py +93 -0
- requirements.txt +3 -0
- utils.py +6 -0
.gitignore
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
.idea/
|
2 |
+
__pycache__/
|
3 |
+
playground.py
|
app.py
ADDED
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import PIL.Image
|
2 |
+
import gradio as gr
|
3 |
+
from PIL import ImageColor
|
4 |
+
from rembg import new_session, remove
|
5 |
+
|
6 |
+
from utils import *
|
7 |
+
|
8 |
+
remove_bg_models = {
|
9 |
+
"U2NET": "u2net",
|
10 |
+
"U2NET Human Seg": "u2net_human_seg",
|
11 |
+
"U2NET Cloth Seg": "u2net_cloth_seg"
|
12 |
+
}
|
13 |
+
|
14 |
+
model_choices = keys(remove_bg_models)
|
15 |
+
|
16 |
+
|
17 |
+
def alpha_matting(state):
|
18 |
+
if state:
|
19 |
+
return 270, 20, 11
|
20 |
+
return 0, 0, 0
|
21 |
+
|
22 |
+
|
23 |
+
def predict(image,
|
24 |
+
session,
|
25 |
+
matting,
|
26 |
+
only_mask,
|
27 |
+
post_process_mask,
|
28 |
+
foreground_threshold,
|
29 |
+
background_threshold,
|
30 |
+
matting_erode_size,
|
31 |
+
new_bg_color,
|
32 |
+
bg_color,
|
33 |
+
transparency):
|
34 |
+
session = new_session(remove_bg_models[session])
|
35 |
+
|
36 |
+
if new_bg_color:
|
37 |
+
r, g, b, _ = ImageColor.getcolor(bg_color, "RGBA")
|
38 |
+
bg_color = r, g, b, transparency
|
39 |
+
else:
|
40 |
+
bg_color = None
|
41 |
+
|
42 |
+
return remove(data=image,
|
43 |
+
session=session,
|
44 |
+
alpha_matting=matting,
|
45 |
+
only_mask=only_mask,
|
46 |
+
post_process_mask=post_process_mask,
|
47 |
+
bgcolor=bg_color,
|
48 |
+
alpha_matting_foreground_threshold=foreground_threshold,
|
49 |
+
alpha_matting_background_threshold=background_threshold,
|
50 |
+
alpha_matting_erode_size=matting_erode_size)
|
51 |
+
|
52 |
+
|
53 |
+
footer = r"""
|
54 |
+
<center>
|
55 |
+
<b>
|
56 |
+
Demo based on <a href='https://github.com/danielgatis/rembg'>Rembg</a>
|
57 |
+
</b>
|
58 |
+
</center>
|
59 |
+
"""
|
60 |
+
|
61 |
+
with gr.Blocks(title="Remove background") as app:
|
62 |
+
gr.HTML("<center><h1>Remove Background Tool</h1></center>")
|
63 |
+
with gr.Row(equal_height=False):
|
64 |
+
with gr.Column():
|
65 |
+
input_img = gr.Image(type="numpy", label="Input image")
|
66 |
+
drp_models = gr.Dropdown(choices=model_choices, label="Model Segment", value="U2NET")
|
67 |
+
with gr.Row():
|
68 |
+
chk_alm = gr.Checkbox(label="Alpha Matting", value=False)
|
69 |
+
chk_psm = gr.Checkbox(label="Post process mask", value=False)
|
70 |
+
chk_msk = gr.Checkbox(label="Only Mask", value=False)
|
71 |
+
sld_aft = gr.Slider(0, 300, value=0, step=1, label="Alpha matting foreground threshold")
|
72 |
+
sld_amb = gr.Slider(0, 50, value=0, step=1, label="Alpha matting background threshold")
|
73 |
+
sld_aes = gr.Slider(0, 20, value=0, step=1, label="Alpha matting erode size")
|
74 |
+
with gr.Box():
|
75 |
+
with gr.Row():
|
76 |
+
chk_col = gr.Checkbox(label="Change background color", value=False)
|
77 |
+
color = gr.ColorPicker(label="Pick a new color")
|
78 |
+
trans = gr.Number(label="Transparency level", value=255, precision=0, minimum=0, maximum=255)
|
79 |
+
run_btn = gr.Button(value="Remove background", variant="primary")
|
80 |
+
with gr.Column():
|
81 |
+
output_img = gr.Image(type="pil", label="result")
|
82 |
+
gr.ClearButton(components=[input_img, output_img])
|
83 |
+
|
84 |
+
chk_alm.change(alpha_matting, inputs=[chk_alm], outputs=[sld_aft, sld_amb, sld_aes])
|
85 |
+
|
86 |
+
run_btn.click(predict, [input_img, drp_models,
|
87 |
+
chk_alm, chk_msk, chk_psm,
|
88 |
+
sld_aft, sld_amb, sld_aes, chk_col, color, trans], [output_img])
|
89 |
+
|
90 |
+
with gr.Row():
|
91 |
+
gr.HTML(footer)
|
92 |
+
|
93 |
+
app.launch(share=False, debug=True, enable_queue=True, show_error=True)
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
rembg~=2.0.47
|
2 |
+
pillow~=9.5.0
|
3 |
+
opencv-python-headless
|
utils.py
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
def keys(dictionary: dict):
|
2 |
+
return [k for k, v in dictionary.items()]
|
3 |
+
|
4 |
+
|
5 |
+
def split_numbers(numbers: str):
|
6 |
+
return [int(i) for i in numbers.split(",")]
|