Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import pipeline
|
| 2 |
+
from transformers.utils import logging
|
| 3 |
+
from helper import load_image_from_url, render_results_in_image
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import gradio as gr
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
# Reduce the verbosity of transformers and suppress warnings
|
| 9 |
+
logging.set_verbosity_error()
|
| 10 |
+
|
| 11 |
+
# Load the object detection pipeline
|
| 12 |
+
od_pipe = pipeline("object-detection", "facebook/detr-resnet-50")
|
| 13 |
+
|
| 14 |
+
def get_pipeline_prediction(pil_image):
|
| 15 |
+
# Perform object detection
|
| 16 |
+
pipeline_output = od_pipe(pil_image)
|
| 17 |
+
# Render results on the image
|
| 18 |
+
processed_image = render_results_in_image(pil_image, pipeline_output)
|
| 19 |
+
return processed_image
|
| 20 |
+
|
| 21 |
+
# Custom CSS to improve the interface
|
| 22 |
+
css = """
|
| 23 |
+
body { font-family: Arial, sans-serif; }
|
| 24 |
+
button { background-color: #4CAF50; color: white; border: none; padding: 10px 20px; }
|
| 25 |
+
"""
|
| 26 |
+
|
| 27 |
+
# Set up the Gradio Blocks interface
|
| 28 |
+
with gr.Blocks(css=css) as demo:
|
| 29 |
+
gr.Markdown("## Object Detection Service")
|
| 30 |
+
gr.Markdown("Upload an image and see the object detection results rendered on the image.")
|
| 31 |
+
with gr.Row():
|
| 32 |
+
image_input = gr.Image(label="Upload Image", type="pil", tool="editor")
|
| 33 |
+
submit_button = gr.Button("Detect Objects")
|
| 34 |
+
output_image = gr.Image(label="Detected Objects")
|
| 35 |
+
|
| 36 |
+
submit_button.click(
|
| 37 |
+
get_pipeline_prediction,
|
| 38 |
+
inputs=[image_input],
|
| 39 |
+
outputs=[output_image]
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
# Launch the Gradio app
|
| 43 |
+
demo.launch(share=True, server_port=int(os.environ.get('PORT1', 7860))) # Default port 7860 if PORT1 is not set
|