Spaces:
Running
Running
File size: 2,122 Bytes
caff61e e82b28e bccf53b dc80d48 4fa263e caff61e 35669c6 e82b28e 248b9ce 35669c6 36e1064 35669c6 a29d5e2 4fa263e e82b28e 4fa263e 35669c6 4fa263e 35669c6 73df658 35669c6 4fa263e a29d5e2 35669c6 e82b28e 1195707 4fa263e e82b28e 46e3370 35669c6 |
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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
import torch
import cv2
import numpy as np
import gradio as gr
from PIL import Image
# Device configuration
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# Load YOLOv5x model
model = torch.hub.load('ultralytics/yolov5', 'yolov5x', pretrained=True).to(device)
# Generate distinct colors for each class using HSV color space
def generate_distinct_colors(num_classes):
colors = {}
for i, class_name in enumerate(model.names):
# Use HSV to generate evenly distributed hues
hue = (i * 255 // num_classes)
# Convert HSV to BGR (OpenCV uses BGR)
hsv_color = np.uint8([[[hue, 255, 255]]])
bgr_color = cv2.cvtColor(hsv_color, cv2.COLOR_HSV2BGR)[0][0]
# Store as tuple for easier use
colors[class_name] = tuple(map(int, bgr_color))
return colors
# Generate colors once at startup
CLASS_COLORS = generate_distinct_colors(len(model.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(image)
image = np.array(image)
# Process all detections at once
detections = results.xyxy[0]
for *box, conf, cls in detections:
x1, y1, x2, y2 = map(int, box)
class_name = model.names[int(cls)]
confidence = conf.item() * 100
color = CLASS_COLORS[class_name]
# Draw rectangle and label
cv2.rectangle(image, (x1, y1), (x2, y2), color, 4)
label = f"{class_name} ({confidence:.1f}%)"
cv2.putText(image, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 1, color, 3, cv2.LINE_AA)
return image
# Gradio interface
iface = gr.Interface(
fn=detect_objects,
inputs=gr.Image(type="numpy", label="Upload Image"),
outputs=gr.Image(type="numpy", label="Detected Objects"),
title="Object Detection with YOLOv5",
description="Use webcam or upload an image to detect objects.",
allow_flagging="never",
examples=["spring_street_after.jpg", "pexels-hikaique-109919.jpg"]
)
iface.launch() |