Spaces:
Running
Running
from ultralytics import YOLO | |
import torch | |
import cv2 | |
import numpy as np | |
import gradio as gr | |
from PIL import Image | |
# Load YOLOv5 model | |
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') | |
model = YOLO("yolov5s.pt") # Load pre-trained YOLOv5s model | |
model.to(device) | |
model.eval() | |
# Load COCO class labels | |
CLASS_NAMES = model.names # YOLOv5's built-in class names | |
def preprocess_image(image): | |
image = Image.fromarray(image) | |
image = image.convert("RGB") | |
return image | |
def detect_objects(image): | |
image = preprocess_image(image) | |
results = model.predict(image) # Run YOLOv5 inference | |
# Convert results to bounding box format | |
image = np.array(image) | |
for result in results: | |
for box, cls in zip(result.boxes.xyxy, result.boxes.cls): | |
x1, y1, x2, y2 = map(int, box[:4]) | |
class_name = CLASS_NAMES[int(cls)] # Get class name | |
# Draw bounding box | |
cv2.rectangle(image, (x1, y1), (x2, y2), (255, 0, 0), 2) | |
# Put class label | |
cv2.putText(image, class_name, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, | |
0.5, (255, 0, 0), 2, cv2.LINE_AA) | |
return image | |
# Gradio UI | |
iface = gr.Interface( | |
fn=detect_objects, | |
inputs=gr.Image(type="numpy"), | |
outputs=gr.Image(type="numpy"), | |
live=True, | |
) | |
iface.launch() | |