Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,68 +2,69 @@ import gradio as gr
|
|
| 2 |
from rembg import remove
|
| 3 |
from PIL import Image, ImageDraw
|
| 4 |
import numpy as np
|
|
|
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
selected_points = []
|
| 8 |
-
|
| 9 |
def remove_bg(image):
|
| 10 |
if image is None:
|
| 11 |
-
return None
|
| 12 |
-
|
| 13 |
-
image = Image.open(image).convert("RGBA")
|
| 14 |
output = remove(image)
|
| 15 |
-
return output
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
if image is None:
|
| 20 |
return None
|
| 21 |
|
| 22 |
-
image = image.convert("RGBA")
|
| 23 |
mask = Image.new("L", image.size, 0)
|
| 24 |
draw = ImageDraw.Draw(mask)
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
| 29 |
|
| 30 |
refined = Image.composite(image, Image.new("RGBA", image.size, (0, 0, 0, 0)), mask)
|
| 31 |
return refined
|
| 32 |
|
|
|
|
| 33 |
with gr.Blocks() as iface:
|
| 34 |
gr.Markdown("# AI Background Remover")
|
| 35 |
-
|
| 36 |
with gr.Row():
|
| 37 |
input_img = gr.Image(type="filepath", label="Upload Image")
|
| 38 |
-
output_img = gr.Image(type="pil", label="Output Image"
|
| 39 |
-
|
| 40 |
remove_btn = gr.Button("Remove Background")
|
|
|
|
| 41 |
|
| 42 |
refine_btn = gr.Button("Refine")
|
| 43 |
-
|
| 44 |
|
| 45 |
-
with
|
|
|
|
|
|
|
| 46 |
keep_btn = gr.Button("+ (Keep)")
|
| 47 |
remove_btn = gr.Button("- (Remove)")
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
-
refine_mode = gr.State("keep") # Default mode: Keep
|
| 52 |
-
|
| 53 |
def set_mode_keep():
|
| 54 |
-
return "keep"
|
| 55 |
|
| 56 |
def set_mode_remove():
|
| 57 |
-
return "remove"
|
| 58 |
-
|
| 59 |
-
remove_btn.click(remove_bg, inputs=input_img, outputs=[output_img, output_img])
|
| 60 |
-
keep_btn.click(set_mode_keep, outputs=refine_mode)
|
| 61 |
-
remove_btn.click(set_mode_remove, outputs=refine_mode)
|
| 62 |
-
|
| 63 |
-
def refine_with_clicks(image, evt: gr.SelectData, mode, threshold):
|
| 64 |
-
""" Captures user clicks and applies AI-assisted refinement. """
|
| 65 |
-
return add_point(image, evt.index[0], evt.index[1], mode, threshold)
|
| 66 |
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
iface.launch()
|
|
|
|
| 2 |
from rembg import remove
|
| 3 |
from PIL import Image, ImageDraw
|
| 4 |
import numpy as np
|
| 5 |
+
import io
|
| 6 |
|
| 7 |
+
# Function to remove background automatically
|
|
|
|
|
|
|
| 8 |
def remove_bg(image):
|
| 9 |
if image is None:
|
| 10 |
+
return None
|
| 11 |
+
image = Image.open(image)
|
|
|
|
| 12 |
output = remove(image)
|
| 13 |
+
return output
|
| 14 |
|
| 15 |
+
# Function to refine background using + and - points
|
| 16 |
+
def refine_bg(image, points, threshold, mode):
|
| 17 |
if image is None:
|
| 18 |
return None
|
| 19 |
|
| 20 |
+
image = Image.open(image).convert("RGBA")
|
| 21 |
mask = Image.new("L", image.size, 0)
|
| 22 |
draw = ImageDraw.Draw(mask)
|
| 23 |
|
| 24 |
+
for point in points:
|
| 25 |
+
x, y = point
|
| 26 |
+
color = 255 if mode == "keep" else 0
|
| 27 |
+
draw.ellipse((x - threshold * 10, y - threshold * 10, x + threshold * 10, y + threshold * 10), fill=color)
|
| 28 |
|
| 29 |
refined = Image.composite(image, Image.new("RGBA", image.size, (0, 0, 0, 0)), mask)
|
| 30 |
return refined
|
| 31 |
|
| 32 |
+
# Gradio UI
|
| 33 |
with gr.Blocks() as iface:
|
| 34 |
gr.Markdown("# AI Background Remover")
|
| 35 |
+
|
| 36 |
with gr.Row():
|
| 37 |
input_img = gr.Image(type="filepath", label="Upload Image")
|
| 38 |
+
output_img = gr.Image(type="pil", label="Output Image")
|
| 39 |
+
|
| 40 |
remove_btn = gr.Button("Remove Background")
|
| 41 |
+
remove_btn.click(remove_bg, inputs=input_img, outputs=output_img)
|
| 42 |
|
| 43 |
refine_btn = gr.Button("Refine")
|
| 44 |
+
refine_options = gr.Column(visible=False)
|
| 45 |
|
| 46 |
+
with refine_options:
|
| 47 |
+
gr.Markdown("### Refine Background")
|
| 48 |
+
refine_editor = gr.Image(type="filepath", label="Tap to Add Points", interactive=True)
|
| 49 |
keep_btn = gr.Button("+ (Keep)")
|
| 50 |
remove_btn = gr.Button("- (Remove)")
|
| 51 |
+
threshold_slider = gr.Slider(0.00, 1.00, value=0.5, step=0.01, label="Threshold")
|
| 52 |
+
apply_refine_btn = gr.Button("Apply Refinements")
|
| 53 |
+
|
| 54 |
+
refine_mode = gr.State("keep")
|
| 55 |
+
|
| 56 |
+
def show_refine_section(image):
|
| 57 |
+
return gr.update(visible=True), image # Show section and keep image visible
|
| 58 |
|
|
|
|
|
|
|
| 59 |
def set_mode_keep():
|
| 60 |
+
return "keep", "+ (Keep) Selected"
|
| 61 |
|
| 62 |
def set_mode_remove():
|
| 63 |
+
return "remove", "- (Remove) Selected"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
+
refine_btn.click(show_refine_section, inputs=input_img, outputs=[refine_options, refine_editor])
|
| 66 |
+
keep_btn.click(set_mode_keep, outputs=[refine_mode, keep_btn])
|
| 67 |
+
remove_btn.click(set_mode_remove, outputs=[refine_mode, remove_btn])
|
| 68 |
+
apply_refine_btn.click(refine_bg, inputs=[input_img, refine_editor, threshold_slider, refine_mode], outputs=output_img)
|
| 69 |
|
| 70 |
iface.launch()
|