File size: 948 Bytes
164487e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Import required libraries
import torch
import gradio as gr
from PIL import Image

# Load the pretrained YOLOv5 model
model = torch.hub.load('ultralytics/yolov5', 'yolov5x', pretrained=True)

# Function to process the image and return detections
def detect_objects(image):
    # Perform inference on the uploaded image
    results = model(image)
    
    # Plot results on the image (YOLOv5 provides results with bounding boxes, class names, and confidence scores)
    results_img = results.render()[0]  # Render the detections on the image
    
    # Convert to a PIL Image for compatibility with Gradio
    return Image.fromarray(results_img)

# Define the Gradio interface
interface = gr.Interface(
    fn=detect_objects,
    inputs=gr.Image(type="pil"),
    outputs=gr.Image(type="pil"),
    title="Object Detection App",
    description="Upload an image to detect objects using the YOLOv5 model."
)

# Launch the Gradio app
interface.launch()