Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,22 +1,41 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from rembg import remove
|
| 3 |
-
from PIL import Image
|
|
|
|
| 4 |
|
| 5 |
-
def remove_bg(
|
| 6 |
-
if
|
| 7 |
return None
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
output = remove(image)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
return output
|
| 11 |
|
| 12 |
# Creating Gradio interface
|
| 13 |
iface = gr.Interface(
|
| 14 |
fn=remove_bg,
|
| 15 |
-
inputs=
|
|
|
|
|
|
|
|
|
|
| 16 |
outputs=gr.Image(type="pil"),
|
| 17 |
-
title="AI Background Remover",
|
| 18 |
-
description="Upload an image
|
| 19 |
)
|
| 20 |
|
| 21 |
iface.launch()
|
| 22 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from rembg import remove
|
| 3 |
+
from PIL import Image, ImageDraw
|
| 4 |
+
import io
|
| 5 |
|
| 6 |
+
def remove_bg(image, points):
|
| 7 |
+
if image is None:
|
| 8 |
return None
|
| 9 |
+
|
| 10 |
+
# Load image
|
| 11 |
+
image = Image.open(image).convert("RGBA")
|
| 12 |
+
|
| 13 |
+
# Convert points format
|
| 14 |
+
keep_points = [tuple(p["coordinates"]) for p in points if p["label"] == "keep"]
|
| 15 |
+
remove_points = [tuple(p["coordinates"]) for p in points if p["label"] == "remove"]
|
| 16 |
+
|
| 17 |
+
# Apply automatic background removal
|
| 18 |
output = remove(image)
|
| 19 |
+
|
| 20 |
+
# Apply manual refinement (simple demonstration using circles)
|
| 21 |
+
draw = ImageDraw.Draw(output)
|
| 22 |
+
for point in keep_points:
|
| 23 |
+
draw.ellipse([point[0]-5, point[1]-5, point[0]+5, point[1]+5], fill=(255, 255, 255, 255))
|
| 24 |
+
for point in remove_points:
|
| 25 |
+
draw.ellipse([point[0]-5, point[1]-5, point[0]+5, point[1]+5], fill=(0, 0, 0, 0))
|
| 26 |
+
|
| 27 |
return output
|
| 28 |
|
| 29 |
# Creating Gradio interface
|
| 30 |
iface = gr.Interface(
|
| 31 |
fn=remove_bg,
|
| 32 |
+
inputs=[
|
| 33 |
+
gr.Image(type="filepath"),
|
| 34 |
+
gr.AnnotatedImage(choices=["keep", "remove"], label="Refine Background")
|
| 35 |
+
],
|
| 36 |
outputs=gr.Image(type="pil"),
|
| 37 |
+
title="AI Background Remover with Refinement",
|
| 38 |
+
description="Upload an image, remove its background, and refine the result using + (keep) and - (remove) points!",
|
| 39 |
)
|
| 40 |
|
| 41 |
iface.launch()
|
|
|