File size: 1,235 Bytes
a84f7fb
 
 
 
871f642
a84f7fb
 
 
 
 
 
 
 
 
 
 
 
871f642
a84f7fb
 
 
 
871f642
a84f7fb
 
 
871f642
a84f7fb
 
 
 
 
 
 
 
871f642
a84f7fb
 
 
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


import torch
import cv2
import gradio as gr
from pathlib import Path

# Load the YOLOv5 model
MODEL_PATH = "best.pt"  # Path to your custom model
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = torch.hub.load('ultralytics/yolov5', 'custom', path=MODEL_PATH, force_reload=True).to(device)

# Define the prediction function
def predict(image):
    # Convert the input image (numpy) to a compatible format
    img_bgr = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
    results = model(img_bgr)  # Run inference

    # Render results
    annotated_image = results.render()[0]
    annotated_image_rgb = cv2.cvtColor(annotated_image, cv2.COLOR_BGR2RGB)
    return annotated_image_rgb

# Gradio Interface
title = "Custom YOLOv5 Model Deployment"
description = "Upload an image to get predictions using a custom YOLOv5 model trained with your dataset."

interface = gr.Interface(
    fn=predict,                  # Prediction function
    inputs=gr.Image(type="numpy", label="Upload Image"),
    outputs=gr.Image(type="numpy", label="Detected Objects"),
    title=title,
    description=description,
    live=True  # Enable live processing
)

# Launch the Gradio app
if __name__ == "__main__":
    interface.launch()