AjaykumarPilla commited on
Commit
8a4d72c
·
verified ·
1 Parent(s): 8cf0b5b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +246 -257
app.py CHANGED
@@ -1,290 +1,279 @@
1
  import cv2
2
  import numpy as np
 
3
  from ultralytics import YOLO
4
- import mediapipe as mp
5
- from scipy.interpolate import interp1d
6
  import gradio as gr
 
7
  import plotly.graph_objects as go
 
8
  import os
9
- import logging
10
 
11
- # Setup logging
12
- logging.basicConfig(level=logging.INFO)
13
- logger = logging.getLogger(__name__)
14
 
15
- # Initialize models
16
- ball_model = YOLO('best.pt') # Load pre-trained YOLOv8 model
17
- mp_pose = mp.solutions.pose
18
- pose = mp_pose.Pose()
 
 
 
 
 
 
 
 
 
19
 
20
  def process_video(video_path):
21
- logger.info("Starting video processing")
22
- # Load video
23
  cap = cv2.VideoCapture(video_path)
24
- if not cap.isOpened():
25
- logger.error("Failed to open video file")
26
- return None, "Error: Could not open video", None
27
-
28
  frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
29
  frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
30
- fps = int(cap.get(cv2.CAP_PROP_FPS))
31
- frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
32
- if frame_count < 10: # Ensure video has enough frames
33
- logger.error("Video too short, requires at least 10 frames")
34
- cap.release()
35
- return None, "Error: Video too short", None
36
-
37
- output_path = "replay.mp4"
38
- output_video = cv2.VideoWriter(output_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, (frame_width, frame_height))
39
-
40
- ball_positions = [] # Store (frame_idx, x, y, confidence)
41
- release_frame, pitch_frame, impact_frame = None, None, None
42
- release_x, release_y = None, None
43
- pitch_x, pitch_y = None, None
44
- impact_x, impact_y = None, None
45
- decision = "Not Out"
46
-
47
- # Initialize Kalman Filter
48
- kalman = cv2.KalmanFilter(4, 2)
49
- kalman.measurementMatrix = np.array([[1, 0, 0, 0], [0, 1, 0, 0]], np.float32)
50
- kalman.transitionMatrix = np.array([[1, 0, 1, 0], [0, 1, 0, 1], [0, 0, 1, 0], [0, 0, 0, 1]], np.float32)
51
- kalman.processNoiseCov = np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]], np.float32) * 0.005
52
- kalman.measurementNoiseCov = np.array([[1, 0], [0, 1]], np.float32) * 0.2
53
- kalman.statePre = np.array([[frame_width/2], [frame_height/2], [0], [0]], np.float32)
54
-
55
  frames = []
56
- frame_idx = 0
57
- last_valid_pos = None
 
58
 
 
59
  while cap.isOpened():
60
  ret, frame = cap.read()
61
  if not ret:
62
  break
 
63
  frames.append(frame.copy())
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
 
