Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from ultralytics import YOLO
|
3 |
+
|
4 |
+
# Load your trained YOLOv8n model
|
5 |
+
# Ensure 'yolov8n.pt' is in the repository or provide the correct path.
|
6 |
+
model = YOLO("yolov8n.pt")
|
7 |
+
|
8 |
+
def detect(image):
|
9 |
+
"""
|
10 |
+
Runs object detection on the input image.
|
11 |
+
|
12 |
+
Args:
|
13 |
+
image: A numpy array representing the input image.
|
14 |
+
|
15 |
+
Returns:
|
16 |
+
Annotated image as a numpy array.
|
17 |
+
"""
|
18 |
+
# Run inference
|
19 |
+
results = model(image)
|
20 |
+
|
21 |
+
# Use the first result and plot detections on the image
|
22 |
+
annotated_image = results[0].plot()
|
23 |
+
return annotated_image
|
24 |
+
|
25 |
+
# Build Gradio Interface
|
26 |
+
iface = gr.Interface(
|
27 |
+
fn=detect,
|
28 |
+
inputs=gr.Image(type="numpy", label="Input Image"),
|
29 |
+
outputs=gr.Image(type="numpy", label="Detection Output"),
|
30 |
+
title="YOLOv8n Object Detection",
|
31 |
+
description="Upload an image and the model will detect objects using YOLOv8n."
|
32 |
+
)
|
33 |
+
|
34 |
+
# Launch the interface (Hugging Face Spaces will automatically run the app)
|
35 |
+
if __name__ == "__main__":
|
36 |
+
iface.launch()
|