Detectify / app.py
arpit13's picture
Create app.py
107566b verified
raw
history blame
898 Bytes
import gradio as gr
import cv2
import torch
import numpy as np
# Load the YOLOv5 model
model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True)
# Function to run inference on an image
def run_inference(image):
# Convert the image from PIL format to a format compatible with OpenCV
image = np.array(image)
# Run YOLOv5 inference
results = model(image)
# Convert the annotated image from BGR to RGB for display
annotated_image = results.render()[0]
annotated_image = cv2.cvtColor(annotated_image, cv2.COLOR_BGR2RGB)
return annotated_image
# Create the Gradio interface
interface = gr.Interface(
fn=run_inference,
inputs=gr.Image(type="pil"),
outputs=gr.Image(type="pil"),
title="YOLOv5 Object Detection",
description="Upload an image to run YOLOv5 object detection and see the results."
)
# Launch the app
interface.launch()