Spaces:
Runtime error
Runtime error
import gradio as gr | |
import torch | |
from PIL import Image | |
import requests | |
from io import BytesIO | |
# Load YOLOv5 pre-trained model from Hugging Face | |
model = torch.hub.load('ultralytics/yolov5', 'yolov5s') # You can choose other versions like yolov5m or yolov5l | |
# Function for object detection | |
def detect_objects(input_image): | |
# If the input is a URL, download the image | |
if isinstance(input_image, str): | |
response = requests.get(input_image) | |
img = Image.open(BytesIO(response.content)) | |
else: | |
img = Image.fromarray(input_image) | |
# Run YOLOv5 object detection | |
results = model(img) | |
# Render results on image | |
results.render() # Render boxes on the image | |
# Return image with detections | |
output_image = results.imgs[0] | |
return Image.fromarray(output_image) | |
# Create Gradio interface | |
interface = gr.Interface( | |
fn=detect_objects, | |
inputs=gr.inputs.Image(type="numpy", label="Upload an image"), | |
outputs=gr.outputs.Image(type="pil", label="Detected Image"), | |
title="YOLOv5 Object Detection", | |
description="Upload an image and detect objects using YOLOv5 model. The model can identify objects like people, cars, animals, and more.", | |
theme="huggingface" | |
) | |
# Launch the interface | |
interface.launch() | |