dschandra commited on
Commit
ac2cf6e
·
verified ·
1 Parent(s): 977a33b

Update trajectory_predictor.py

Browse files
Files changed (1) hide show
  1. trajectory_predictor.py +17 -55
trajectory_predictor.py CHANGED
@@ -1,63 +1,25 @@
 
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
- }
 
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