| import gradio as gr | |
| import cv2 | |
| import requests | |
| import os | |
| from ultralytics import YOLO | |
| path = ['./data/0068.jpg'] | |
| model_path = './weights/best.pt' | |
| model = YOLO(model_path) | |
| def detect_cheerios(image): | |
| # Run inference on the input image | |
| results = model(image) | |
| # Get the first result (assuming single image input) | |
| result = results[0] | |
| # Draw bounding boxes on the image | |
| for box in result.boxes: | |
| x1, y1, x2, y2 = box.xyxy[0] | |
| conf = box.conf[0] | |
| cv2.rectangle(image, (int(x1), int(y1)), (int(x2), int(y2)), (0, 255, 0), 2) | |
| cv2.putText(image, f'Apple: {conf:.2f}', (int(x1), int(y1) - 10), | |
| cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2) | |
| return image | |
| iface = gr.Interface( | |
| fn=detect_cheerios, | |
| inputs=gr.Image(type="numpy"), | |
| outputs=gr.Image(), | |
| title="Cheerios detector", | |
| examples=path, | |
| ) | |
| # Launch the interface | |
| iface.launch() |