Update app.py
Browse files
app.py
CHANGED
@@ -1,8 +1,6 @@
|
|
1 |
import logging
|
2 |
-
|
3 |
import gradio as gr
|
4 |
from rembg import new_session
|
5 |
-
|
6 |
from cutter import remove, make_label
|
7 |
from utils import *
|
8 |
|
@@ -12,116 +10,28 @@ remove_bg_models = {
|
|
12 |
"U2NET Cloth Seg": "u2net_cloth_seg"
|
13 |
}
|
14 |
|
15 |
-
model_choices = keys(
|
16 |
-
|
17 |
|
18 |
def predict(image, session, smoot, matting, bg_color):
|
19 |
-
|
20 |
session = new_session(remove_bg_models[session])
|
21 |
-
|
22 |
try:
|
23 |
return remove(session, image, smoot, matting, bg_color)
|
24 |
except ValueError as err:
|
25 |
logging.error(err)
|
26 |
return make_label(str(err)), None
|
27 |
|
28 |
-
|
29 |
-
def change_show_mask(chk_state):
|
30 |
-
return gr.Image.update(visible=chk_state)
|
31 |
-
|
32 |
-
|
33 |
-
def change_include_matting(chk_state):
|
34 |
-
return gr.Box.update(visible=chk_state), (0, 0, 0), 0, 0, 0
|
35 |
-
|
36 |
-
|
37 |
-
def change_foreground_threshold(fg_value, value):
|
38 |
-
fg, bg, erode = value
|
39 |
-
return fg_value, bg, erode
|
40 |
-
|
41 |
-
|
42 |
-
def change_background_threshold(bg_value, value):
|
43 |
-
fg, bg, erode = value
|
44 |
-
return fg, bg_value, erode
|
45 |
-
|
46 |
-
|
47 |
-
def change_erode_size(erode_value, value):
|
48 |
-
fg, bg, erode = value
|
49 |
-
return fg, bg, erode_value
|
50 |
-
|
51 |
-
|
52 |
-
def set_dominant_color(chk_state):
|
53 |
-
return chk_state, gr.ColorPicker.update(value=False, visible=not chk_state)
|
54 |
-
|
55 |
-
|
56 |
-
def change_picker_color(picker, dominant):
|
57 |
-
if not dominant:
|
58 |
-
return picker
|
59 |
-
return dominant
|
60 |
-
|
61 |
-
|
62 |
-
def change_background_mode(chk_state):
|
63 |
-
return gr.ColorPicker.update(value=False, visible=chk_state), \
|
64 |
-
gr.Checkbox.update(value=False, visible=chk_state)
|
65 |
-
|
66 |
-
|
67 |
-
footer = r"""
|
68 |
-
|
69 |
-
"""
|
70 |
-
|
71 |
with gr.Blocks(title="Remove background") as app:
|
72 |
-
color_state = gr.State(value=False)
|
73 |
-
matting_state = gr.State(value=(0, 0, 0))
|
74 |
-
|
75 |
gr.HTML("<center><h1>Background Remover</h1></center>")
|
76 |
with gr.Row(equal_height=False):
|
77 |
with gr.Column():
|
78 |
input_img = gr.Image(type="pil", label="Input image")
|
79 |
-
drp_models = gr.Dropdown(choices=model_choices, label="Model Segment", value="U2NET")
|
80 |
-
with gr.Row():
|
81 |
-
chk_include_matting = gr.Checkbox(label="Matting", value=False)
|
82 |
-
chk_smoot_mask = gr.Checkbox(label="Smoot Mask", value=False)
|
83 |
-
chk_show_mask = gr.Checkbox(label="Show Mask", value=False)
|
84 |
-
with gr.Box(visible=False) as slider_matting:
|
85 |
-
slr_fg_threshold = gr.Slider(0, 300, value=270, step=1, label="Alpha matting foreground threshold")
|
86 |
-
slr_bg_threshold = gr.Slider(0, 50, value=20, step=1, label="Alpha matting background threshold")
|
87 |
-
slr_erode_size = gr.Slider(0, 20, value=11, step=1, label="Alpha matting erode size")
|
88 |
-
with gr.Box():
|
89 |
-
with gr.Row():
|
90 |
-
chk_change_color = gr.Checkbox(label="Change background color", value=False)
|
91 |
-
pkr_color = gr.ColorPicker(label="Pick a new color", visible=False)
|
92 |
-
chk_dominant = gr.Checkbox(label="Use dominant color", value=False, visible=False)
|
93 |
run_btn = gr.Button(value="Remove background", variant="primary")
|
|
|
94 |
with gr.Column():
|
95 |
-
output_img = gr.Image(type="pil", label="
|
96 |
-
mask_img = gr.Image(type="pil", label="Image Mask", visible=False)
|
97 |
-
gr.ClearButton(components=[input_img, output_img, mask_img])
|
98 |
-
|
99 |
-
chk_include_matting.change(change_include_matting, inputs=[chk_include_matting],
|
100 |
-
outputs=[slider_matting, matting_state,
|
101 |
-
slr_fg_threshold, slr_bg_threshold, slr_erode_size])
|
102 |
-
|
103 |
-
slr_fg_threshold.change(change_foreground_threshold, inputs=[slr_fg_threshold, matting_state],
|
104 |
-
outputs=[matting_state])
|
105 |
-
|
106 |
-
slr_bg_threshold.change(change_background_threshold, inputs=[slr_bg_threshold, matting_state],
|
107 |
-
outputs=[matting_state])
|
108 |
-
|
109 |
-
slr_erode_size.change(change_erode_size, inputs=[slr_erode_size, matting_state],
|
110 |
-
outputs=[matting_state])
|
111 |
-
|
112 |
-
chk_show_mask.change(change_show_mask, inputs=[chk_show_mask], outputs=[mask_img])
|
113 |
-
|
114 |
-
chk_change_color.change(change_background_mode, inputs=[chk_change_color],
|
115 |
-
outputs=[pkr_color, chk_dominant])
|
116 |
-
|
117 |
-
pkr_color.change(change_picker_color, inputs=[pkr_color, chk_dominant], outputs=[color_state])
|
118 |
-
|
119 |
-
chk_dominant.change(set_dominant_color, inputs=[chk_dominant], outputs=[color_state, pkr_color])
|
120 |
-
|
121 |
-
run_btn.click(predict, inputs=[input_img, drp_models, chk_smoot_mask, matting_state, color_state],
|
122 |
-
outputs=[output_img, mask_img])
|
123 |
|
124 |
-
|
125 |
-
|
|
|
126 |
|
127 |
app.launch(share=False, debug=True, enable_queue=True, show_error=True)
|
|
|
1 |
import logging
|
|
|
2 |
import gradio as gr
|
3 |
from rembg import new_session
|
|
|
4 |
from cutter import remove, make_label
|
5 |
from utils import *
|
6 |
|
|
|
10 |
"U2NET Cloth Seg": "u2net_cloth_seg"
|
11 |
}
|
12 |
|
13 |
+
model_choices = list(remove_bg_models.keys())
|
|
|
14 |
|
15 |
def predict(image, session, smoot, matting, bg_color):
|
|
|
16 |
session = new_session(remove_bg_models[session])
|
|
|
17 |
try:
|
18 |
return remove(session, image, smoot, matting, bg_color)
|
19 |
except ValueError as err:
|
20 |
logging.error(err)
|
21 |
return make_label(str(err)), None
|
22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
with gr.Blocks(title="Remove background") as app:
|
|
|
|
|
|
|
24 |
gr.HTML("<center><h1>Background Remover</h1></center>")
|
25 |
with gr.Row(equal_height=False):
|
26 |
with gr.Column():
|
27 |
input_img = gr.Image(type="pil", label="Input image")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
run_btn = gr.Button(value="Remove background", variant="primary")
|
29 |
+
clear_btn = gr.Button(value="Clear", variant="secondary")
|
30 |
with gr.Column():
|
31 |
+
output_img = gr.Image(type="pil", label="Result image")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
+
run_btn.click(predict, inputs=[input_img, gr.Dropdown(choices=model_choices, value="U2NET"), False, None, None],
|
34 |
+
outputs=[output_img])
|
35 |
+
clear_btn.click(lambda: (None, None), inputs=None, outputs=[input_img, output_img])
|
36 |
|
37 |
app.launch(share=False, debug=True, enable_queue=True, show_error=True)
|