Spaces:
Sleeping
Sleeping
Florent Moulon commited on
Commit ·
7d26f9a
1
Parent(s): 0b7ccc3
Add YOLOv8 object detection app with Gradio interface and dependencies
Browse files- app.py +33 -0
- requirements.txt +5 -0
app.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from ultralytics import YOLO
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import cv2
|
| 4 |
+
import numpy as np
|
| 5 |
+
from PIL import Image
|
| 6 |
+
|
| 7 |
+
# Load your fine-tuned YOLOv8 model
|
| 8 |
+
model = YOLO("best.pt")
|
| 9 |
+
|
| 10 |
+
# Function to perform object detection
|
| 11 |
+
def detect_objects(image):
|
| 12 |
+
image = np.array(image) # Convert PIL image to numpy
|
| 13 |
+
results = model(image) # Run YOLOv8 inference
|
| 14 |
+
|
| 15 |
+
# Draw bounding boxes
|
| 16 |
+
for result in results:
|
| 17 |
+
for box in result.boxes.xyxy.cpu().numpy(): # Extract bounding boxes
|
| 18 |
+
x1, y1, x2, y2 = map(int, box[:4])
|
| 19 |
+
cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 3) # Green box
|
| 20 |
+
|
| 21 |
+
return Image.fromarray(image) # Convert back to PIL image
|
| 22 |
+
|
| 23 |
+
# Gradio interface
|
| 24 |
+
interface = gr.Interface(
|
| 25 |
+
fn=detect_objects,
|
| 26 |
+
inputs=gr.Image(type="pil"),
|
| 27 |
+
outputs=gr.Image(type="pil"),
|
| 28 |
+
title="YOLOv8 Object Detection",
|
| 29 |
+
description="Upload an image and detect objects using a fine-tuned YOLOv8 model."
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
# Launch the app
|
| 33 |
+
interface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
ultralytics
|
| 2 |
+
gradio
|
| 3 |
+
torch
|
| 4 |
+
torchvision
|
| 5 |
+
opencv-python
|