Spaces:
Runtime error
Runtime error
File size: 1,285 Bytes
5d75401 |
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 |
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()
|