Spaces:
Sleeping
Sleeping
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}") | |