Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Import required libraries
|
| 2 |
+
import torch
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from PIL import Image
|
| 5 |
+
|
| 6 |
+
# Load the pretrained YOLOv5 model
|
| 7 |
+
model = torch.hub.load('ultralytics/yolov5', 'yolov5x', pretrained=True)
|
| 8 |
+
|
| 9 |
+
# Function to process the image and return detections
|
| 10 |
+
def detect_objects(image):
|
| 11 |
+
# Perform inference on the uploaded image
|
| 12 |
+
results = model(image)
|
| 13 |
+
|
| 14 |
+
# Plot results on the image (YOLOv5 provides results with bounding boxes, class names, and confidence scores)
|
| 15 |
+
results_img = results.render()[0] # Render the detections on the image
|
| 16 |
+
|
| 17 |
+
# Convert to a PIL Image for compatibility with Gradio
|
| 18 |
+
return Image.fromarray(results_img)
|
| 19 |
+
|
| 20 |
+
# Define the Gradio interface
|
| 21 |
+
interface = gr.Interface(
|
| 22 |
+
fn=detect_objects,
|
| 23 |
+
inputs=gr.Image(type="pil"),
|
| 24 |
+
outputs=gr.Image(type="pil"),
|
| 25 |
+
title="Object Detection App",
|
| 26 |
+
description="Upload an image to detect objects using the YOLOv5 model."
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
# Launch the Gradio app
|
| 30 |
+
interface.launch()
|