Spaces:
Sleeping
Sleeping
Create tracking/ball_tracker.py
Browse files- tracking/ball_tracker.py +25 -0
tracking/ball_tracker.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Simple ByteTrack‑style tracker for the cricket ball."""
|
| 2 |
+
|
| 3 |
+
import math
|
| 4 |
+
from typing import List, Dict
|
| 5 |
+
|
| 6 |
+
# Placeholder minimal tracker (replace with ByteTrack/SORT lib)
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
class BasicBallTracker:
|
| 10 |
+
def __init__(self):
|
| 11 |
+
self.track = [] # list of (frame_idx, x_center, y_center)
|
| 12 |
+
|
| 13 |
+
def update(self, detections: List[Dict], frame_idx: int):
|
| 14 |
+
"""Select the highest‑confidence 'ball' bbox each frame."""
|
| 15 |
+
balls = [d for d in detections if d["class"] == "ball"]
|
| 16 |
+
if not balls:
|
| 17 |
+
return None
|
| 18 |
+
best = max(balls, key=lambda d: d["conf"])
|
| 19 |
+
x1, y1, x2, y2 = best["bbox"]
|
| 20 |
+
xc, yc = (x1 + x2) / 2, (y1 + y2) / 2
|
| 21 |
+
self.track.append((frame_idx, xc, yc))
|
| 22 |
+
return xc, yc
|
| 23 |
+
|
| 24 |
+
def get_track(self):
|
| 25 |
+
return self.track
|