AjaykumarPilla commited on
Commit
db7efde
·
verified ·
1 Parent(s): 1213ff3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -18
app.py CHANGED
@@ -27,7 +27,7 @@ def process_video(video_path):
27
  cap = cv2.VideoCapture(video_path)
28
  frames = []
29
  ball_positions = []
30
- detection_frames = [] # Track frames with detections
31
  debug_log = []
32
 
33
  frame_count = 0
@@ -38,28 +38,26 @@ def process_video(video_path):
38
  frame_count += 1
39
  frames.append(frame.copy())
40
  results = model.predict(frame, conf=CONF_THRESHOLD)
41
- detections = 0
42
- for detection in results[0].boxes:
43
- if detection.cls == 0: # Class 0 is the ball
44
- detections += 1
45
- x1, y1, x2, y2 = detection.xyxy[0].cpu().numpy()
46
- ball_positions.append([(x1 + x2) / 2, (y1 + y2) / 2])
47
- detection_frames.append(frame_count - 1) # 0-based index
48
- cv2.rectangle(frame, (int(x1), int(y1)), (int(x2), int(y2)), (0, 255, 0), 2)
49
  frames[-1] = frame
50
- debug_log.append(f"Frame {frame_count}: {detections} ball detections")
51
  cap.release()
52
 
53
  if not ball_positions:
54
- debug_log.append("No balls detected in any frame")
55
  else:
56
- debug_log.append(f"Total ball detections: {len(ball_positions)}")
57
 
58
  return frames, ball_positions, detection_frames, "\n".join(debug_log)
59
 
60
  def estimate_trajectory(ball_positions, detection_frames, frames):
61
  if len(ball_positions) < 2:
62
- return None, None, None, None, None, "Error: Fewer than 2 ball detections for trajectory"
63
  frame_height = frames[0].shape[0]
64
 
65
  # Extract x, y coordinates
@@ -67,7 +65,7 @@ def estimate_trajectory(ball_positions, detection_frames, frames):
67
  y_coords = [pos[1] for pos in ball_positions]
68
  times = np.array(detection_frames) / FRAME_RATE
69
 
70
- # Pitch point: first detection or when y exceeds PITCH_ZONE_Y
71
  pitch_idx = 0
72
  for i, y in enumerate(y_coords):
73
  if y > frame_height * PITCH_ZONE_Y:
@@ -97,7 +95,7 @@ def estimate_trajectory(ball_positions, detection_frames, frames):
97
  fx = interp1d(times, x_coords, kind='linear', fill_value="extrapolate")
98
  fy = interp1d(times, y_coords, kind='quadratic', fill_value="extrapolate")
99
  except Exception as e:
100
- return None, None, None, None, None, f"Error in trajectory interpolation: {str(e)}"
101
 
102
  # Trajectory for visualization (detected frames only)
103
  vis_trajectory = list(zip(x_coords, y_coords))
@@ -117,7 +115,7 @@ def lbw_decision(ball_positions, full_trajectory, frames, pitch_point, impact_po
117
  if not frames:
118
  return "Error: No frames processed", None, None, None
119
  if not full_trajectory or len(ball_positions) < 2:
120
- return "Not enough data (insufficient ball detections)", None, None, None
121
 
122
  frame_height, frame_width = frames[0].shape[:2]
123
  stumps_x = frame_width / 2
@@ -210,10 +208,10 @@ iface = gr.Interface(
210
  inputs=gr.Video(label="Upload Video Clip"),
211
  outputs=[
212
  gr.Textbox(label="DRS Decision and Debug Log"),
213
- gr.Video(label="Very Slow-Motion Replay with Ball Detection (Green), Trajectory (Blue Line), Pitch Point (Red), Impact Point (Yellow)")
214
  ],
215
  title="AI-Powered DRS for LBW in Local Cricket",
216
- description="Upload a video clip of a cricket delivery to get an LBW decision and slow-motion replay showing ball detection (green boxes), trajectory (blue line), pitch point (red circle), and impact point (yellow circle)."
217
  )
218
 
219
  if __name__ == "__main__":
 
27
  cap = cv2.VideoCapture(video_path)
28
  frames = []
29
  ball_positions = []
30
+ detection_frames = [] # Track frames with exactly one detection
31
  debug_log = []
32
 
33
  frame_count = 0
 
38
  frame_count += 1
39
  frames.append(frame.copy())
40
  results = model.predict(frame, conf=CONF_THRESHOLD)
41
+ detections = [det for det in results[0].boxes if det.cls == 0] # Class 0 is cricketBall
42
+ if len(detections) == 1: # Only consider frames with exactly one detection
43
+ x1, y1, x2, y2 = detections[0].xyxy[0].cpu().numpy()
44
+ ball_positions.append([(x1 + x2) / 2, (y1 + y2) / 2])
45
+ detection_frames.append(frame_count - 1) # 0-based index
46
+ cv2.rectangle(frame, (int(x1), int(y1)), (int(x2), int(y2)), (0, 255, 0), 2)
 
 
47
  frames[-1] = frame
48
+ debug_log.append(f"Frame {frame_count}: {len(detections)} ball detections")
49
  cap.release()
50
 
51
  if not ball_positions:
52
+ debug_log.append("No valid single-ball detections in any frame")
53
  else:
54
+ debug_log.append(f"Total valid single-ball detections: {len(ball_positions)}")
55
 
56
  return frames, ball_positions, detection_frames, "\n".join(debug_log)
57
 
58
  def estimate_trajectory(ball_positions, detection_frames, frames):
59
  if len(ball_positions) < 2:
60
+ return None, None, None, None, None, None, "Error: Fewer than 2 valid single-ball detections for trajectory"
61
  frame_height = frames[0].shape[0]
62
 
63
  # Extract x, y coordinates
 
65
  y_coords = [pos[1] for pos in ball_positions]
66
  times = np.array(detection_frames) / FRAME_RATE
67
 
68
+ # Pitch point: first valid detection or when y exceeds PITCH_ZONE_Y
69
  pitch_idx = 0
70
  for i, y in enumerate(y_coords):
71
  if y > frame_height * PITCH_ZONE_Y:
 
95
  fx = interp1d(times, x_coords, kind='linear', fill_value="extrapolate")
96
  fy = interp1d(times, y_coords, kind='quadratic', fill_value="extrapolate")
97
  except Exception as e:
98
+ return None, None, None, None, None, None, f"Error in trajectory interpolation: {str(e)}"
99
 
100
  # Trajectory for visualization (detected frames only)
101
  vis_trajectory = list(zip(x_coords, y_coords))
 
115
  if not frames:
116
  return "Error: No frames processed", None, None, None
117
  if not full_trajectory or len(ball_positions) < 2:
118
+ return "Not enough data (insufficient valid single-ball detections)", None, None, None
119
 
120
  frame_height, frame_width = frames[0].shape[:2]
121
  stumps_x = frame_width / 2
 
208
  inputs=gr.Video(label="Upload Video Clip"),
209
  outputs=[
210
  gr.Textbox(label="DRS Decision and Debug Log"),
211
+ gr.Video(label="Very Slow-Motion Replay with Ball Detection (Green), Trajectory (Blue Line), Pitch Point (Red), Impact Point (Yellow), Stumps (White)")
212
  ],
213
  title="AI-Powered DRS for LBW in Local Cricket",
214
+ description="Upload a video clip of a cricket delivery to get an LBW decision and slow-motion replay showing ball detection (green boxes), trajectory (blue line), pitch point (red circle), impact point (yellow circle), and stumps (white lines)."
215
  )
216
 
217
  if __name__ == "__main__":