AjaykumarPilla commited on
Commit
7a2099e
·
verified ·
1 Parent(s): 87ee33d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -18
app.py CHANGED
@@ -13,8 +13,8 @@ model = YOLO("best.pt")
13
  # Constants for LBW decision and video processing
14
  STUMPS_WIDTH = 0.2286 # meters (width of stumps)
15
  BALL_DIAMETER = 0.073 # meters (approx. cricket ball diameter)
16
- FRAME_RATE = 30 # Input video frame rate
17
- SLOW_MOTION_FACTOR = 6 # For very slow motion (6x slower)
18
  CONF_THRESHOLD = 0.25 # Confidence threshold for detection
19
  IMPACT_ZONE_Y = 0.85 # Fraction of frame height where impact is likely (near stumps)
20
 
@@ -103,17 +103,17 @@ def lbw_decision(ball_positions, trajectory, frames, pitch_point, impact_point):
103
 
104
  frame_height, frame_width = frames[0].shape[:2]
105
  stumps_x = frame_width / 2
106
- stumps_y = frame_height * 0.9
107
  stumps_width_pixels = frame_width * (STUMPS_WIDTH / 3.0)
108
 
109
  pitch_x, pitch_y = pitch_point
110
  impact_x, impact_y = impact_point
111
 
112
- # Check pitching point
113
  if pitch_x < stumps_x - stumps_width_pixels / 2 or pitch_x > stumps_x + stumps_width_pixels / 2:
114
  return f"Not Out (Pitched outside line at x: {pitch_x:.1f}, y: {pitch_y:.1f})", trajectory, pitch_point, impact_point
115
 
116
- # Check impact point
117
  if impact_x < stumps_x - stumps_width_pixels / 2 or impact_x > stumps_x + stumps_width_pixels / 2:
118
  return f"Not Out (Impact outside line at x: {impact_x:.1f}, y: {impact_y:.1f})", trajectory, pitch_point, impact_point
119
 
@@ -121,6 +121,7 @@ def lbw_decision(ball_positions, trajectory, frames, pitch_point, impact_point):
121
  for x, y in trajectory:
122
  if abs(x - stumps_x) < stumps_width_pixels / 2 and abs(y - stumps_y) < frame_height * 0.1:
123
  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
 
124
  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
125
 
126
  def generate_slow_motion(frames, trajectory, pitch_point, impact_point, detection_frames, output_path):
