update gradio api
Browse files- .gitignore +1 -0
- app.py +9 -8
- cutter.py +14 -4
- requirements.txt +5 -2
.gitignore
CHANGED
@@ -1,3 +1,4 @@
|
|
1 |
.idea/
|
|
|
2 |
__pycache__/
|
3 |
playground.py
|
|
|
1 |
.idea/
|
2 |
+
.gradio/
|
3 |
__pycache__/
|
4 |
playground.py
|
app.py
CHANGED
@@ -27,11 +27,11 @@ def predict(image, session, smoot, matting, bg_color):
|
|
27 |
|
28 |
|
29 |
def change_show_mask(chk_state):
|
30 |
-
return gr.Image
|
31 |
|
32 |
|
33 |
def change_include_matting(chk_state):
|
34 |
-
return gr.
|
35 |
|
36 |
|
37 |
def change_foreground_threshold(fg_value, value):
|
@@ -50,7 +50,7 @@ def change_erode_size(erode_value, value):
|
|
50 |
|
51 |
|
52 |
def set_dominant_color(chk_state):
|
53 |
-
return chk_state, gr.ColorPicker
|
54 |
|
55 |
|
56 |
def change_picker_color(picker, dominant):
|
@@ -60,8 +60,8 @@ def change_picker_color(picker, dominant):
|
|
60 |
|
61 |
|
62 |
def change_background_mode(chk_state):
|
63 |
-
return gr.ColorPicker
|
64 |
-
gr.Checkbox
|
65 |
|
66 |
|
67 |
footer = r"""
|
@@ -85,11 +85,11 @@ with gr.Blocks(title="Remove background") as app:
|
|
85 |
chk_include_matting = gr.Checkbox(label="Matting", value=False)
|
86 |
chk_smoot_mask = gr.Checkbox(label="Smoot Mask", value=False)
|
87 |
chk_show_mask = gr.Checkbox(label="Show Mask", value=False)
|
88 |
-
with gr.
|
89 |
slr_fg_threshold = gr.Slider(0, 300, value=270, step=1, label="Alpha matting foreground threshold")
|
90 |
slr_bg_threshold = gr.Slider(0, 50, value=20, step=1, label="Alpha matting background threshold")
|
91 |
slr_erode_size = gr.Slider(0, 20, value=11, step=1, label="Alpha matting erode size")
|
92 |
-
with gr.
|
93 |
with gr.Row():
|
94 |
chk_change_color = gr.Checkbox(label="Change background color", value=False)
|
95 |
pkr_color = gr.ColorPicker(label="Pick a new color", visible=False)
|
@@ -128,4 +128,5 @@ with gr.Blocks(title="Remove background") as app:
|
|
128 |
with gr.Row():
|
129 |
gr.HTML(footer)
|
130 |
|
131 |
-
app.
|
|
|
|
27 |
|
28 |
|
29 |
def change_show_mask(chk_state):
|
30 |
+
return gr.Image(visible=chk_state)
|
31 |
|
32 |
|
33 |
def change_include_matting(chk_state):
|
34 |
+
return gr.Group(visible=chk_state), (0, 0, 0), 0, 0, 0
|
35 |
|
36 |
|
37 |
def change_foreground_threshold(fg_value, value):
|
|
|
50 |
|
51 |
|
52 |
def set_dominant_color(chk_state):
|
53 |
+
return chk_state, gr.ColorPicker(visible=not chk_state)
|
54 |
|
55 |
|
56 |
def change_picker_color(picker, dominant):
|
|
|
60 |
|
61 |
|
62 |
def change_background_mode(chk_state):
|
63 |
+
return gr.ColorPicker(visible=chk_state), \
|
64 |
+
gr.Checkbox(value=False, visible=chk_state)
|
65 |
|
66 |
|
67 |
footer = r"""
|
|
|
85 |
chk_include_matting = gr.Checkbox(label="Matting", value=False)
|
86 |
chk_smoot_mask = gr.Checkbox(label="Smoot Mask", value=False)
|
87 |
chk_show_mask = gr.Checkbox(label="Show Mask", value=False)
|
88 |
+
with gr.Group(visible=False) as slider_matting:
|
89 |
slr_fg_threshold = gr.Slider(0, 300, value=270, step=1, label="Alpha matting foreground threshold")
|
90 |
slr_bg_threshold = gr.Slider(0, 50, value=20, step=1, label="Alpha matting background threshold")
|
91 |
slr_erode_size = gr.Slider(0, 20, value=11, step=1, label="Alpha matting erode size")
|
92 |
+
with gr.Group():
|
93 |
with gr.Row():
|
94 |
chk_change_color = gr.Checkbox(label="Change background color", value=False)
|
95 |
pkr_color = gr.ColorPicker(label="Pick a new color", visible=False)
|
|
|
128 |
with gr.Row():
|
129 |
gr.HTML(footer)
|
130 |
|
131 |
+
app.queue()
|
132 |
+
app.launch(share=False, debug=True, show_error=True)
|
cutter.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
import PIL
|
2 |
import numpy as np
|
3 |
-
from PIL import Image,
|
4 |
from PIL.Image import Image as PILImage
|
5 |
from pymatting.alpha.estimate_alpha_cf import estimate_alpha_cf
|
6 |
from pymatting.foreground.estimate_foreground_ml import estimate_foreground_ml
|
@@ -84,15 +84,25 @@ def remove(session, img: PILImage, smoot: bool, matting: tuple, color) -> (PILIm
|
|
84 |
color = get_background_dominant_color(img, mask)
|
85 |
cutout = apply_background_color(cutout, color)
|
86 |
elif isinstance(color, str):
|
87 |
-
|
88 |
-
cutout = apply_background_color(cutout, (r, g, b, 255))
|
89 |
|
90 |
return cutout, mask
|
91 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
|
93 |
def make_label(text, width=600, height=200, color="black") -> PILImage:
|
94 |
image = Image.new("RGB", (width, height), color)
|
95 |
draw = ImageDraw.Draw(image)
|
96 |
-
text_width, text_height = draw
|
97 |
draw.text(((width-text_width)/2, height/2), text)
|
98 |
return image
|
|
|
1 |
import PIL
|
2 |
import numpy as np
|
3 |
+
from PIL import Image, ImageDraw
|
4 |
from PIL.Image import Image as PILImage
|
5 |
from pymatting.alpha.estimate_alpha_cf import estimate_alpha_cf
|
6 |
from pymatting.foreground.estimate_foreground_ml import estimate_foreground_ml
|
|
|
84 |
color = get_background_dominant_color(img, mask)
|
85 |
cutout = apply_background_color(cutout, color)
|
86 |
elif isinstance(color, str):
|
87 |
+
cutout = apply_background_color(cutout, parse_rgba(color))
|
|
|
88 |
|
89 |
return cutout, mask
|
90 |
|
91 |
+
def parse_rgba(color_str):
|
92 |
+
color_values = color_str[5:-1].split(',')
|
93 |
+
r = int(float(color_values[0].strip()))
|
94 |
+
g = int(float(color_values[1].strip()))
|
95 |
+
b = int(float(color_values[2].strip()))
|
96 |
+
a = int(float(color_values[3].strip()) * 255) # Alpha scaled to 0-255
|
97 |
+
return r, g, b, a
|
98 |
+
|
99 |
+
def text_size(draw, text):
|
100 |
+
_, _, width, height = draw.textbbox((0, 0), text=text)
|
101 |
+
return width, height
|
102 |
|
103 |
def make_label(text, width=600, height=200, color="black") -> PILImage:
|
104 |
image = Image.new("RGB", (width, height), color)
|
105 |
draw = ImageDraw.Draw(image)
|
106 |
+
text_width, text_height = text_size(draw, text)
|
107 |
draw.text(((width-text_width)/2, height/2), text)
|
108 |
return image
|
requirements.txt
CHANGED
@@ -1,4 +1,7 @@
|
|
1 |
rembg~=2.0.47
|
2 |
-
pillow~=
|
3 |
-
pymatting
|
4 |
opencv-python-headless
|
|
|
|
|
|
|
|
1 |
rembg~=2.0.47
|
2 |
+
pillow~=10.4.0
|
3 |
+
pymatting~=1.1.12
|
4 |
opencv-python-headless
|
5 |
+
gradio~=5.1.0
|
6 |
+
numpy~=1.26.4
|
7 |
+
scipy~=1.14.1
|