File size: 904 Bytes
ac2cf6e
3227154
ac2cf6e
3227154
ac2cf6e
3227154
ac2cf6e
 
3227154
ac2cf6e
 
3227154
ac2cf6e
 
 
3227154
ac2cf6e
 
 
3227154
ac2cf6e
 
3227154
ac2cf6e
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# trajectory_predictor.py
import numpy as np
from sklearn.linear_model import LinearRegression

def predict_trajectory(ball_positions, future_frames=10):
    """
    Predicts future trajectory based on current ball positions using polynomial regression.
    Returns extrapolated list of (x, y) points.
    """
    if len(ball_positions) < 5:
        return []  # not enough data

    frames = np.array([p[0] for p in ball_positions])
    xs = np.array([p[1] for p in ball_positions])
    ys = np.array([p[2] for p in ball_positions])

    # Fit 2nd-degree polynomial (quadratic) to x and y separately
    x_poly = np.poly1d(np.polyfit(frames, xs, 2))
    y_poly = np.poly1d(np.polyfit(frames, ys, 2))

    last_frame = frames[-1]
    future_frame_ids = np.arange(last_frame, last_frame + future_frames)

    trajectory = [(int(x_poly(f)), int(y_poly(f))) for f in future_frame_ids]
    return trajectory