File size: 412 Bytes
8293298
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
from ultralytics import YOLO
import cv2

model = YOLO("best.pt")  # Trained YOLOv8 model for cricket ball

def detect_ball(frame):
    results = model(frame)
    for r in results:
        for box in r.boxes:
            cls = int(box.cls)
            if cls == 0:  # assuming 0 = ball
                x1, y1, x2, y2 = map(int, box.xyxy[0])
                return ((x1 + x2) // 2, (y1 + y2) // 2)
    return None