File size: 1,058 Bytes
0972e74
a31dda9
0972e74
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# Install required libraries
#!pip install ultralytics opencv-python matplotlib gradio

import gradio as gr
import cv2
import numpy as np
from ultralytics import YOLO

# Load YOLOv8 model
model = YOLO("yolov8s.pt")

def detect_objects(image):
    """
    Function to perform object detection using YOLOv8.
    """
    # Convert image to RGB (Gradio provides images in numpy array format)
    image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

    # Run YOLOv8 inference
    results = model(image_rgb)

    # Convert the annotated image back to OpenCV format
    annotated_image = results[0].plot()  # Get annotated image

    return annotated_image  # Return the image with bounding boxes

# Create a Gradio interface
interface = gr.Interface(
    fn=detect_objects,  # Function to call
    inputs=gr.Image(type="numpy"),  # Input: Image
    outputs=gr.Image(type="numpy"),  # Output: Processed Image
    title="YOLOv8 Object Detection",
    description="Upload an image, and YOLOv8 will detect objects in it."
)

# Launch the Gradio app
interface.launch()