Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Install required libraries
|
| 2 |
+
!pip install ultralytics opencv-python matplotlib gradio
|
| 3 |
+
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import cv2
|
| 6 |
+
import numpy as np
|
| 7 |
+
from ultralytics import YOLO
|
| 8 |
+
|
| 9 |
+
# Load YOLOv8 model
|
| 10 |
+
model = YOLO("yolov8s.pt")
|
| 11 |
+
|
| 12 |
+
def detect_objects(image):
|
| 13 |
+
"""
|
| 14 |
+
Function to perform object detection using YOLOv8.
|
| 15 |
+
"""
|
| 16 |
+
# Convert image to RGB (Gradio provides images in numpy array format)
|
| 17 |
+
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 18 |
+
|
| 19 |
+
# Run YOLOv8 inference
|
| 20 |
+
results = model(image_rgb)
|
| 21 |
+
|
| 22 |
+
# Convert the annotated image back to OpenCV format
|
| 23 |
+
annotated_image = results[0].plot() # Get annotated image
|
| 24 |
+
|
| 25 |
+
return annotated_image # Return the image with bounding boxes
|
| 26 |
+
|
| 27 |
+
# Create a Gradio interface
|
| 28 |
+
interface = gr.Interface(
|
| 29 |
+
fn=detect_objects, # Function to call
|
| 30 |
+
inputs=gr.Image(type="numpy"), # Input: Image
|
| 31 |
+
outputs=gr.Image(type="numpy"), # Output: Processed Image
|
| 32 |
+
title="YOLOv8 Object Detection",
|
| 33 |
+
description="Upload an image, and YOLOv8 will detect objects in it."
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
# Launch the Gradio app
|
| 37 |
+
interface.launch()
|