65
- # Ball detection
66
- results = ball_model.predict(frame, verbose=False)
67
- ball_detected = False
68
- for result in results:
69
- for box in result.boxes:
70
- if result.names[int(box.cls)] == 'cricket_ball' and box.conf > 0.5: # Lowered threshold
71
- x, y, w, h = box.xywh[0]
72
- if last_valid_pos is None or (
73
- abs(x - last_valid_pos[0]) < 100 and abs(y - last_valid_pos[1]) < 100
74
- ):
75
- measurement = np.array([[np.float32(x)], [np.float32(y)]])
76
- kalman.correct(measurement)
77
- ball_positions.append((frame_idx, x, y, box.conf))
78
- last_valid_pos = (x, y)
79
- ball_detected = True
80
- logger.info(f"Frame {frame_idx}: Detected ball at ({x:.2f}, {y:.2f}), conf={box.conf:.2f}")
81
- break
82
- if ball_detected:
83
- break
84
-
85
- if not ball_detected:
86
- prediction = kalman.predict()
87
- x, y = prediction[0].item(), prediction[1].item()
88
- if 0 <= x < frame_width and 0 <= y < frame_height:
89
- ball_positions.append((frame_idx, x, y, 0.0))
90
- logger.debug(f"Frame {frame_idx}: Predicted ball at ({x:.2f}, {y:.2f})")
91
-
92
- # Pose detection
93
- pose_results = pose.process(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
94
- if pose_results.pose_landmarks:
95
- try:
96
- if frame_idx < 10 and release_frame is None:
97
- hand = pose_results.pose_landmarks.landmark[mp_pose.PoseLandmark.RIGHT_WRIST]
98
- if hand.visibility > 0.7:
99
- release_x, release_y = hand.x * frame_width, hand.y * frame_height
100
- release_frame = frame_idx
101
- logger.info(f"Release point detected at frame {frame_idx}: ({release_x:.2f}, {release_y:.2f})")
102
-
103
- if ball_detected and impact_frame is None:
104
- for landmark in [mp_pose.PoseLandmark.LEFT_KNEE, mp_pose.PoseLandmark.RIGHT_KNEE]:
105
- knee = pose_results.pose_landmarks.landmark[landmark]
106
- if knee.visibility > 0.7:
107
- knee_x, knee_y = knee.x * frame_width, knee.y * frame_height
108
- if abs(knee_x - x) < 50 and abs(knee_y - y) < 50:
109
- impact_x, impact_y = x, y
110
- impact_frame = frame_idx
111
- logger.info(f"Impact point detected at frame {frame_idx}: ({impact_x:.2f}, {impact_y:.2f})")
112
- break
113
- except Exception as e:
114
- logger.error(f"Pose detection error: {str(e)}")
115
-
116
- # Pitch point
117
- if ball_detected and pitch_frame is None:
118
- if y > frame_height * 0.8:
119
- pitch_x, pitch_y = x, y
120
- pitch_frame = frame_idx
121
- logger.info(f"Pitch point detected at frame {frame_idx}: ({pitch_x:.2f}, {pitch_y:.2f})")
122
-
123
- frame_idx += 1
124
 
125
- cap.release()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
126
 
127
- # Smooth trajectory
128
- confident_positions = [(idx, x, y) for idx, x, y, conf in ball_positions if conf > 0.5]
129
- if not confident_positions:
130
- logger.error("No confident ball detections found in the video")
131
- output_video.release()
132
- fig = go.Figure()
133
- fig.add_annotation(text="No confident ball detections for trajectory plot", showarrow=False)
134
- fig.update_layout(template="plotly_dark")
135
- return output_path, "Error: No ball detections", fig
136
-
137
- smooth_trajectory = []
138
- if len(confident_positions) > 2:
139
- frames_range, x_coords, y_coords = zip(*confident_positions)
140
- try:
141
- fx = interp1d(frames_range, x_coords, kind='cubic', fill_value="extrapolate")
142
- fy = interp1d(frames_range, y_coords, kind='cubic', fill_value="extrapolate")
143
- all_frames = np.arange(min(frames_range), max(frames_range) + 1)
144
- smooth_trajectory = [
145
- (int(fx(t)), int(fy(t)))
146
- for t in all_frames
147
- if 0 <= fx(t) < frame_width and 0 <= fy(t) < frame_height and not np.isnan(fx(t)) and not np.isnan(fy(t))
148
- ]
149
- logger.info(f"Generated smooth trajectory with {len(smooth_trajectory)} points")
150
- except Exception as e:
151
- logger.error(f"Interpolation failed: {str(e)}")
152
- smooth_trajectory = [(int(x), int(y)) for idx, x, y, conf in ball_positions if conf > 0.5 and 0 <= x < frame_width and 0 <= y < frame_height]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
153
  else:
154
- smooth_trajectory = [(int(x), int(y)) for idx, x, y, conf in ball_positions if conf > 0.5 and 0 <= x < frame_width and 0 <= y < frame_height]
155
- logger.warning(f"Insufficient confident detections ({len(confident_positions)}), using raw confident positions")
156
-
157
- # LBW Decision
158
- pitch_in_line = pitch_x is not None and frame_width * 0.4 < pitch_x < frame_width * 0.6
159
- impact_in_line = impact_x is not None and frame_width * 0.4 < impact_x < frame_width * 0.6
160
- stumps_hit = False
161
- if impact_frame and smooth_trajectory:
162
- last_x, last_y = smooth_trajectory[-1]
163
- if last_y < frame_height * 0.3 and frame_width * 0.4 < last_x < frame_width * 0.6:
164
- stumps_hit = True
165
- if pitch_in_line and impact_in_line and stumps_hit:
166
- decision = "Out"
167
- logger.info(f"Decision: {decision}")
168
-
169
- # Generate replay video
170
  for i, frame in enumerate(frames):
171
- for x, y in smooth_trajectory:
172
- try:
173
- cv2.circle(frame, (x, y), 3, (0, 0, 255), -1) # Red trajectory
174
- except Exception as e:
175
- logger.error(f"Error drawing trajectory at ({x}, {y}): {str(e)}")
176
- if i == release_frame and release_x and release_y:
177
- cv2.circle(frame, (int(release_x), int(release_y)), 5, (255, 0, 0), -1) # Blue release
178
- if i == pitch_frame and pitch_x and pitch_y:
179
- cv2.circle(frame, (int(pitch_x), int(pitch_y)), 5, (0, 255, 255), -1) # Yellow pitch
180
- if i == impact_frame and impact_x and impact_y:
181
- cv2.circle(frame, (int(impact_x), int(impact_y)), 5, (0, 255, 0), -1) # Green impact
182
- cv2.putText(frame, f"Decision: {decision}", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
183
- output_video.write(frame)
184
-
185
- output_video.release()
186
-
187
- # Generate Plotly trajectory plots
188
- if confident_positions:
189
- frames_range, x_coords, y_coords = zip(*confident_positions)
190
- # Plot 1: X, Y vs Frame Index
191
- fig1 = go.Figure()
192
- fig1.add_trace(go.Scatter(
193
- x=frames_range, y=x_coords, mode='lines+markers', name='X Coordinate',
194
- line=dict(color='blue'), marker=dict(size=8)
195
- ))
196
- fig1.add_trace(go.Scatter(
197
- x=frames_range, y=y_coords, mode='lines+markers', name='Y Coordinate',
198
- line=dict(color='red'), marker=dict(size=8)
199
- ))
200
- if release_frame:
201
- fig1.add_trace(go.Scatter(
202
- x=[release_frame], y=[release_y], mode='markers', name='Release Point',
203
- marker=dict(size=12, color='blue', symbol='star')
204
- ))
205
- if pitch_frame:
206
- fig1.add_trace(go.Scatter(
207
- x=[pitch_frame], y=[pitch_y], mode='markers', name='Pitch Point',
208
- marker=dict(size=12, color='yellow', symbol='star')
209
- ))
210
- if impact_frame:
211
- fig1.add_trace(go.Scatter(
212
- x=[impact_frame], y=[impact_y], mode='markers', name='Impact Point',
213
- marker=dict(size=12, color='green', symbol='star')
214
- ))
215
- fig1.update_layout(
216
- title="Ball Trajectory (X, Y vs Frame Index)",
217
- xaxis_title="Frame Index",
218
- yaxis_title="Pixel Coordinate",
219
- template="plotly_dark",
220
- showlegend=True
221
- )
222
-
223
- # Plot 2: X vs Y (Spatial Trajectory)
224
- fig2 = go.Figure()
225
- fig2.add_trace(go.Scatter(
226
- x=x_coords, y=y_coords, mode='lines+markers', name='Ball Trajectory',
227
- line=dict(color='red'), marker=dict(size=8)
228
- ))
229
- if release_frame:
230
- fig2.add_trace(go.Scatter(
231
- x=[release_x], y=[release_y], mode='markers', name='Release Point',
232
- marker=dict(size=12, color='blue', symbol='star')
233
- ))
234
- if pitch_frame:
235
- fig2.add_trace(go.Scatter(
236
- x=[pitch_x], y=[pitch_y], mode='markers', name='Pitch Point',
237
- marker=dict(size=12, color='yellow', symbol='star')
238
- ))
239
- if impact_frame:
240
- fig2.add_trace(go.Scatter(
241
- x=[impact_x], y=[impact_y], mode='markers', name='Impact Point',
242
- marker=dict(size=12, color='green', symbol='star')
243
- ))
244
- fig2.update_layout(
245
- title="Ball Trajectory (X vs Y, Spatial View)",
246
- xaxis_title="X Coordinate (pixels)",
247
- yaxis_title="Y Coordinate (pixels)",
248
- template="plotly_dark",
249
- showlegend=True,
250
- xaxis=dict(range=[0, frame_width]),
251
- yaxis=dict(range=[frame_height, 0]) # Invert y-axis to match video orientation
252
- )
253
-
254
- # Combine plots
255
- fig = go.Figure()
256
- fig.add_traces(fig1.data + fig2.data)
257
- fig.update_layout(
258
- title="Ball Trajectory Analysis",
259
- grid=dict(rows=2, columns=1),
260
- subplot_titles=["X, Y vs Frame Index", "X vs Y (Spatial View)"],
261
- template="plotly_dark",
262
- showlegend=True
263
- )
264
- fig.layout['xaxis'].update(title="Frame Index", range=[min(frames_range), max(frames_range)])
265
- fig.layout['yaxis'].update(title="Pixel Coordinate")
266
- fig.layout['xaxis2'].update(title="X Coordinate (pixels)", range=[0, frame_width])
267
- fig.layout['yaxis2'].update(title="Y Coordinate (pixels)", range=[frame_height, 0])
268
- else:
269
- fig = go.Figure()
270
- fig.add_annotation(text="No confident ball detections for trajectory plot", showarrow=False)
271
- fig.update_layout(template="plotly_dark")
272
 
273
- logger.info("Video processing completed")
274
- return output_path, decision, fig
 
 
 
275
 
276
- # Gradio Interface
277
  iface = gr.Interface(
278
- fn=process_video,
279
- inputs=gr.Video(label="Upload Cricket Video (5-10s, 1080p, behind bowler/side-on)"),
280
  outputs=[
281
- gr.Video(label="Replay Video"),
282
- gr.Textbox(label="Decision"),
283
- gr.Plot(label="Trajectory Plots (Temporal and Spatial)")
 
284
  ],
285
- title="Cricket DRS System",
286
- description="Upload a cricket video to get a DRS analysis with smooth ball trajectory, Out/Not Out decision, and trajectory plots."
287
  )
288
 
289
  if __name__ == "__main__":
290
- iface.launch()
 
1
  import cv2
2
  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 plotly.graph_objects as go
8
+ import uuid
9
  import os
 
10
 
11
+ # Load the trained YOLOv8n model with optimizations
12
+ model = YOLO("best.pt")
13
+ model.to('cuda' if torch.cuda.is_available() else 'cpu') # Use GPU if available
14
 
15
+ # Constants for LBW decision and video processing
16
+ STUMPS_WIDTH = 0.2286 # meters (width of stumps)
17
+ BALL_DIAMETER = 0.073 # meters (approx. cricket ball diameter)
18
+ FRAME_RATE = 20 # Input video frame rate
19
+ SLOW_MOTION_FACTOR = 3 # For very slow motion (3x slower)
20
+ CONF_THRESHOLD = 0.2 # Confidence threshold
21
+ IMPACT_ZONE_Y = 0.85 # Fraction of frame height where impact is likely
22
+ IMPACT_DELTA_Y = 50 # Pixels for detecting sudden y-position change
23
+ PITCH_LENGTH = 20.12 # meters (standard cricket pitch length)
24
+ STUMPS_HEIGHT = 0.71 # meters (stumps height)
25
+ CAMERA_HEIGHT = 2.0 # meters (assumed camera height)
26
+ CAMERA_DISTANCE = 10.0 # meters (assumed camera distance from pitch)
27
+ MAX_POSITION_JUMP = 30 # Pixels, tightened for continuous trajectory
28
 
29
  def process_video(video_path):
30
+ if not os.path.exists(video_path):
31
+ return [], [], [], "Error: Video file not found"
32
  cap = cv2.VideoCapture(video_path)
33
+ # Get native video resolution
 
 
 
34
  frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
35
  frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  frames = []
37
+ ball_positions = []
38
+ detection_frames = []
39
+ debug_log = []
40
 
41
+ frame_count = 0
42
  while cap.isOpened():
43
  ret, frame = cap.read()
44
  if not ret:
45
  break
46
+ frame_count += 1
47
  frames.append(frame.copy())
48
+ # Use native resolution for inference
49
+ results = model.predict(frame, conf=CONF_THRESHOLD, imgsz=(frame_height, frame_width), iou=0.5, max_det=1)
50
+ detections = 0
51
+ for detection in results[0].boxes:
52
+ if detection.cls == 0: # Class 0 is the ball
53
+ detections += 1
54
+ if detections == 1: # Only consider frames with exactly one detection
55
+ x1, y1, x2, y2 = detection.xyxy[0].cpu().numpy()
56
+ ball_positions.append([(x1 + x2) / 2, (y1 + y2) / 2])
57
+ detection_frames.append(frame_count - 1)
58
+ cv2.rectangle(frame, (int(x1), int(y1)), (int(x2), int(y2)), (0, 255, 0), 2)
59
+ frames[-1] = frame
60
+ debug_log.append(f"Frame {frame_count}: {detections} ball detections")
61
+ cap.release()
62
 
63
+ if not ball_positions:
64
+ debug_log.append("No balls detected in any frame")
65
+ else:
66
+ debug_log.append(f"Total ball detections: {len(ball_positions)}")
67
+ debug_log.append(f"Video resolution: {frame_width}x{frame_height}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
 
69
+ return frames, ball_positions, detection_frames, "\n".join(debug_log)
70
+
71
+ def pixel_to_3d(x, y, frame_height, frame_width):
72
+ """Convert 2D pixel coordinates to 3D real-world coordinates."""
73
+ x_norm = x / frame_width
74
+ y_norm = y / frame_height
75
+ x_3d = (x_norm - 0.5) * 3.0 # Center x at 0 (middle of pitch)
76
+ y_3d = y_norm * PITCH_LENGTH
77
+ z_3d = (1 - y_norm) * BALL_DIAMETER * 5 # Scale to approximate ball bounce height
78
+ return x_3d, y_3d, z_3d
79
+
80
+ def estimate_trajectory(ball_positions, frames, detection_frames):
81
+ if len(ball_positions) < 2:
82
+ return None, None, None, None, None, None, None, None, None, "Error: Fewer than 2 ball detections for trajectory"
83
+ frame_height, frame_width = frames[0].shape[:2]
84
+
85
+ # Filter out sudden changes in position for continuous trajectory
86
+ filtered_positions = [ball_positions[0]]
87
+ filtered_frames = [detection_frames[0]]
88
+ for i in range(1, len(ball_positions)):
89
+ prev_pos = filtered_positions[-1]
90
+ curr_pos = ball_positions[i]
91
+ distance = np.sqrt((curr_pos[0] - prev_pos[0])**2 + (curr_pos[1] - prev_pos[1])**2)
92
+ if distance <= MAX_POSITION_JUMP:
93
+ filtered_positions.append(curr_pos)
94
+ filtered_frames.append(detection_frames[i])
95
+ else:
96
+ # Skip sudden jumps to maintain continuity
97
+ continue
98
+
99
+ if len(filtered_positions) < 2:
100
+ return None, None, None, None, None, None, None, None, None, "Error: Fewer than 2 valid ball detections after filtering"
101
+
102
+ x_coords = [pos[0] for pos in filtered_positions]
103
+ y_coords = [pos[1] for pos in filtered_positions]
104
+ times = np.array(filtered_frames) / FRAME_RATE
105
+
106
+ # Pitch point detection: Assume it happens when the ball reaches a certain low point on the y-axis
107
+ pitch_point = None
108
+ pitch_frame = None
109
+ for i in range(1, len(y_coords)):
110
+ if y_coords[i] > frame_height * 0.75: # The ball reaches near the ground
111
+ pitch_point = filtered_positions[i]
112
+ pitch_frame = filtered_frames[i]
113
+ break
114
+
115
+ # Impact point detection: Look for sudden changes in the y-position (delta_y) or when ball enters impact zone
116
+ impact_idx = None
117
+ impact_frame = None
118
+ for i in range(1, len(y_coords)):
119
+ delta_y = abs(y_coords[i] - y_coords[i-1])
120
+ if delta_y > IMPACT_DELTA_Y:
121
+ impact_idx = i
122
+ impact_frame = filtered_frames[i]
123
+ break
124
+ elif y_coords[i] > frame_height * IMPACT_ZONE_Y:
125
+ impact_idx = i
126
+ impact_frame = filtered_frames[i]
127
+ break
128
+ if impact_idx is None:
129
+ impact_idx = len(filtered_positions) - 1
130
+ impact_frame = filtered_frames[-1]
131
+ impact_point = filtered_positions[impact_idx]
132
+
133
+ try:
134
+ # Use cubic interpolation for smoother trajectory
135
+ fx = interp1d(times[:impact_idx + 1], x_coords[:impact_idx + 1], kind='cubic', fill_value="extrapolate")
136
+ fy = interp1d(times[:impact_idx + 1], y_coords[:impact_idx + 1], kind='cubic', fill_value="extrapolate")
137
+ except Exception as e:
138
+ return None, None, None, None, None, None, None, None, None, f"Error in trajectory interpolation: {str(e)}"
139
 
140
+ # Generate dense points for all frames between first and last detection
141
+ total_frames = max(detection_frames) - min(detection_frames) + 1
142
+ t_full = np.linspace(times[0], times[-1], total_frames * SLOW_MOTION_FACTOR)
143
+ x_full = fx(t_full)
144
+ y_full = fy(t_full)
145
+ trajectory_2d = list(zip(x_full, y_full))
146
+
147
+ trajectory_3d = [pixel_to_3d(x, y, frame_height, frame_width) for x, y in trajectory_2d]
148
+ detections_3d = [pixel_to_3d(x, y, frame_height, frame_width) for x, y in filtered_positions]
149
+
150
+ # Handle missing pitch and impact points gracefully
151
+ pitch_point_3d = pixel_to_3d(pitch_point[0], pitch_point[1], frame_height, frame_width) if pitch_point else None
152
+ impact_point_3d = pixel_to_3d(impact_point[0], impact_point[1], frame_height, frame_width) if impact_point else None
153
+
154
+ # Handle cases where no pitch/impact point is found
155
+ if pitch_point is None:
156
+ pitch_frame = "N/A"
157
+ pitch_point_3d = None # No 3D coordinates for pitch point
158
+ if impact_point is None:
159
+ impact_frame = "N/A"
160
+ impact_point_3d = None # No 3D coordinates for impact point
161
+
162
+ debug_log = (
163
+ f"Trajectory estimated successfully\n"
164
+ f"Pitch point at frame {pitch_frame + 1 if pitch_frame != 'N/A' else 'N/A'}: {pitch_point if pitch_point else 'Not detected'}\n"
165
+ f"Impact point at frame {impact_frame + 1 if impact_frame != 'N/A' else 'N/A'}: {impact_point if impact_point else 'Not detected'}\n"
166
+ f"Detections in frames: {filtered_frames}"
167
+ )
168
+ return trajectory_2d, pitch_point, impact_point, pitch_frame, impact_frame, detections_3d, trajectory_3d, pitch_point_3d, impact_point_3d, debug_log
169
+
170
+ def lbw_decision(ball_positions, trajectory, frames, pitch_point, impact_point):
171
+ if not frames:
172
+ return "Error: No frames processed", None, None, None
173
+ if not trajectory or len(ball_positions) < 2:
174
+ return "Not enough data (insufficient ball detections)", None, None, None
175
+
176
+ # Check for None values before unpacking
177
+ if pitch_point is None or impact_point is None:
178
+ return "Not Out (Unable to determine pitch or impact points)", trajectory, pitch_point, impact_point
179
+
180
+ frame_height, frame_width = frames[0].shape[:2]
181
+ stumps_x = frame_width / 2
182
+ stumps_y = frame_height * 0.9
183
+ stumps_width_pixels = frame_width * (STUMPS_WIDTH / 3.0)
184
+
185
+ pitch_x, pitch_y = pitch_point
186
+ impact_x, impact_y = impact_point
187
+
188
+ if pitch_x < stumps_x - stumps_width_pixels / 2 or pitch_x > stumps_x + stumps_width_pixels / 2:
189
+ return f"Not Out (Pitched outside line at x: {pitch_x:.1f}, y: {pitch_y:.1f})", trajectory, pitch_point, impact_point
190
+ if impact_x < stumps_x - stumps_width_pixels / 2 or impact_x > stumps_x + stumps_width_pixels / 2:
191
+ return f"Not Out (Impact outside line at x: {impact_x:.1f}, y: {impact_y:.1f})", trajectory, pitch_point, impact_point
192
+ for x, y in trajectory:
193
+ if abs(x - stumps_x) < stumps_width_pixels / 2 and abs(y - stumps_y) < frame_height * 0.1:
194
+ return f"Out (Ball hits stumps, Pitch at x: {pitch_x:.1f}, y: {pitch_y:.1f}, Impact at x: {impact_x:.1f}, y: {impact_y:.1f})", trajectory, pitch_point, impact_point
195
+ return f"Not Out (Missing stumps, Pitch at x: {pitch_x:.1f}, y: {pitch_y:.1f}, Impact at x: {impact_x:.1f}, y: {impact_y:.1f})", trajectory, pitch_point, impact_point
196
+
197
+ def generate_slow_motion(frames, trajectory, pitch_point, impact_point, detection_frames, pitch_frame, impact_frame, output_path):
198
+ if not frames:
199
+ return None
200
+ frame_height, frame_width = frames[0].shape[:2]
201
+ fourcc = cv2.VideoWriter_fourcc(*'mp4v')
202
+ out = cv2.VideoWriter(output_path, fourcc, FRAME_RATE / SLOW_MOTION_FACTOR, (frame_width, frame_height))
203
+
204
+ # Map trajectory points to all frames between first and last detection
205
+ if trajectory and detection_frames:
206
+ min_frame = min(detection_frames)
207
+ max_frame = max(detection_frames)
208
+ total_frames = max_frame - min_frame + 1
209
+ trajectory_points = np.array(trajectory, dtype=np.int32).reshape((-1, 1, 2))
210
+ traj_per_frame = len(trajectory) // total_frames
211
+ trajectory_indices = [i * traj_per_frame for i in range(total_frames)]
212
  else:
213
+ trajectory_points = np.array([], dtype=np.int32)
214
+ trajectory_indices = []
215
+
 
 
 
 
 
 
 
 
 
 
 
 
 
216
  for i, frame in enumerate(frames):
217
+ frame_idx = i - min_frame if trajectory_indices else -1
218
+ if frame_idx >= 0 and frame_idx < total_frames and trajectory_points.size > 0:
219
+ # Draw trajectory up to current frame
220
+ end_idx = trajectory_indices[frame_idx] + 1
221
+ cv2.polylines(frame, [trajectory_points[:end_idx]], False, (255, 0, 0), 2)
222
+ if pitch_point and i == pitch_frame:
223
+ x, y = pitch_point
224
+ cv2.circle(frame, (int(x), int(y)), 8, (0, 0, 255), -1)
225
+ cv2.putText(frame, "Pitch Point", (int(x) + 10, int(y) - 10),
226
+ cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 2)
227
+ if impact_point and i == impact_frame:
228
+ x, y = impact_point
229
+ cv2.circle(frame, (int(x), int(y)), 8, (0, 255, 255), -1)
230
+ cv2.putText(frame, "Impact Point", (int(x) + 10, int(y) + 20),
231
+ cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 255), 2)
232
+ for _ in range(SLOW_MOTION_FACTOR):
233
+ out.write(frame)
234
+ out.release()
235
+ return output_path
236
+
237
+ def drs_review(video):
238
+ frames, ball_positions, detection_frames, debug_log = process_video(video)
239
+ if not frames:
240
+ return f"Error: Failed to process video\nDebug Log:\n{debug_log}", None, None, None
241
+
242
+ trajectory_2d, pitch_point, impact_point, pitch_frame, impact_frame, detections_3d, trajectory_3d, pitch_point_3d, impact_point_3d, trajectory_log = estimate_trajectory(ball_positions, frames, detection_frames)
243
+
244
+ if trajectory_2d is None:
245
+ return (f"Error: {trajectory_log}\nDebug Log:\n{debug_log}", None, None, None)
246
+
247
+ decision, trajectory_2d, pitch_point, impact_point = lbw_decision(ball_positions, trajectory_2d, frames, pitch_point, impact_point)
248
+
249
+ output_path = f"output_{uuid.uuid4()}.mp4"
250
+ slow_motion_path = generate_slow_motion(frames, trajectory_2d, pitch_point, impact_point, detection_frames, pitch_frame, impact_frame, output_path)
251
+
252
+ detections_fig = None
253
+ trajectory_fig = None
254
+ if detections_3d:
255
+ detections_fig = create_3d_plot(detections_3d, trajectory_3d, pitch_point_3d, impact_point_3d, "detections")
256
+ trajectory_fig = create_3d_plot(detections_3d, trajectory_3d, pitch_point_3d, impact_point_3d, "trajectory")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
257
 
258
+ debug_output = f"{debug_log}\n{trajectory_log}"
259
+ return (f"DRS Decision: {decision}\nDebug Log:\n{debug_output}",
260
+ slow_motion_path,
261
+ detections_fig,
262
+ trajectory_fig)
263
 
264
+ # Gradio interface
265
  iface = gr.Interface(
266
+ fn=drs_review,
267
+ inputs=gr.Video(label="Upload Video Clip"),
268
  outputs=[
269
+ gr.Textbox(label="DRS Decision and Debug Log"),
270
+ gr.Video(label="Very Slow-Motion Replay with Ball Detection (Green), Trajectory (Blue Line), Pitch Point (Red), Impact Point (Yellow)"),
271
+ gr.Plot(label="3D Single Ball Detections Plot"),
272
+ gr.Plot(label="3D Ball Trajectory Plot (Single Detections)")
273
  ],
274
+ title="AI-Powered DRS for LBW in Local Cricket",
275
+ description="Upload a video clip of a cricket delivery to get an LBW decision, a slow-motion replay, and 3D visualizations. The replay shows ball detection (green boxes), trajectory (blue line), pitch point (red circle), and impact point (yellow circle). The 3D plots show single-detection frames (green markers) and trajectory (blue line) with wicket lines (black), pitch point (red), and impact point (yellow)."
276
  )
277
 
278
  if __name__ == "__main__":
279
+ iface.launch()