Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,37 +1,32 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
| 2 |
from PIL import Image
|
| 3 |
-
|
| 4 |
-
import torch
|
| 5 |
-
from transformers import DPTForDepthEstimation, DPTFeatureExtractor
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
model = DPTForDepthEstimation.from_pretrained(model_name)
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
outputs
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
#
|
| 24 |
-
|
| 25 |
-
depth_min, depth_max = depth.min(), depth.max()
|
| 26 |
-
normalized_depth = (depth - depth_min) / (depth_max - depth_min)
|
| 27 |
-
|
| 28 |
-
# Simple threshold to create mask (background = 1)
|
| 29 |
-
import numpy as np
|
| 30 |
-
mask = (normalized_depth < 0.6).astype(np.uint8) * 255
|
| 31 |
-
|
| 32 |
-
# Apply mask to image
|
| 33 |
-
image_np = np.array(image)
|
| 34 |
-
image_rgba = np.dstack([image_np, mask]) # add alpha channel
|
| 35 |
-
result = Image.fromarray(image_rgba)
|
| 36 |
-
result.save("output.png")
|
| 37 |
-
print("Background removed and saved as output.png")
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from rembg import remove
|
| 4 |
from PIL import Image
|
| 5 |
+
import io
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
# Function to remove background
|
| 8 |
+
def remove_background(image):
|
| 9 |
+
# Convert uploaded image to PIL
|
| 10 |
+
pil_image = Image.fromarray(image).convert("RGBA")
|
| 11 |
+
|
| 12 |
+
# Remove background
|
| 13 |
+
result = remove(pil_image)
|
| 14 |
+
|
| 15 |
+
# Convert result to displayable format
|
| 16 |
+
return result
|
| 17 |
|
| 18 |
+
# Gradio UI
|
| 19 |
+
title = "AI Background Remover"
|
| 20 |
+
description = "Upload an image and remove its background instantly! Made by Viateur Irasubiza."
|
|
|
|
| 21 |
|
| 22 |
+
iface = gr.Interface(
|
| 23 |
+
fn=remove_background,
|
| 24 |
+
inputs=gr.Image(type="numpy", label="Upload Your Image"),
|
| 25 |
+
outputs=gr.Image(type="pil", label="Result"),
|
| 26 |
+
title=title,
|
| 27 |
+
description=description,
|
| 28 |
+
allow_flagging="never"
|
| 29 |
+
)
|
| 30 |
|
| 31 |
+
# Launch the app
|
| 32 |
+
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|