Spaces:
Sleeping
Sleeping
File size: 1,326 Bytes
0bbf6ef |
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 |
import gradio as gr
from ultralytics import YOLO
import cv2
import numpy as np
# Load the trained model
model_path = 'best.pt' # Replace with the path to your trained .pt file
model = YOLO(model_path)
# Function to perform inference on an image
def infer_image(image):
# Convert the image from BGR to RGB
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# Perform inference
results = model(image_rgb)
# Extract results and annotate image
for result in results:
for box in result.boxes:
x1, y1, x2, y2 = box.xyxy[0]
cls = int(box.cls[0])
conf = float(box.conf[0])
# Draw bounding box
cv2.rectangle(image, (int(x1), int(y1)), (int(x2), int(y2)), (0, 255, 0), 2)
# Draw label
label = f'{model.names[cls]} {conf:.2f}'
cv2.putText(image, label, (int(x1), int(y1) - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
return image
# Create Gradio interface
iface = gr.Interface(
fn=infer_image,
inputs=gr.Image(type="numpy", label="Upload an Image"),
outputs=gr.Image(type="numpy", label="Annotated Image"),
title="YOLOv8 Inference",
description="Upload an image to get object detection results using YOLOv8."
)
# Launch the app
iface.launch()
|