GullyDRS.LBW.Predictorapp / yolo_ball_detector.py
viswanani's picture
Update yolo_ball_detector.py
bda24db verified
raw
history blame contribute delete
650 Bytes
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 []