File size: 1,424 Bytes
1807951
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import math

def euclidean_distance(p1, p2):
    """
    Returns the Euclidean distance between two (x, y) points.
    """
    return math.sqrt((p1[0] - p2[0]) ** 2 + (p1[1] - p2[1]) ** 2)

def estimate_ball_speed(ball_positions, fps=25, scale=0.01):
    """
    Estimates average ball speed in meters/second.

    Args:
        ball_positions: List of (frame_idx, x, y)
        fps: Frame rate of video
        scale: Pixel-to-meter conversion (assumed or calibrated)

    Returns:
        float: estimated speed (m/s)
    """
    if len(ball_positions) < 2:
        return 0.0

    distances = []
    for i in range(1, len(ball_positions)):
        _, x1, y1 = ball_positions[i - 1]
        _, x2, y2 = ball_positions[i]
        dist_px = euclidean_distance((x1, y1), (x2, y2))
        distances.append(dist_px)

    avg_px_per_frame = sum(distances) / len(distances)
    speed_m_per_s = avg_px_per_frame * scale * fps
    return round(speed_m_per_s, 2)

def frame_to_time(frame_idx, fps):
    """
    Converts frame index to timestamp (seconds).
    """
    return round(frame_idx / fps, 2)

def format_impact_info(impact_frame, impact_type, fps):
    """
    Creates a descriptive impact summary.
    """
    time_sec = frame_to_time(impact_frame, fps)
    return f"Impact at {time_sec}s on {impact_type.upper()}"

def log(message):
    """
    Simple print logger with prefix.
    """
    print(f"[LBW-DRS] {message}")