Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,4 @@
|
|
|
|
1 |
import cv2
|
2 |
import numpy as np
|
3 |
import torch
|
@@ -12,12 +13,11 @@ model = YOLO("best.pt")
|
|
12 |
|
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
|
17 |
-
SLOW_MOTION_FACTOR =
|
18 |
CONF_THRESHOLD = 0.25 # Confidence threshold for detection
|
19 |
-
PITCH_ZONE_Y = 0.9 # Fraction of frame height for pitch zone
|
20 |
-
IMPACT_ZONE_Y = 0.8 # Fraction of frame height for impact zone
|
21 |
IMPACT_DELTA_Y = 50 # Pixels for detecting sudden y-position change
|
22 |
STUMPS_HEIGHT = 0.711 # meters (height of stumps)
|
23 |
|
@@ -35,17 +35,18 @@ def process_video(video_path):
|
|
35 |
ret, frame = cap.read()
|
36 |
if not ret:
|
37 |
break
|
38 |
-
frame_count
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
debug_log.append(f"Frame {frame_count}: {len(detections)} ball detections")
|
|
|
49 |
cap.release()
|
50 |
|
51 |
if not ball_positions:
|
@@ -60,16 +61,21 @@ def estimate_trajectory(ball_positions, detection_frames, frames):
|
|
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 |
-
|
64 |
-
|
65 |
-
|
|
|
|
|
|
|
|
|
|
|
66 |
|
67 |
pitch_idx = 0
|
68 |
for i, y in enumerate(y_coords):
|
69 |
if y > frame_height * PITCH_ZONE_Y:
|
70 |
pitch_idx = i
|
71 |
break
|
72 |
-
pitch_point =
|
73 |
pitch_frame = detection_frames[pitch_idx]
|
74 |
|
75 |
impact_idx = None
|
@@ -79,8 +85,8 @@ def estimate_trajectory(ball_positions, detection_frames, frames):
|
|
79 |
impact_idx = i
|
80 |
break
|
81 |
if impact_idx is None:
|
82 |
-
impact_idx = len(
|
83 |
-
impact_point =
|
84 |
impact_frame = detection_frames[impact_idx]
|
85 |
|
86 |
x_coords = x_coords[:impact_idx + 1]
|
@@ -94,8 +100,7 @@ def estimate_trajectory(ball_positions, detection_frames, frames):
|
|
94 |
return None, None, None, None, None, None, f"Error in trajectory interpolation: {str(e)}"
|
95 |
|
96 |
vis_trajectory = list(zip(x_coords, y_coords))
|
97 |
-
|
98 |
-
t_full = np.linspace(times[0], times[-1] + 0.5, len(times) + 10)
|
99 |
x_full = fx(t_full)
|
100 |
y_full = fy(t_full)
|
101 |
full_trajectory = list(zip(x_full, y_full))
|
@@ -115,12 +120,11 @@ def lbw_decision(ball_positions, full_trajectory, frames, pitch_point, impact_po
|
|
115 |
stumps_x = frame_width / 2
|
116 |
stumps_y = frame_height * 0.9
|
117 |
stumps_width_pixels = frame_width * (STUMPS_WIDTH / 3.0)
|
118 |
-
batsman_area_y = frame_height * 0.8
|
119 |
|
120 |
pitch_x, pitch_y = pitch_point
|
121 |
impact_x, impact_y = impact_point
|
122 |
|
123 |
-
# LBW Conditions
|
124 |
in_line_threshold = stumps_width_pixels / 2
|
125 |
if pitch_x < stumps_x - in_line_threshold or pitch_x > stumps_x + in_line_threshold:
|
126 |
return f"Not Out (Pitched outside line at x: {pitch_x:.1f}, y: {pitch_y:.1f})", full_trajectory, pitch_point, impact_point
|
@@ -136,8 +140,7 @@ def lbw_decision(ball_positions, full_trajectory, frames, pitch_point, impact_po
|
|
136 |
break
|
137 |
|
138 |
if hit_stumps:
|
139 |
-
|
140 |
-
if abs(x - stumps_x) < in_line_threshold * 0.1: # Arbitrary small margin for clipping
|
141 |
return f"Umpire's Call - Not Out (Ball clips stumps, Pitch at x: {pitch_x:.1f}, y: {pitch_y:.1f}, Impact at x: {impact_x:.1f}, y: {impact_y:.1f})", full_trajectory, pitch_point, impact_point
|
142 |
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})", full_trajectory, pitch_point, impact_point
|
143 |
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})", full_trajectory, pitch_point, impact_point
|
@@ -157,17 +160,12 @@ def generate_slow_motion(frames, vis_trajectory, pitch_point, pitch_frame, impac
|
|
157 |
trajectory_points = np.array(vis_trajectory, dtype=np.int32).reshape((-1, 1, 2))
|
158 |
|
159 |
for i, frame in enumerate(frames):
|
160 |
-
# Draw stumps (
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
for x, y in stump_positions:
|
167 |
-
cv2.line(frame, (int(x), int(y)), (int(x), int(y - stumps_height_pixels)), (255, 255, 255), 2)
|
168 |
-
|
169 |
-
# Draw crease line (striker to non-striker)
|
170 |
-
cv2.line(frame, (0, int(stumps_y)), (frame_width, int(stumps_y)), (255, 255, 0), 2) # Yellow line
|
171 |
|
172 |
if i in detection_frames and trajectory_points.size > 0:
|
173 |
idx = detection_frames.index(i) + 1
|
@@ -176,20 +174,19 @@ def generate_slow_motion(frames, vis_trajectory, pitch_point, pitch_frame, impac
|
|
176 |
|
177 |
if pitch_point and i == pitch_frame:
|
178 |
x, y = pitch_point
|
179 |
-
cv2.circle(frame, (int(x), int(y)), 8, (0, 255, 0), -1)
|
180 |
cv2.putText(frame, "Pitching Factor", (int(x) + 10, int(y) - 10),
|
181 |
-
cv2.FONT_HERSHEY_SIMPLEX, 0.
|
182 |
|
183 |
if impact_point and i == impact_frame:
|
184 |
x, y = impact_point
|
185 |
-
cv2.circle(frame, (int(x), int(y)), 8, (0, 0, 255), -1)
|
186 |
cv2.putText(frame, "Impact Factor", (int(x) + 10, int(y) + 20),
|
187 |
-
cv2.FONT_HERSHEY_SIMPLEX, 0.
|
188 |
|
189 |
-
# Wicket factor (show at impact frame if hitting stumps)
|
190 |
if impact_point and i == impact_frame and "Out" in lbw_decision(ball_positions, full_trajectory, frames, pitch_point, impact_point)[0]:
|
191 |
cv2.putText(frame, "Wicket Factor", (int(stumps_x) - 50, int(stumps_y) - 20),
|
192 |
-
cv2.FONT_HERSHEY_SIMPLEX, 0.
|
193 |
|
194 |
for _ in range(SLOW_MOTION_FACTOR):
|
195 |
out.write(frame)
|
@@ -215,11 +212,12 @@ iface = gr.Interface(
|
|
215 |
inputs=gr.Video(label="Upload Video Clip"),
|
216 |
outputs=[
|
217 |
gr.Textbox(label="DRS Decision and Debug Log"),
|
218 |
-
gr.Video(label="
|
219 |
],
|
220 |
title="AI-Powered DRS for LBW in Local Cricket",
|
221 |
-
description="Upload a video clip of a cricket delivery to get an LBW decision and slow-motion replay showing pitching factor (green circle), impact factor (red circle), wicket factor (orange text), stumps (white lines), and crease line (yellow line)."
|
222 |
)
|
223 |
|
224 |
if __name__ == "__main__":
|
225 |
-
iface.launch()
|
|
|
|
1 |
+
```python
|
2 |
import cv2
|
3 |
import numpy as np
|
4 |
import torch
|
|
|
13 |
|
14 |
# Constants for LBW decision and video processing
|
15 |
STUMPS_WIDTH = 0.2286 # meters (width of stumps)
|
|
|
16 |
FRAME_RATE = 20 # Input video frame rate
|
17 |
+
SLOW_MOTION_FACTOR = 2 # Reduced for faster output
|
18 |
CONF_THRESHOLD = 0.25 # Confidence threshold for detection
|
19 |
+
PITCH_ZONE_Y = 0.9 # Fraction of frame height for pitch zone
|
20 |
+
IMPACT_ZONE_Y = 0.8 # Fraction of frame height for impact zone
|
21 |
IMPACT_DELTA_Y = 50 # Pixels for detecting sudden y-position change
|
22 |
STUMPS_HEIGHT = 0.711 # meters (height of stumps)
|
23 |
|
|
|
35 |
ret, frame = cap.read()
|
36 |
if not ret:
|
37 |
break
|
38 |
+
if frame_count % 2 == 0: # Process every 2nd frame
|
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]
|
42 |
+
if len(detections) == 1:
|
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(len(frames) - 1)
|
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 |
+
frame_count += 1
|
50 |
cap.release()
|
51 |
|
52 |
if not ball_positions:
|
|
|
61 |
return None, None, None, None, None, None, "Error: Fewer than 2 valid single-ball detections for trajectory"
|
62 |
frame_height = frames[0].shape[0]
|
63 |
|
64 |
+
# Filter to unique positions to reduce interpolation points
|
65 |
+
unique_positions = [ball_positions[0]]
|
66 |
+
for pos in ball_positions[1:]:
|
67 |
+
if abs(pos[0] - unique_positions[-1][0]) > 10 or abs(pos[1] - unique_positions[-1][1]) > 10:
|
68 |
+
unique_positions.append(pos)
|
69 |
+
x_coords = [pos[0] for pos in unique_positions]
|
70 |
+
y_coords = [pos[1] for pos in unique_positions]
|
71 |
+
times = np.array([i / FRAME_RATE for i in range(len(unique_positions))])
|
72 |
|
73 |
pitch_idx = 0
|
74 |
for i, y in enumerate(y_coords):
|
75 |
if y > frame_height * PITCH_ZONE_Y:
|
76 |
pitch_idx = i
|
77 |
break
|
78 |
+
pitch_point = unique_positions[pitch_idx]
|
79 |
pitch_frame = detection_frames[pitch_idx]
|
80 |
|
81 |
impact_idx = None
|
|
|
85 |
impact_idx = i
|
86 |
break
|
87 |
if impact_idx is None:
|
88 |
+
impact_idx = len(y_coords) - 1
|
89 |
+
impact_point = unique_positions[impact_idx]
|
90 |
impact_frame = detection_frames[impact_idx]
|
91 |
|
92 |
x_coords = x_coords[:impact_idx + 1]
|
|
|
100 |
return None, None, None, None, None, None, f"Error in trajectory interpolation: {str(e)}"
|
101 |
|
102 |
vis_trajectory = list(zip(x_coords, y_coords))
|
103 |
+
t_full = np.linspace(times[0], times[-1] + 0.5, len(times) + 5) # Reduced points
|
|
|
104 |
x_full = fx(t_full)
|
105 |
y_full = fy(t_full)
|
106 |
full_trajectory = list(zip(x_full, y_full))
|
|
|
120 |
stumps_x = frame_width / 2
|
121 |
stumps_y = frame_height * 0.9
|
122 |
stumps_width_pixels = frame_width * (STUMPS_WIDTH / 3.0)
|
123 |
+
batsman_area_y = frame_height * 0.8
|
124 |
|
125 |
pitch_x, pitch_y = pitch_point
|
126 |
impact_x, impact_y = impact_point
|
127 |
|
|
|
128 |
in_line_threshold = stumps_width_pixels / 2
|
129 |
if pitch_x < stumps_x - in_line_threshold or pitch_x > stumps_x + in_line_threshold:
|
130 |
return f"Not Out (Pitched outside line at x: {pitch_x:.1f}, y: {pitch_y:.1f})", full_trajectory, pitch_point, impact_point
|
|
|
140 |
break
|
141 |
|
142 |
if hit_stumps:
|
143 |
+
if abs(x - stumps_x) < in_line_threshold * 0.1:
|
|
|
144 |
return f"Umpire's Call - Not Out (Ball clips stumps, Pitch at x: {pitch_x:.1f}, y: {pitch_y:.1f}, Impact at x: {impact_x:.1f}, y: {impact_y:.1f})", full_trajectory, pitch_point, impact_point
|
145 |
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})", full_trajectory, pitch_point, impact_point
|
146 |
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})", full_trajectory, pitch_point, impact_point
|
|
|
160 |
trajectory_points = np.array(vis_trajectory, dtype=np.int32).reshape((-1, 1, 2))
|
161 |
|
162 |
for i, frame in enumerate(frames):
|
163 |
+
# Draw stumps (single line for efficiency)
|
164 |
+
cv2.line(frame, (int(stumps_x - stumps_width_pixels / 2), int(stumps_y)),
|
165 |
+
(int(stumps_x + stumps_width_pixels / 2), int(stumps_y)), (255, 255, 255), 2)
|
166 |
+
|
167 |
+
# Draw crease line
|
168 |
+
cv2.line(frame, (0, int(stumps_y)), (frame_width, int(stumps_y)), (255, 255, 0), 2)
|
|
|
|
|
|
|
|
|
|
|
169 |
|
170 |
if i in detection_frames and trajectory_points.size > 0:
|
171 |
idx = detection_frames.index(i) + 1
|
|
|
174 |
|
175 |
if pitch_point and i == pitch_frame:
|
176 |
x, y = pitch_point
|
177 |
+
cv2.circle(frame, (int(x), int(y)), 8, (0, 255, 0), -1)
|
178 |
cv2.putText(frame, "Pitching Factor", (int(x) + 10, int(y) - 10),
|
179 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1)
|
180 |
|
181 |
if impact_point and i == impact_frame:
|
182 |
x, y = impact_point
|
183 |
+
cv2.circle(frame, (int(x), int(y)), 8, (0, 0, 255), -1)
|
184 |
cv2.putText(frame, "Impact Factor", (int(x) + 10, int(y) + 20),
|
185 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1)
|
186 |
|
|
|
187 |
if impact_point and i == impact_frame and "Out" in lbw_decision(ball_positions, full_trajectory, frames, pitch_point, impact_point)[0]:
|
188 |
cv2.putText(frame, "Wicket Factor", (int(stumps_x) - 50, int(stumps_y) - 20),
|
189 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 165, 255), 1)
|
190 |
|
191 |
for _ in range(SLOW_MOTION_FACTOR):
|
192 |
out.write(frame)
|
|
|
212 |
inputs=gr.Video(label="Upload Video Clip"),
|
213 |
outputs=[
|
214 |
gr.Textbox(label="DRS Decision and Debug Log"),
|
215 |
+
gr.Video(label="Optimized Slow-Motion Replay with Pitching Factor (Green), Impact Factor (Red), Wicket Factor (Orange), Stumps (White), Crease (Yellow)")
|
216 |
],
|
217 |
title="AI-Powered DRS for LBW in Local Cricket",
|
218 |
+
description="Upload a video clip of a cricket delivery to get an LBW decision and optimized slow-motion replay showing pitching factor (green circle), impact factor (red circle), wicket factor (orange text), stumps (white lines), and crease line (yellow line)."
|
219 |
)
|
220 |
|
221 |
if __name__ == "__main__":
|
222 |
+
iface.launch()
|
223 |
+
```
|