Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import cv2
|
| 3 |
+
import torch
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
# Load the YOLOv5 model
|
| 7 |
+
model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True)
|
| 8 |
+
|
| 9 |
+
# Function to run inference on an image
|
| 10 |
+
def run_inference(image):
|
| 11 |
+
# Convert the image from PIL format to a format compatible with OpenCV
|
| 12 |
+
image = np.array(image)
|
| 13 |
+
|
| 14 |
+
# Run YOLOv5 inference
|
| 15 |
+
results = model(image)
|
| 16 |
+
|
| 17 |
+
# Convert the annotated image from BGR to RGB for display
|
| 18 |
+
annotated_image = results.render()[0]
|
| 19 |
+
annotated_image = cv2.cvtColor(annotated_image, cv2.COLOR_BGR2RGB)
|
| 20 |
+
|
| 21 |
+
return annotated_image
|
| 22 |
+
|
| 23 |
+
# Create the Gradio interface
|
| 24 |
+
interface = gr.Interface(
|
| 25 |
+
fn=run_inference,
|
| 26 |
+
inputs=gr.Image(type="pil"),
|
| 27 |
+
outputs=gr.Image(type="pil"),
|
| 28 |
+
title="YOLOv5 Object Detection",
|
| 29 |
+
description="Upload an image to run YOLOv5 object detection and see the results."
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
# Launch the app
|
| 33 |
+
interface.launch()
|