AjaykumarPilla commited on
Commit
66c096b
·
verified ·
1 Parent(s): 8c456c5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +132 -314
app.py CHANGED
@@ -1,341 +1,159 @@
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 create_3d_plot(detections_3d, trajectory_3d, pitch_point_3d, impact_point_3d, plot_type="detections"):
171
- """Create 3D Plotly visualization for detections or trajectory using single-detection frames."""
172
- stump_x = [-STUMPS_WIDTH/2, STUMPS_WIDTH/2, 0]
173
- stump_y = [PITCH_LENGTH, PITCH_LENGTH, PITCH_LENGTH]
174
- stump_z = [0, 0, 0]
175
- stump_top_z = [STUMPS_HEIGHT, STUMPS_HEIGHT, STUMPS_HEIGHT]
176
- bail_x = [-STUMPS_WIDTH/2, STUMPS_WIDTH/2]
177
- bail_y = [PITCH_LENGTH, PITCH_LENGTH]
178
- bail_z = [STUMPS_HEIGHT, STUMPS_HEIGHT]
179
-
180
- stump_traces = []
181
- for i in range(3):
182
- stump_traces.append(go.Scatter3d(
183
- x=[stump_x[i], stump_x[i]], y=[stump_y[i], stump_y[i]], z=[stump_z[i], stump_top_z[i]],
184
- mode='lines', line=dict(color='black', width=5), name=f'Stump {i+1}'
185
- ))
186
- bail_traces = [
187
- go.Scatter3d(
188
- x=bail_x, y=bail_y, z=bail_z,
189
- mode='lines', line=dict(color='black', width=5), name='Bail'
190
- )
191
- ]
192
-
193
- pitch_scatter = go.Scatter3d(
194
- x=[pitch_point_3d[0]] if pitch_point_3d else [],
195
- y=[pitch_point_3d[1]] if pitch_point_3d else [],
196
- z=[pitch_point_3d[2]] if pitch_point_3d else [],
197
- mode='markers', marker=dict(size=8, color='red'), name='Pitch Point'
198
- )
199
- impact_scatter = go.Scatter3d(
200
- x=[impact_point_3d[0]] if impact_point_3d else [],
201
- y=[impact_point_3d[1]] if impact_point_3d else [],
202
- z=[impact_point_3d[2]] if impact_point_3d else [],
203
- mode='markers', marker=dict(size=8, color='yellow'), name='Impact Point'
204
- )
205
-
206
- if plot_type == "detections":
207
- x, y, z = zip(*detections_3d) if detections_3d else ([], [], [])
208
- scatter = go.Scatter3d(
209
- x=x, y=y, z=z, mode='markers',
210
- marker=dict(size=5, color='green'), name='Single Ball Detections'
211
- )
212
- data = [scatter, pitch_scatter, impact_scatter] + stump_traces + bail_traces
213
- title = "3D Single Ball Detections"
214
- else:
215
- x, y, z = zip(*trajectory_3d) if trajectory_3d else ([], [], [])
216
- trajectory_line = go.Scatter3d(
217
- x=x, y=y, z=z, mode='lines',
218
- line=dict(color='blue', width=4), name='Ball Trajectory (Single Detections)'
219
- )
220
- data = [trajectory_line, pitch_scatter, impact_scatter] + stump_traces + bail_traces
221
- title = "3D Ball Trajectory (Single Detections)"
222
-
223
- layout = go.Layout(
224
- title=title,
225
- scene=dict(
226
- xaxis_title='X (meters)', yaxis_title='Y (meters)', zaxis_title='Z (meters)',
227
- xaxis=dict(range=[-1.5, 1.5]), yaxis=dict(range=[0, PITCH_LENGTH]),
228
- zaxis=dict(range=[0, STUMPS_HEIGHT * 2]), aspectmode='manual',
229
- aspectratio=dict(x=1, y=4, z=0.5)
230
- ),
231
- showlegend=True
232
- )
233
- fig = go.Figure(data=data, layout=layout)
234
- return fig
235
-
236
- def lbw_decision(ball_positions, trajectory, frames, pitch_point, impact_point):
237
- if not frames:
238
- return "Error: No frames processed", None, None, None
239
- if not trajectory or len(ball_positions) < 2:
240
- return "Not enough data (insufficient ball detections)", None, None, None
241
-
242
- frame_height, frame_width = frames[0].shape[:2]
243
- stumps_x = frame_width / 2
244
- stumps_y = frame_height * 0.9
245
- stumps_width_pixels = frame_width * (STUMPS_WIDTH / 3.0)
246
-
247
- pitch_x, pitch_y = pitch_point
248
- impact_x, impact_y = impact_point
249
-
250
- if pitch_x < stumps_x - stumps_width_pixels / 2 or pitch_x > stumps_x + stumps_width_pixels / 2:
251
- return f"Not Out (Pitched outside line at x: {pitch_x:.1f}, y: {pitch_y:.1f})", trajectory, pitch_point, impact_point
252
- if impact_x < stumps_x - stumps_width_pixels / 2 or impact_x > stumps_x + stumps_width_pixels / 2:
253
- return f"Not Out (Impact outside line at x: {impact_x:.1f}, y: {impact_y:.1f})", trajectory, pitch_point, impact_point
254
- for x, y in trajectory:
255
- if abs(x - stumps_x) < stumps_width_pixels / 2 and abs(y - stumps_y) < frame_height * 0.1:
256
- 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
257
- 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
258
-
259
- def generate_slow_motion(frames, trajectory, pitch_point, impact_point, detection_frames, pitch_frame, impact_frame, output_path):
260
- if not frames:
261
- return None
262
- frame_height, frame_width = frames[0].shape[:2]
263
- fourcc = cv2.VideoWriter_fourcc(*'mp4v')
264
- out = cv2.VideoWriter(output_path, fourcc, FRAME_RATE / SLOW_MOTION_FACTOR, (frame_width, frame_height))
265
 
