AjaykumarPilla commited on
Commit
f2ea3f3
·
verified ·
1 Parent(s): baaf301

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -13
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 estimate_trajectory(ball_positions, frames):
58
  if len(ball_positions) < 2:
59
- return None, None, None, "Error: Fewer than 2 ball detections for trajectory"
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 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")
72
  except Exception as e:
73
- return None, None, None, f"Error in trajectory interpolation: {str(e)}"
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 = fx(t_full)
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
@@ -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