Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from PIL import Image
|
4 |
+
import requests
|
5 |
+
|
6 |
+
# Load the trained YOLOv8 model
|
7 |
+
model = torch.hub.load('ultralytics/yolov8', 'custom', path='best.pt')
|
8 |
+
|
9 |
+
def detect_objects(image):
|
10 |
+
# Run the YOLOv8 model on the input image
|
11 |
+
results = model(image)
|
12 |
+
|
13 |
+
# Extract the bounding boxes and labels from the results
|
14 |
+
boxes = results.xyxy[0].numpy() # Bounding boxes
|
15 |
+
labels = results.names # Labels
|
16 |
+
|
17 |
+
# Convert the image to a PIL Image for display
|
18 |
+
img = Image.fromarray(results.render()[0])
|
19 |
+
|
20 |
+
return img
|
21 |
+
|
22 |
+
# Create the Gradio interface
|
23 |
+
iface = gr.Interface(
|
24 |
+
fn=detect_objects,
|
25 |
+
inputs=gr.inputs.Image(type="pil"),
|
26 |
+
outputs="image",
|
27 |
+
title="YOLOv8 Object Detection",
|
28 |
+
description="Upload an image to detect objects using the YOLOv8 model."
|
29 |
+
)
|
30 |
+
|
31 |
+
# Launch the interface
|
32 |
+
iface.launch()
|