266
- # Map trajectory points to all frames between first and last detection
267
- if trajectory and detection_frames:
268
- min_frame = min(detection_frames)
269
- max_frame = max(detection_frames)
270
- total_frames = max_frame - min_frame + 1
271
- trajectory_points = np.array(trajectory, dtype=np.int32).reshape((-1, 1, 2))
272
- traj_per_frame = len(trajectory) // total_frames
273
- trajectory_indices = [i * traj_per_frame for i in range(total_frames)]
 
274
  else:
275
- trajectory_points = np.array([], dtype=np.int32)
276
- trajectory_indices = []
277
-
 
 
 
 
 
 
 
 
 
 
 
278
  for i, frame in enumerate(frames):
279
- frame_idx = i - min_frame if trajectory_indices else -1
280
- if frame_idx >= 0 and frame_idx < total_frames and trajectory_points.size > 0:
281
- # Draw trajectory up to current frame
282
- end_idx = trajectory_indices[frame_idx] + 1
283
- cv2.polylines(frame, [trajectory_points[:end_idx]], False, (255, 0, 0), 2)
284
- if pitch_point and i == pitch_frame:
285
- x, y = pitch_point
286
- cv2.circle(frame, (int(x), int(y)), 8, (0, 0, 255), -1)
287
- cv2.putText(frame, "Pitch Point", (int(x) + 10, int(y) - 10),
288
- cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 2)
289
- if impact_point and i == impact_frame:
290
- x, y = impact_point
291
- cv2.circle(frame, (int(x), int(y)), 8, (0, 255, 255), -1)
292
- cv2.putText(frame, "Impact Point", (int(x) + 10, int(y) + 20),
293
- cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 255), 2)
294
- for _ in range(SLOW_MOTION_FACTOR):
295
- out.write(frame)
296
- out.release()
297
- return output_path
298
-
299
- def drs_review(video):
300
- frames, ball_positions, detection_frames, debug_log = process_video(video)
301
- if not frames:
302
- return f"Error: Failed to process video\nDebug Log:\n{debug_log}", None, None, None
303
-
304
- 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)
305
-
306
- if trajectory_2d is None:
307
- return (f"Error: {trajectory_log}\nDebug Log:\n{debug_log}", None, None, None)
308
-
309
- decision, trajectory_2d, pitch_point, impact_point = lbw_decision(ball_positions, trajectory_2d, frames, pitch_point, impact_point)
310
-
311
- output_path = f"output_{uuid.uuid4()}.mp4"
312
- slow_motion_path = generate_slow_motion(frames, trajectory_2d, pitch_point, impact_point, detection_frames, pitch_frame, impact_frame, output_path)
313
-
314
- detections_fig = None
315
- trajectory_fig = None
316
- if detections_3d:
317
- detections_fig = create_3d_plot(detections_3d, trajectory_3d, pitch_point_3d, impact_point_3d, "detections")
318
- trajectory_fig = create_3d_plot(detections_3d, trajectory_3d, pitch_point_3d, impact_point_3d, "trajectory")
319
-
320
- debug_output = f"{debug_log}\n{trajectory_log}"
321
- return (f"DRS Decision: {decision}\nDebug Log:\n{debug_output}",
322
- slow_motion_path,
323
- detections_fig,
324
- trajectory_fig)
325
-
326
- # Gradio interface
327
  iface = gr.Interface(
328
- fn=drs_review,
329
- inputs=gr.Video(label="Upload Video Clip"),
330
- outputs=[
331
- gr.Textbox(label="DRS Decision and Debug Log"),
332
- gr.Video(label="Very Slow-Motion Replay with Ball Detection (Green), Trajectory (Blue Line), Pitch Point (Red), Impact Point (Yellow)"),
333
- gr.Plot(label="3D Single Ball Detections Plot"),
334
- gr.Plot(label="3D Ball Trajectory Plot (Single Detections)")
335
- ],
336
- title="AI-Powered DRS for LBW in Local Cricket",
337
- 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)."
338
  )
