dschandra commited on
Commit
3227154
·
verified ·
1 Parent(s): 3bf3b3c

Create trajectory_predictor.py

Browse files
Files changed (1) hide show
  1. trajectory_predictor.py +63 -0
trajectory_predictor.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+
3
+ def predict_trajectory(detection_data, pitch_height=720, stump_zone=(280, 360)):
4
+ """
5
+ Uses polynomial regression to predict post-impact ball trajectory.
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 = detection_data["ball_positions"]
18
+ impact_frame = detection_data["impact_frame"]
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
+ # Predict future trajectory
45
+ last_x = xs[-1]
46
+ future_xs = list(range(last_x, last_x + 60, 5)) # simulate 60px ahead
47
+ future_ys = [int(poly(x)) for x in future_xs]
48
+
49
+ trajectory_points = list(zip(xs, ys)) + list(zip(future_xs, future_ys))
50
+
51
+ # OUT logic: predicted y crosses stump plane and x within stump zone
52
+ for x, y in zip(future_xs, future_ys):
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
+ return {
61
+ "trajectory_points": trajectory_points,
62
+ "decision": "NOT OUT"
63
+ }