Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,39 +3,32 @@ from rembg import remove
|
|
| 3 |
from PIL import Image, ImageDraw
|
| 4 |
import io
|
| 5 |
|
| 6 |
-
def remove_bg(image,
|
| 7 |
if image is None:
|
| 8 |
return None
|
|
|
|
| 9 |
|
| 10 |
-
#
|
| 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
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 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 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
gr.
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
)
|
|
|
|
| 40 |
|
| 41 |
iface.launch()
|
|
|
|
| 3 |
from PIL import Image, ImageDraw
|
| 4 |
import io
|
| 5 |
|
| 6 |
+
def remove_bg(image, mask=None):
|
| 7 |
if image is None:
|
| 8 |
return None
|
| 9 |
+
image = Image.open(image)
|
| 10 |
|
| 11 |
+
# Apply background removal
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
output = remove(image)
|
| 13 |
|
| 14 |
+
# Apply manual mask refinements if provided
|
| 15 |
+
if mask:
|
| 16 |
+
mask_image = Image.open(mask).convert("L")
|
| 17 |
+
output.paste(image, mask=mask_image)
|
|
|
|
|
|
|
| 18 |
|
| 19 |
return output
|
| 20 |
|
| 21 |
# Creating Gradio interface
|
| 22 |
+
with gr.Blocks() as iface:
|
| 23 |
+
gr.Markdown("# AI Background Remover with Refinement")
|
| 24 |
+
|
| 25 |
+
with gr.Row():
|
| 26 |
+
input_img = gr.Image(type="filepath", label="Upload Image")
|
| 27 |
+
mask_img = gr.Image(type="filepath", label="Upload Mask (Optional)")
|
| 28 |
+
|
| 29 |
+
output_img = gr.Image(type="pil", label="Output Image")
|
| 30 |
+
|
| 31 |
+
process_btn = gr.Button("Remove Background")
|
| 32 |
+
process_btn.click(remove_bg, inputs=[input_img, mask_img], outputs=output_img)
|
| 33 |
|
| 34 |
iface.launch()
|