339
 
340
  if __name__ == "__main__":
341
- iface.launch()
 
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 os
8
 
9
+ # Initialize models
10
+ ball_model = YOLO('best.pt') # Load pre-trained YOLOv8 model
11
+ mp_pose = mp.solutions.pose
12
+ pose = mp_pose.Pose()
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
  def process_video(video_path):
15
+ # Load video
 
16
  cap = cv2.VideoCapture(video_path)
 
17
  frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
18
  frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
19
+ fps = int(cap.get(cv2.CAP_PROP_FPS))
20
+ output_path = "replay.mp4"
21
+ output_video = cv2.VideoWriter(output_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, (frame_width, frame_height))
22
+
23
+ ball_positions = [] # Store (frame_idx, x, y, confidence)
24
+ release_frame, pitch_frame, impact_frame = None, None, None
25
+ release_x, release_y = None, None
26
+ pitch_x, pitch_y = None, None
27
+ impact_x, impact_y = None, None
28
+ decision = "Not Out"
29
+
30
+ # Initialize Kalman Filter (tuned for smooth cricket ball motion)
31
+ kalman = cv2.KalmanFilter(4, 2)
32
+ kalman.measurementMatrix = np.array([[1, 0, 0, 0], [0, 1, 0, 0]], np.float32)
33
+ kalman.transitionMatrix = np.array([[1, 0, 1, 0], [0, 1, 0, 1], [0, 0, 1, 0], [0, 0, 0, 1]], np.float32)
34
+ kalman.processNoiseCov = np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]], np.float32) * 0.01 # Reduced noise for smoother tracking
35
+ kalman.measurementNoiseCov = np.array([[1, 0], [0, 1]], np.float32) * 0.1 # Trust measurements less to avoid jumps
36
+ kalman.statePre = np.array([[frame_width/2], [frame_height/2], [0], [0]], np.float32) # Initialize at frame center
37
+
38
  frames = []
39
+ frame_idx = 0
40
+ last_valid_pos = None
 
41
 
 
42
  while cap.isOpened():
43
  ret, frame = cap.read()
44
  if not ret:
45
  break
 
46
  frames.append(frame.copy())
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
 
