Spaces:
Runtime error
Runtime error
Create detection/predict.py
Browse files- detection/predict.py +14 -0
detection/predict.py
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from ultralytics import YOLO
|
| 2 |
+
import cv2
|
| 3 |
+
|
| 4 |
+
model = YOLO("best.pt") # Trained YOLOv8 model for cricket ball
|
| 5 |
+
|
| 6 |
+
def detect_ball(frame):
|
| 7 |
+
results = model(frame)
|
| 8 |
+
for r in results:
|
| 9 |
+
for box in r.boxes:
|
| 10 |
+
cls = int(box.cls)
|
| 11 |
+
if cls == 0: # assuming 0 = ball
|
| 12 |
+
x1, y1, x2, y2 = map(int, box.xyxy[0])
|
| 13 |
+
return ((x1 + x2) // 2, (y1 + y2) // 2)
|
| 14 |
+
return None
|