@@ -129,7 +130,6 @@ def generate_slow_motion(frames, trajectory, pitch_point, impact_point, detectio
129
  fourcc = cv2.VideoWriter_fourcc(*'mp4v')
130
  out = cv2.VideoWriter(output_path, fourcc, FRAME_RATE / SLOW_MOTION_FACTOR, (frames[0].shape[1], frames[0].shape[0]))
131
 
132
- # Extract trajectory points up to impact for visualization
133
  trajectory_points = np.array(trajectory[:len(detection_frames)], dtype=np.int32).reshape((-1, 1, 2))
134
 
135
  for i, frame in enumerate(frames):
@@ -137,19 +137,21 @@ def generate_slow_motion(frames, trajectory, pitch_point, impact_point, detectio
137
  if i in detection_frames and trajectory_points.size > 0:
138
  cv2.polylines(frame, [trajectory_points[:detection_frames.index(i) + 1]], False, (255, 0, 0), 2)
139
 
140
- # Draw pitch point (red circle with label)
141
- if pitch_point and i >= detection_frames[0]:
142
  x, y = pitch_point
143
- cv2.circle(frame, (int(x), int(y)), 8, (0, 0, 255), -1)
144
- cv2.putText(frame, "Pitch Point", (int(x) + 10, int(y) - 10),
145
- cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 2)
 
146
 
147
- # Draw impact point (yellow circle with label) only in frames after impact
148
  if impact_point and i >= detection_frames[min(len(detection_frames) - 1, detection_frames.index(detection_frames[-1]))]:
149
  x, y = impact_point
150
- cv2.circle(frame, (int(x), int(y)), 8, (0, 255, 255), -1)
151
- cv2.putText(frame, "Impact Point", (int(x) + 10, int(y) + 20),
152
- cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 255), 2)
 
153
 
154
  for _ in range(SLOW_MOTION_FACTOR):
155
  out.write(frame)
@@ -175,11 +177,11 @@ iface = gr.Interface(
175
  inputs=gr.Video(label="Upload Video Clip"),
176
  outputs=[
177
  gr.Textbox(label="DRS Decision and Debug Log"),
178
- gr.Video(label="Very Slow-Motion Replay with Ball Detection (Green), Trajectory (Blue Line), Pitch Point (Red), Impact Point (Yellow)")
179
  ],
180
  title="AI-Powered DRS for LBW in Local Cricket",
181
- description="Upload a video clip of a cricket delivery to get an LBW decision and very slow-motion replay showing ball detection (green boxes), trajectory (blue line), pitch point (red circle), and impact point (yellow circle)."
182
  )
183
 
184
  if __name__ == "__main__":
185
- iface.launch()
 
13
  # Constants for LBW decision and video processing
14
  STUMPS_WIDTH = 0.2286 # meters (width of stumps)
15
  BALL_DIAMETER = 0.073 # meters (approx. cricket ball diameter)
16
+ FRAME_RATE = 20 # Input video frame rate (reduced to 20 FPS)
17
+ SLOW_MOTION_FACTOR = 3 # Adjusted for 20 FPS (slower playback without being too slow)
18
  CONF_THRESHOLD = 0.25 # Confidence threshold for detection
19
  IMPACT_ZONE_Y = 0.85 # Fraction of frame height where impact is likely (near stumps)
20
 
 
103
 
104
  frame_height, frame_width = frames[0].shape[:2]
105
  stumps_x = frame_width / 2
106
+ stumps_y = frame_height * 0.9 # Position of the stumps at the bottom of the frame
107
  stumps_width_pixels = frame_width * (STUMPS_WIDTH / 3.0)
108
 
109
  pitch_x, pitch_y = pitch_point
110
  impact_x, impact_y = impact_point
111
 
112
+ # Check pitching point - the ball should land between stumps
113
  if pitch_x < stumps_x - stumps_width_pixels / 2 or pitch_x > stumps_x + stumps_width_pixels / 2:
114
  return f"Not Out (Pitched outside line at x: {pitch_x:.1f}, y: {pitch_y:.1f})", trajectory, pitch_point, impact_point
115
 
116
+ # Check impact point - the ball should hit within the stumps area
117
  if impact_x < stumps_x - stumps_width_pixels / 2 or impact_x > stumps_x + stumps_width_pixels / 2:
118
  return f"Not Out (Impact outside line at x: {impact_x:.1f}, y: {impact_y:.1f})", trajectory, pitch_point, impact_point
119
 
 
121
  for x, y in trajectory:
122
  if abs(x - stumps_x) < stumps_width_pixels / 2 and abs(y - stumps_y) < frame_height * 0.1:
123
  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
124
+
125
  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
126
 
127
  def generate_slow_motion(frames, trajectory, pitch_point, impact_point, detection_frames, output_path):
 
130
  fourcc = cv2.VideoWriter_fourcc(*'mp4v')
131
  out = cv2.VideoWriter(output_path, fourcc, FRAME_RATE / SLOW_MOTION_FACTOR, (frames[0].shape[1], frames[0].shape[0]))
132
 
 
133
  trajectory_points = np.array(trajectory[:len(detection_frames)], dtype=np.int32).reshape((-1, 1, 2))
134
 
135
  for i, frame in enumerate(frames):
 
137
  if i in detection_frames and trajectory_points.size > 0:
138
  cv2.polylines(frame, [trajectory_points[:detection_frames.index(i) + 1]], False, (255, 0, 0), 2)
139
 
140
+ # Draw pitch point (red circle with label) when the ball touches the ground (y < ground threshold)
141
+ if pitch_point and impact_point and i >= detection_frames[0]:
142
  x, y = pitch_point
143
+ if y > frame.shape[0] * 0.75: # Threshold for ground contact (adjust as necessary)
144
+ cv2.circle(frame, (int(x), int(y)), 8, (0, 0, 255), -1)
145
+ cv2.putText(frame, "Pitch Point", (int(x) + 10, int(y) - 10),
146
+ cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 2)
147
 
148
+ # Draw impact point (yellow circle with label) when ball is near stumps (y near bottom)
149
  if impact_point and i >= detection_frames[min(len(detection_frames) - 1, detection_frames.index(detection_frames[-1]))]:
150
  x, y = impact_point
151
+ if y > frame.shape[0] * 0.85: # Threshold for impact (adjust as necessary)
152
+ cv2.circle(frame, (int(x), int(y)), 8, (0, 255, 255), -1)
153
+ cv2.putText(frame, "Impact Point", (int(x) + 10, int(y) + 20),
154
+ cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 255), 2)
155
 
156
  for _ in range(SLOW_MOTION_FACTOR):
157
  out.write(frame)
 
177
  inputs=gr.Video(label="Upload Video Clip"),
178
  outputs=[
179
  gr.Textbox(label="DRS Decision and Debug Log"),
180
+ gr.Video(label="Slow-Motion Replay with Ball Detection (Green), Trajectory (Blue Line), Pitch Point (Red), Impact Point (Yellow)")
181
  ],
182
  title="AI-Powered DRS for LBW in Local Cricket",
183
+ 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)."
184
  )
185
 
186
  if __name__ == "__main__":
187
+ iface.launch()