viswanani's picture
Create detection/predict.py
8293298 verified
raw
history blame contribute delete
412 Bytes
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