Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -65,29 +65,7 @@ def estimate_trajectory(ball_positions, frames):
|
|
65 |
y_coords = [pos[1] for pos in ball_positions]
|
66 |
times = np.arange(len(ball_positions)) / FRAME_RATE
|
67 |
|
68 |
-
# Detect the pitch point: find when the ball touches the ground
|
69 |
-
pitch_point = None
|
70 |
-
for i, y in enumerate(y_coords):
|
71 |
-
if y > frame_height * 0.75: # Threshold for ground contact (near the bottom of the frame)
|
72 |
-
pitch_point = ball_positions[i]
|
73 |
-
break
|
74 |
-
|
75 |
-
# Find impact point (closest to batsman, near stumps)
|
76 |
-
impact_idx = None
|
77 |
-
for i, y in enumerate(y_coords):
|
78 |
-
if y > frame_height * IMPACT_ZONE_Y: # Ball is near stumps/batsman
|
79 |
-
impact_idx = i
|
80 |
-
break
|
81 |
-
if impact_idx is None:
|
82 |
-
impact_idx = len(ball_positions) - 1 # Fallback to last detection
|
83 |
-
|
84 |
-
impact_point = ball_positions[impact_idx]
|
85 |
-
|
86 |
# Use positions up to impact for interpolation
|
87 |
-
x_coords = x_coords[:impact_idx + 1]
|
88 |
-
y_coords = y_coords[:impact_idx + 1]
|
89 |
-
times = times[:impact_idx + 1]
|
90 |
-
|
91 |
try:
|
92 |
fx = interp1d(times, x_coords, kind='linear', fill_value="extrapolate")
|
93 |
fy = interp1d(times, y_coords, kind='quadratic', fill_value="extrapolate")
|
@@ -100,7 +78,29 @@ def estimate_trajectory(ball_positions, frames):
|
|
100 |
y_full = fy(t_full)
|
101 |
trajectory = list(zip(x_full, y_full))
|
102 |
|
103 |
-
return trajectory,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
104 |
|
105 |
def lbw_decision(ball_positions, trajectory, frames, pitch_point, impact_point):
|
106 |
if not frames:
|
@@ -186,6 +186,10 @@ def drs_review(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 |
decision, trajectory, pitch_point, impact_point = lbw_decision(ball_positions, trajectory, frames, pitch_point, impact_point)
|
190 |
|
191 |
output_path = f"output_{uuid.uuid4()}.mp4"
|
|
|
65 |
y_coords = [pos[1] for pos in ball_positions]
|
66 |
times = np.arange(len(ball_positions)) / FRAME_RATE
|
67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
# Use positions up to impact for interpolation
|
|
|
|
|
|
|
|
|
69 |
try:
|
70 |
fx = interp1d(times, x_coords, kind='linear', fill_value="extrapolate")
|
71 |
fy = interp1d(times, y_coords, kind='quadratic', fill_value="extrapolate")
|
|
|
78 |
y_full = fy(t_full)
|
79 |
trajectory = list(zip(x_full, y_full))
|
80 |
|
81 |
+
return trajectory, None, None, "Trajectory estimated successfully"
|
82 |
+
|
83 |
+
def detect_pitch_and_impact(ball_positions, frames, frame_height):
|
84 |
+
pitch_point = None
|
85 |
+
impact_point = None
|
86 |
+
|
87 |
+
# Pitch detection: Ball hits the ground (vertical velocity and low height)
|
88 |
+
pitch_threshold_y = frame_height * 0.75 # Ball reaches near the ground
|
89 |
+
|
90 |
+
# For the impact point, we assume that the region of interest is near the stumps
|
91 |
+
impact_zone_y_min = frame_height * 0.80
|
92 |
+
impact_zone_y_max = frame_height * 0.85
|
93 |
+
|
94 |
+
# Detect pitch and impact points
|
95 |
+
for i, (x, y) in enumerate(ball_positions):
|
96 |
+
if y > pitch_threshold_y and not pitch_point:
|
97 |
+
pitch_point = (x, y) # Ball has hit the ground
|
98 |
+
|
99 |
+
# Check if the ball is near the batsman's impact zone (e.g., near stumps)
|
100 |
+
if impact_zone_y_min <= y <= impact_zone_y_max and not impact_point:
|
101 |
+
impact_point = (x, y) # Ball has impacted the batsman (bat or pad)
|
102 |
+
|
103 |
+
return pitch_point, impact_point
|
104 |
|
105 |
def lbw_decision(ball_positions, trajectory, frames, pitch_point, impact_point):
|
106 |
if not frames:
|
|
|
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 |
+
|
193 |
decision, trajectory, pitch_point, impact_point = lbw_decision(ball_positions, trajectory, frames, pitch_point, impact_point)
|
194 |
|
195 |
output_path = f"output_{uuid.uuid4()}.mp4"
|