Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from rembg import remove
|
| 3 |
-
from PIL import Image
|
| 4 |
import numpy as np
|
| 5 |
import io
|
| 6 |
|
|
@@ -11,11 +11,19 @@ def remove_bg(image):
|
|
| 11 |
output = remove(image)
|
| 12 |
return output
|
| 13 |
|
| 14 |
-
def refine_bg(image,
|
| 15 |
-
if image is None or
|
| 16 |
return None
|
| 17 |
image = Image.open(image).convert("RGBA")
|
| 18 |
-
mask = Image.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
refined = Image.composite(image, Image.new("RGBA", image.size, (0, 0, 0, 0)), mask)
|
| 20 |
return refined
|
| 21 |
|
|
@@ -30,10 +38,17 @@ with gr.Blocks() as iface:
|
|
| 30 |
remove_btn = gr.Button("Remove Background")
|
| 31 |
remove_btn.click(remove_bg, inputs=input_img, outputs=output_img)
|
| 32 |
|
| 33 |
-
gr.
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from rembg import remove
|
| 3 |
+
from PIL import Image, ImageDraw
|
| 4 |
import numpy as np
|
| 5 |
import io
|
| 6 |
|
|
|
|
| 11 |
output = remove(image)
|
| 12 |
return output
|
| 13 |
|
| 14 |
+
def refine_bg(image, points, threshold):
|
| 15 |
+
if image is None or points is None:
|
| 16 |
return None
|
| 17 |
image = Image.open(image).convert("RGBA")
|
| 18 |
+
mask = Image.new("L", image.size, 0)
|
| 19 |
+
draw = ImageDraw.Draw(mask)
|
| 20 |
+
|
| 21 |
+
for point in points:
|
| 22 |
+
x, y, action = point # action: 1 for keep (+), 0 for remove (-)
|
| 23 |
+
radius = int(threshold * 50) # Scale the threshold to radius size
|
| 24 |
+
color = 255 if action == 1 else 0 # White for keep, Black for remove
|
| 25 |
+
draw.ellipse((x - radius, y - radius, x + radius, y + radius), fill=color)
|
| 26 |
+
|
| 27 |
refined = Image.composite(image, Image.new("RGBA", image.size, (0, 0, 0, 0)), mask)
|
| 28 |
return refined
|
| 29 |
|
|
|
|
| 38 |
remove_btn = gr.Button("Remove Background")
|
| 39 |
remove_btn.click(remove_bg, inputs=input_img, outputs=output_img)
|
| 40 |
|
| 41 |
+
with gr.Column(visible=False) as refine_section:
|
| 42 |
+
gr.Markdown("### Refine Background")
|
| 43 |
+
refine_editor = gr.Image(type="filepath", label="Tap to Add Points (+ to Keep, - to Remove)", tool="editor")
|
| 44 |
+
threshold_slider = gr.Slider(0.00, 1.00, value=0.5, step=0.01, label="Threshold")
|
| 45 |
+
refine_btn = gr.Button("Apply Refinements")
|
| 46 |
+
refine_btn.click(refine_bg, inputs=[input_img, refine_editor, threshold_slider], outputs=output_img)
|
| 47 |
+
|
| 48 |
+
def show_refine_section(image):
|
| 49 |
+
return gr.update(visible=True)
|
| 50 |
+
|
| 51 |
+
refine_toggle_btn = gr.Button("Refine")
|
| 52 |
+
refine_toggle_btn.click(show_refine_section, inputs=input_img, outputs=refine_section)
|
| 53 |
|
| 54 |
+
iface.launch()
|