Spaces:
Build error
Build error
| # Install required libraries | |
| #!pip install ultralytics opencv-python matplotlib gradio | |
| import gradio as gr | |
| import cv2 | |
| import numpy as np | |
| from ultralytics import YOLO | |
| # Load YOLOv8 model | |
| model = YOLO("yolov8s.pt") | |
| def detect_objects(image): | |
| """ | |
| Function to perform object detection using YOLOv8. | |
| """ | |
| # Convert image to RGB (Gradio provides images in numpy array format) | |
| image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) | |
| # Run YOLOv8 inference | |
| results = model(image_rgb) | |
| # Convert the annotated image back to OpenCV format | |
| annotated_image = results[0].plot() # Get annotated image | |
| return annotated_image # Return the image with bounding boxes | |
| # Create a Gradio interface | |
| interface = gr.Interface( | |
| fn=detect_objects, # Function to call | |
| inputs=gr.Image(type="numpy"), # Input: Image | |
| outputs=gr.Image(type="numpy"), # Output: Processed Image | |
| title="YOLOv8 Object Detection", | |
| description="Upload an image, and YOLOv8 will detect objects in it." | |
| ) | |
| # Launch the Gradio app | |
| interface.launch() | |