from ultralytics import YOLO from PIL import Image import numpy as np import gradio as gr # Load a YOLOv8 model (YOLOv8n is the smallest and fastest version) model = YOLO('yolov8n.pt') def detect_objects(image): results = model(image) rendered_image = results[0].plot() # This returns a numpy array with the bounding boxes drawn return Image.fromarray(rendered_image) # Convert the numpy array back to an image # Define the Gradio interface interface = gr.Interface( fn=detect_objects, # Function to process the input inputs=gr.Image(type="pil"), # Input is an image outputs=gr.Image(type="pil"), # Output is an image title="YOLOv8 Object Detection", description="Upload an image to detect objects using YOLOv8." ) # Launch the Gradio interface interface.launch(share=True)