Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,26 +5,29 @@ import io
|
|
| 5 |
import numpy as np
|
| 6 |
|
| 7 |
def remove_bg(image):
|
| 8 |
-
#
|
| 9 |
-
if isinstance(image, str): #
|
| 10 |
-
|
| 11 |
-
elif isinstance(image, np.ndarray): #
|
| 12 |
-
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
#
|
| 15 |
img_byte_arr = io.BytesIO()
|
| 16 |
-
|
| 17 |
output_bytes = remove(img_byte_arr.getvalue())
|
| 18 |
return Image.open(io.BytesIO(output_bytes))
|
| 19 |
|
| 20 |
-
#
|
| 21 |
iface = gr.Interface(
|
| 22 |
fn=remove_bg,
|
| 23 |
-
inputs=gr.Image(type="filepath"), # Important for API
|
| 24 |
-
outputs=gr.Image(),
|
| 25 |
title="Background Remover",
|
| 26 |
-
|
| 27 |
)
|
| 28 |
|
|
|
|
| 29 |
if __name__ == "__main__":
|
| 30 |
iface.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 5 |
import numpy as np
|
| 6 |
|
| 7 |
def remove_bg(image):
|
| 8 |
+
# Convert input to PIL Image
|
| 9 |
+
if isinstance(image, str): # file path
|
| 10 |
+
img = Image.open(image)
|
| 11 |
+
elif isinstance(image, np.ndarray): # numpy array from Gradio
|
| 12 |
+
img = Image.fromarray(image)
|
| 13 |
+
else:
|
| 14 |
+
raise ValueError("Unsupported input type")
|
| 15 |
|
| 16 |
+
# Process with rembg
|
| 17 |
img_byte_arr = io.BytesIO()
|
| 18 |
+
img.save(img_byte_arr, format='PNG')
|
| 19 |
output_bytes = remove(img_byte_arr.getvalue())
|
| 20 |
return Image.open(io.BytesIO(output_bytes))
|
| 21 |
|
| 22 |
+
# Define the interface
|
| 23 |
iface = gr.Interface(
|
| 24 |
fn=remove_bg,
|
| 25 |
+
inputs=gr.Image(type="filepath", label="Input Image"), # Important: use "filepath" for API compatibility
|
| 26 |
+
outputs=gr.Image(label="Output Image"),
|
| 27 |
title="Background Remover",
|
| 28 |
+
description="Upload an image to remove its background"
|
| 29 |
)
|
| 30 |
|
| 31 |
+
# Launch the app
|
| 32 |
if __name__ == "__main__":
|
| 33 |
iface.launch(server_name="0.0.0.0", server_port=7860)
|