Spaces:
Sleeping
Sleeping
Update trajectory_predictor.py
Browse files- trajectory_predictor.py +17 -55
trajectory_predictor.py
CHANGED
@@ -1,63 +1,25 @@
|
|
|
|
1 |
import numpy as np
|
|
|
2 |
|
3 |
-
def predict_trajectory(
|
4 |
"""
|
5 |
-
|
6 |
-
|
7 |
-
Args:
|
8 |
-
detection_data: output from `detect_lbw_event`
|
9 |
-
pitch_height: total frame height (in pixels) to simulate stumps
|
10 |
-
stump_zone: x-coordinate range for stumps (min_x, max_x)
|
11 |
-
|
12 |
-
Returns:
|
13 |
-
dict with:
|
14 |
-
- trajectory_points: [(x, y), ...] actual + predicted
|
15 |
-
- decision: "OUT" or "NOT OUT"
|
16 |
"""
|
17 |
-
ball_positions
|
18 |
-
|
19 |
-
|
20 |
-
if not ball_positions or impact_frame == -1:
|
21 |
-
return {
|
22 |
-
"trajectory_points": [],
|
23 |
-
"decision": "NOT ENOUGH DATA"
|
24 |
-
}
|
25 |
-
|
26 |
-
# Extract coordinates pre-impact
|
27 |
-
xs = []
|
28 |
-
ys = []
|
29 |
-
for idx, x, y in ball_positions:
|
30 |
-
if idx <= impact_frame:
|
31 |
-
xs.append(x)
|
32 |
-
ys.append(y)
|
33 |
-
|
34 |
-
if len(xs) < 5:
|
35 |
-
return {
|
36 |
-
"trajectory_points": [],
|
37 |
-
"decision": "NOT ENOUGH POINTS"
|
38 |
-
}
|
39 |
-
|
40 |
-
# Fit polynomial regression (degree 2 for parabolic path)
|
41 |
-
coeffs = np.polyfit(xs, ys, deg=2)
|
42 |
-
poly = np.poly1d(coeffs)
|
43 |
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
future_ys = [int(poly(x)) for x in future_xs]
|
48 |
|
49 |
-
|
|
|
|
|
50 |
|
51 |
-
|
52 |
-
|
53 |
-
if y >= pitch_height - 150: # near stump base
|
54 |
-
if stump_zone[0] <= x <= stump_zone[1]:
|
55 |
-
return {
|
56 |
-
"trajectory_points": trajectory_points,
|
57 |
-
"decision": "OUT"
|
58 |
-
}
|
59 |
|
60 |
-
|
61 |
-
|
62 |
-
"decision": "NOT OUT"
|
63 |
-
}
|
|
|
1 |
+
# trajectory_predictor.py
|
2 |
import numpy as np
|
3 |
+
from sklearn.linear_model import LinearRegression
|
4 |
|
5 |
+
def predict_trajectory(ball_positions, future_frames=10):
|
6 |
"""
|
7 |
+
Predicts future trajectory based on current ball positions using polynomial regression.
|
8 |
+
Returns extrapolated list of (x, y) points.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
"""
|
10 |
+
if len(ball_positions) < 5:
|
11 |
+
return [] # not enough data
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
+
frames = np.array([p[0] for p in ball_positions])
|
14 |
+
xs = np.array([p[1] for p in ball_positions])
|
15 |
+
ys = np.array([p[2] for p in ball_positions])
|
|
|
16 |
|
17 |
+
# Fit 2nd-degree polynomial (quadratic) to x and y separately
|
18 |
+
x_poly = np.poly1d(np.polyfit(frames, xs, 2))
|
19 |
+
y_poly = np.poly1d(np.polyfit(frames, ys, 2))
|
20 |
|
21 |
+
last_frame = frames[-1]
|
22 |
+
future_frame_ids = np.arange(last_frame, last_frame + future_frames)
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
+
trajectory = [(int(x_poly(f)), int(y_poly(f))) for f in future_frame_ids]
|
25 |
+
return trajectory
|
|
|
|