Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,33 +1,36 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
from PIL import Image
|
| 3 |
-
import
|
| 4 |
import os
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
def
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
output_img_path = "output_result.png"
|
| 12 |
-
image.save(output_img_path)
|
| 13 |
-
return output_img_path, f"Predicted Species: {prediction}"
|
| 14 |
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
example_images = [
|
| 17 |
-
"examples/
|
| 18 |
-
"examples/
|
| 19 |
-
"examples/
|
| 20 |
-
"examples/
|
| 21 |
-
"examples/
|
| 22 |
-
"examples/
|
| 23 |
-
"examples/green_parrot.jpg",
|
| 24 |
-
"examples/palm_cockatoo.jpg",
|
| 25 |
-
"examples/yellow_parrot.jpg",
|
| 26 |
]
|
| 27 |
|
| 28 |
-
with gr.Blocks(title="
|
| 29 |
-
gr.Markdown("
|
| 30 |
-
gr.Markdown("
|
| 31 |
|
| 32 |
with gr.Row():
|
| 33 |
with gr.Column():
|
|
@@ -36,11 +39,11 @@ with gr.Blocks(title="Parrot60 Classifier") as demo:
|
|
| 36 |
clear_btn = gr.Button("Clear")
|
| 37 |
with gr.Column():
|
| 38 |
output_image = gr.Image(label="Output", type="filepath")
|
| 39 |
-
output_text = gr.Textbox(label="
|
| 40 |
download_output = gr.File(label="Download Output")
|
| 41 |
|
| 42 |
with gr.Row():
|
| 43 |
-
gr.Markdown("### 🧪
|
| 44 |
with gr.Row():
|
| 45 |
gr.Examples(
|
| 46 |
examples=example_images,
|
|
@@ -48,12 +51,8 @@ with gr.Blocks(title="Parrot60 Classifier") as demo:
|
|
| 48 |
label="Click to try an example",
|
| 49 |
)
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
return result_path, label, result_path
|
| 54 |
-
|
| 55 |
-
submit_btn.click(fn=run_pipeline, inputs=image_input, outputs=[output_image, output_text, download_output])
|
| 56 |
clear_btn.click(fn=lambda: (None, "", None), outputs=[image_input, output_text, download_output])
|
| 57 |
|
| 58 |
-
# Launch app
|
| 59 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from rembg import remove
|
| 3 |
from PIL import Image
|
| 4 |
+
import io
|
| 5 |
import os
|
| 6 |
|
| 7 |
+
# Background removal function
|
| 8 |
+
def remove_background(image):
|
| 9 |
+
input_bytes = io.BytesIO()
|
| 10 |
+
image.save(input_bytes, format='PNG')
|
| 11 |
+
input_bytes = input_bytes.getvalue()
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
output_bytes = remove(input_bytes)
|
| 14 |
+
output_img = Image.open(io.BytesIO(output_bytes))
|
| 15 |
+
|
| 16 |
+
output_path = "output.png"
|
| 17 |
+
output_img.save(output_path, format="PNG")
|
| 18 |
+
|
| 19 |
+
return output_path, "Background removed successfully!", output_path
|
| 20 |
+
|
| 21 |
+
# Example images (replace with actual sample image paths)
|
| 22 |
example_images = [
|
| 23 |
+
"examples/person1.jpg",
|
| 24 |
+
"examples/product1.jpg",
|
| 25 |
+
"examples/cat.jpg",
|
| 26 |
+
"examples/dog.jpg",
|
| 27 |
+
"examples/portrait.jpg",
|
| 28 |
+
"examples/bird.jpg"
|
|
|
|
|
|
|
|
|
|
| 29 |
]
|
| 30 |
|
| 31 |
+
with gr.Blocks(title="Background Remover") as demo:
|
| 32 |
+
gr.Markdown("# 🖼️ Background Remover")
|
| 33 |
+
gr.Markdown("Upload an image or use an example to remove its background.")
|
| 34 |
|
| 35 |
with gr.Row():
|
| 36 |
with gr.Column():
|
|
|
|
| 39 |
clear_btn = gr.Button("Clear")
|
| 40 |
with gr.Column():
|
| 41 |
output_image = gr.Image(label="Output", type="filepath")
|
| 42 |
+
output_text = gr.Textbox(label="Status", lines=1)
|
| 43 |
download_output = gr.File(label="Download Output")
|
| 44 |
|
| 45 |
with gr.Row():
|
| 46 |
+
gr.Markdown("### 🧪 Example Images")
|
| 47 |
with gr.Row():
|
| 48 |
gr.Examples(
|
| 49 |
examples=example_images,
|
|
|
|
| 51 |
label="Click to try an example",
|
| 52 |
)
|
| 53 |
|
| 54 |
+
# Hook logic
|
| 55 |
+
submit_btn.click(fn=remove_background, inputs=image_input, outputs=[output_image, output_text, download_output])
|
|
|
|
|
|
|
|
|
|
| 56 |
clear_btn.click(fn=lambda: (None, "", None), outputs=[image_input, output_text, download_output])
|
| 57 |
|
|
|
|
| 58 |
demo.launch()
|