48
+ # Ball detection with confidence threshold
49
+ results = ball_model.predict(frame, verbose=False)
50
+ ball_detected = False
51
+ for result in results:
52
+ for box in result.boxes:
53
+ if result.names[int(box.cls)] == 'cricket_ball' and box.conf > 0.7: # Confidence threshold
54
+ x, y, w, h = box.xywh[0]
55
+ # Avoid drastic jumps: check if position is physically plausible
56
+ if last_valid_pos is None or (
57
+ abs(x - last_valid_pos[0]) < 100 and abs(y - last_valid_pos[1]) < 100 # Limit max jump (pixels)
58
+ ):
59
+ measurement = np.array([[np.float32(x)], [np.float32(y)]])
60
+ kalman.correct(measurement)
61
+ ball_positions.append((frame_idx, x, y, box.conf))
62
+ last_valid_pos = (x, y)
63
+ ball_detected = True
64
+ break
65
+
66
+ if not ball_detected:
67
+ # Predict position for missing frames
68
+ prediction = kalman.predict()
69
+ x, y = prediction[0], prediction[1]
70
+ if 0 <= x < frame_width and 0 <= y < frame_height: # Ensure prediction is within frame
71
+ ball_positions.append((frame_idx, x, y, 0.0)) # Zero confidence for predictions
72
+
73
+ # Pose detection for release and impact
74
+ pose_results = pose.process(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
75
+ if pose_results.pose_landmarks:
76
+ # Release point (early frames, bowler's hand)
77
+ if frame_idx < 10 and release_frame is None:
78
+ hand = pose_results.pose_landmarks.landmark[mp_pose.PoseLandmark.RIGHT_HAND]
79
+ if hand.visibility > 0.7:
80
+ release_x, release_y = hand.x * frame_width, hand.y * frame_height
81
+ release_frame = frame_idx
82
+
83
+ # Impact point (batsman’s pad/bat)
84
+ if ball_detected and impact_frame is None:
85
+ for landmark in [mp_pose.PoseLandmark.LEFT_KNEE, mp_pose.PoseLandmark.RIGHT_KNEE]:
86
+ knee = pose_results.pose_landmarks.landmark[landmark]
87
+ if knee.visibility > 0.7:
88
+ knee_x, knee_y = knee.x * frame_width, knee.y * frame_height
89
+ if abs(knee_x - x) < 50 and abs(knee_y - y) < 50: # Simplified collision check
90
+ impact_x, impact_y = x, y
91
+ impact_frame = frame_idx
92
+ break
93
+
94
+ # Pitch point (lowest y-coordinate among confident detections)
95
+ if ball_detected and pitch_frame is None:
96
+ if y > frame_height * 0.8: # Assume pitch is in lower 20% of frame
97
+ pitch_x, pitch_y = x, y
98
+ pitch_frame = frame_idx
99
+
100
+ frame_idx += 1
101
 
102
+ cap.release()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103
 
104
+ # Filter confident detections for trajectory
105
+ confident_positions = [(idx, x, y) for idx, x, y, conf in ball_positions if conf > 0.7]
106
+ if len(confident_positions) > 2: # Need at least 3 points for cubic interpolation
107
+ frames_range, x_coords, y_coords = zip(*confident_positions)
108
+ # Smooth trajectory with cubic spline
109
+ fx = interp1d(frames_range, x_coords, kind='cubic', fill_value="extrapolate")
110
+ fy = interp1d(frames_range, y_coords, kind='cubic', fill_value="extrapolate")
111
+ all_frames = np.arange(min(frames_range), max(frames_range) + 1)
112
+ smooth_trajectory = [(int(fx(t)), int(fy(t))) for t in all_frames if 0 <= fx(t) < frame_width and 0 <= fy(t) < frame_height]
113
  else:
114
+ smooth_trajectory = [(x, y) for idx, x, y, conf in ball_positions if 0 <= x < frame_width and 0 <= y < frame_height]
115
+
116
+ # LBW Decision (simplified)
117
+ pitch_in_line = pitch_x is not None and frame_width * 0.4 < pitch_x < frame_width * 0.6
118
+ impact_in_line = impact_x is not None and frame_width * 0.4 < impact_x < frame_width * 0.6
119
+ stumps_hit = False
120
+ if impact_frame and smooth_trajectory:
121
+ last_x, last_y = smooth_trajectory[-1]
122
+ if last_y < frame_height * 0.3 and frame_width * 0.4 < last_x < frame_width * 0.6:
123
+ stumps_hit = True
124
+ if pitch_in_line and impact_in_line and stumps_hit:
125
+ decision = "Out"
126
+
127
+ # Generate replay video
128
  for i, frame in enumerate(frames):
129
+ # Draw trajectory (red)
130
+ for x, y in smooth_trajectory:
131
+ cv2.circle(frame, (x, y), 3, (0, 0, 255), -1)
132
+ # Draw release point (blue)
133
+ if i == release_frame and release_x and release_y:
134
+ cv2.circle(frame, (int(release_x), int(release_y)), 5, (255, 0, 0), -1)
135
+ # Draw pitch point (yellow)
136
+ if i == pitch_frame and pitch_x and pitch_y:
137
+ cv2.circle(frame, (int(pitch_x), int(pitch_y)), 5, (0, 255, 255), -1)
138
+ # Draw impact point (green)
139
+ if i == impact_frame and impact_x and impact_y:
140
+ cv2.circle(frame, (int(impact_x), int(impact_y)), 5, (0, 255, 0), -1)
141
+ # Add decision text
142
+ cv2.putText(frame, f"Decision: {decision}", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
143
+ output_video.write(frame)
144
+
145
+ output_video.release()
146
+
147
+ return output_path, decision
148
+
149
+ # Gradio Interface
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
150
  iface = gr.Interface(
151
+ fn=process_video,
152
+ inputs=gr.Video(label="Upload Cricket Video (5-10s, 1080p, behind bowler/side-on)"),
153
+ outputs=[gr.Video(label="Replay Video"), gr.Textbox(label="Decision")],
154
+ title="Cricket DRS System",
155
+ description="Upload a cricket video to get a DRS analysis with smooth ball trajectory and Out/Not Out decision."
 
 
 
 
 
156
  )
157
 
158
  if __name__ == "__main__":
159
+ iface.launch()