Spaces:
Runtime error
Runtime error
| from ultralytics import YOLO | |
| import cv2 | |
| # β Use the pretrained YOLOv8n model (no custom model required) | |
| model = YOLO("yolov8n.pt") | |
| def detect_ball_yolo(frame): | |
| results = model(frame, verbose=False)[0] | |
| ball_coords = [] | |
| for box in results.boxes: | |
| cls = int(box.cls[0]) | |
| conf = float(box.conf[0]) | |
| # π Sports ball class in COCO = 32 | |
| if cls == 32 and conf > 0.4: | |
| x1, y1, x2, y2 = map(int, box.xyxy[0]) | |
| cx, cy = (x1 + x2) // 2, (y1 + y2) // 2 | |
| ball_coords.append((cx, cy)) | |
| # Only return the most confident detection | |
| return ball_coords[:1] if ball_coords else [] | |