Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,7 +3,7 @@ import numpy as np
|
|
3 |
import torch
|
4 |
from ultralytics import YOLO
|
5 |
import gradio as gr
|
6 |
-
from scipy.interpolate import interp1d
|
7 |
import uuid
|
8 |
import os
|
9 |
|
@@ -54,31 +54,29 @@ def process_video(video_path):
|
|
54 |
|
55 |
return frames, ball_positions, detection_frames, "\n".join(debug_log)
|
56 |
|
57 |
-
def
|
58 |
if len(ball_positions) < 2:
|
59 |
-
return None,
|
60 |
-
|
61 |
-
frame_height = frames[0].shape[0]
|
62 |
|
63 |
# Extract x, y coordinates
|
64 |
x_coords = [pos[0] for pos in ball_positions]
|
65 |
y_coords = [pos[1] for pos in ball_positions]
|
66 |
times = np.arange(len(ball_positions)) / FRAME_RATE
|
67 |
|
68 |
-
# Use
|
69 |
try:
|
70 |
-
|
71 |
-
|
72 |
except Exception as e:
|
73 |
-
return None,
|
74 |
|
75 |
# Project trajectory (detected + future for LBW decision)
|
76 |
t_full = np.linspace(times[0], times[-1] + 0.5, len(times) + 10)
|
77 |
-
x_full =
|
78 |
-
y_full =
|
79 |
trajectory = list(zip(x_full, y_full))
|
80 |
|
81 |
-
return trajectory,
|
82 |
|
83 |
def detect_pitch_and_impact(ball_positions, frames, frame_height):
|
84 |
pitch_point = None
|
@@ -185,8 +183,9 @@ def drs_review(video):
|
|
185 |
frames, ball_positions, detection_frames, debug_log = process_video(video)
|
186 |
if not frames:
|
187 |
return f"Error: Failed to process video", None
|
188 |
-
trajectory, pitch_point, impact_point, trajectory_log = estimate_trajectory(ball_positions, frames)
|
189 |
|
|
|
|
|
190 |
# Detect pitch and impact points based on ball positions
|
191 |
pitch_point, impact_point = detect_pitch_and_impact(ball_positions, frames, frames[0].shape[0])
|
192 |
|
|
|
3 |
import torch
|
4 |
from ultralytics import YOLO
|
5 |
import gradio as gr
|
6 |
+
from scipy.interpolate import interp1d, CubicSpline
|
7 |
import uuid
|
8 |
import os
|
9 |
|
|
|
54 |
|
55 |
return frames, ball_positions, detection_frames, "\n".join(debug_log)
|
56 |
|
57 |
+
def smooth_trajectory(ball_positions, frames):
|
58 |
if len(ball_positions) < 2:
|
59 |
+
return None, "Error: Fewer than 2 ball detections for trajectory"
|
|
|
|
|
60 |
|
61 |
# Extract x, y coordinates
|
62 |
x_coords = [pos[0] for pos in ball_positions]
|
63 |
y_coords = [pos[1] for pos in ball_positions]
|
64 |
times = np.arange(len(ball_positions)) / FRAME_RATE
|
65 |
|
66 |
+
# Use cubic spline interpolation to smooth the trajectory
|
67 |
try:
|
68 |
+
spline_x = CubicSpline(times, x_coords, bc_type='natural')
|
69 |
+
spline_y = CubicSpline(times, y_coords, bc_type='natural')
|
70 |
except Exception as e:
|
71 |
+
return None, f"Error in trajectory smoothing: {str(e)}"
|
72 |
|
73 |
# Project trajectory (detected + future for LBW decision)
|
74 |
t_full = np.linspace(times[0], times[-1] + 0.5, len(times) + 10)
|
75 |
+
x_full = spline_x(t_full)
|
76 |
+
y_full = spline_y(t_full)
|
77 |
trajectory = list(zip(x_full, y_full))
|
78 |
|
79 |
+
return trajectory, "Trajectory smoothed successfully"
|
80 |
|
81 |
def detect_pitch_and_impact(ball_positions, frames, frame_height):
|
82 |
pitch_point = None
|
|
|
183 |
frames, ball_positions, detection_frames, debug_log = process_video(video)
|
184 |
if not frames:
|
185 |
return f"Error: Failed to process video", None
|
|
|
186 |
|
187 |
+
trajectory, smoothing_log = smooth_trajectory(ball_positions, frames)
|
188 |
+
|
189 |
# Detect pitch and impact points based on ball positions
|
190 |
pitch_point, impact_point = detect_pitch_and_impact(ball_positions, frames, frames[0].shape[0])
|
191 |
|