Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -19,6 +19,7 @@ CONF_THRESHOLD = 0.25 # Confidence threshold for detection
|
|
19 |
IMPACT_ZONE_Y = 0.85 # Fraction of frame height for impact zone
|
20 |
PITCH_ZONE_Y = 0.75 # Fraction of frame height for pitch zone
|
21 |
IMPACT_DELTA_Y = 50 # Pixels for detecting sudden y-position change
|
|
|
22 |
|
23 |
def process_video(video_path):
|
24 |
if not os.path.exists(video_path):
|
@@ -66,19 +67,12 @@ def estimate_trajectory(ball_positions, detection_frames, frames):
|
|
66 |
|
67 |
# Pitch point: first valid detection or when y exceeds PITCH_ZONE_Y
|
68 |
pitch_idx = 0
|
69 |
-
pitch_point = None
|
70 |
-
pitch_frame = None
|
71 |
for i, y in enumerate(y_coords):
|
72 |
if y > frame_height * PITCH_ZONE_Y:
|
73 |
pitch_idx = i
|
74 |
-
pitch_point = ball_positions[pitch_idx]
|
75 |
-
pitch_frame = detection_frames[pitch_idx]
|
76 |
break
|
77 |
-
|
78 |
-
|
79 |
-
pitch_point = ball_positions[0]
|
80 |
-
pitch_frame = detection_frames[0]
|
81 |
-
debug_log = "Warning: No pitch point detected (possible full toss)"
|
82 |
|
83 |
# Impact point: sudden y-change or y exceeds IMPACT_ZONE_Y
|
84 |
impact_idx = None
|
@@ -92,14 +86,7 @@ def estimate_trajectory(ball_positions, detection_frames, frames):
|
|
92 |
impact_point = ball_positions[impact_idx]
|
93 |
impact_frame = detection_frames[impact_idx]
|
94 |
|
95 |
-
#
|
96 |
-
if pitch_frame >= impact_frame and pitch_idx != 0:
|
97 |
-
debug_log = f"Error: Pitch frame {pitch_frame + 1} after impact frame {impact_frame + 1}, adjusting pitch to first detection"
|
98 |
-
pitch_idx = 0
|
99 |
-
pitch_point = ball_positions[0]
|
100 |
-
pitch_frame = detection_frames[0]
|
101 |
-
|
102 |
-
# Use positions up to impact for trajectory
|
103 |
x_coords = x_coords[:impact_idx + 1]
|
104 |
y_coords = y_coords[:impact_idx + 1]
|
105 |
times = times[:impact_idx + 1]
|
@@ -156,6 +143,10 @@ def generate_slow_motion(frames, vis_trajectory, pitch_point, pitch_frame, impac
|
|
156 |
if not frames:
|
157 |
return None
|
158 |
frame_height, frame_width = frames[0].shape[:2]
|
|
|
|
|
|
|
|
|
159 |
|
160 |
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
161 |
out = cv2.VideoWriter(output_path, fourcc, FRAME_RATE / SLOW_MOTION_FACTOR, (frame_width, frame_height))
|
@@ -164,6 +155,15 @@ def generate_slow_motion(frames, vis_trajectory, pitch_point, pitch_frame, impac
|
|
164 |
trajectory_points = np.array(vis_trajectory, dtype=np.int32).reshape((-1, 1, 2))
|
165 |
|
166 |
for i, frame in enumerate(frames):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
167 |
# Draw trajectory (blue line) only for detected frames
|
168 |
if i in detection_frames and trajectory_points.size > 0:
|
169 |
idx = detection_frames.index(i) + 1
|
@@ -208,10 +208,10 @@ iface = gr.Interface(
|
|
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)")
|
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),
|
215 |
)
|
216 |
|
217 |
if __name__ == "__main__":
|
|
|
19 |
IMPACT_ZONE_Y = 0.85 # Fraction of frame height for impact zone
|
20 |
PITCH_ZONE_Y = 0.75 # Fraction of frame height for pitch zone
|
21 |
IMPACT_DELTA_Y = 50 # Pixels for detecting sudden y-position change
|
22 |
+
STUMPS_HEIGHT = 0.711 # meters (height of stumps)
|
23 |
|
24 |
def process_video(video_path):
|
25 |
if not os.path.exists(video_path):
|
|
|
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:
|
72 |
pitch_idx = i
|
|
|
|
|
73 |
break
|
74 |
+
pitch_point = ball_positions[pitch_idx]
|
75 |
+
pitch_frame = detection_frames[pitch_idx]
|
|
|
|
|
|
|
76 |
|
77 |
# Impact point: sudden y-change or y exceeds IMPACT_ZONE_Y
|
78 |
impact_idx = None
|
|
|
86 |
impact_point = ball_positions[impact_idx]
|
87 |
impact_frame = detection_frames[impact_idx]
|
88 |
|
89 |
+
# Use only detected positions for trajectory
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
x_coords = x_coords[:impact_idx + 1]
|
91 |
y_coords = y_coords[:impact_idx + 1]
|
92 |
times = times[:impact_idx + 1]
|
|
|
143 |
if not frames:
|
144 |
return None
|
145 |
frame_height, frame_width = frames[0].shape[:2]
|
146 |
+
stumps_x = frame_width / 2
|
147 |
+
stumps_y = frame_height * 0.9
|
148 |
+
stumps_width_pixels = frame_width * (STUMPS_WIDTH / 3.0)
|
149 |
+
stumps_height_pixels = frame_height * (STUMPS_HEIGHT / 3.0)
|
150 |
|
151 |
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
152 |
out = cv2.VideoWriter(output_path, fourcc, FRAME_RATE / SLOW_MOTION_FACTOR, (frame_width, frame_height))
|
|
|
155 |
trajectory_points = np.array(vis_trajectory, dtype=np.int32).reshape((-1, 1, 2))
|
156 |
|
157 |
for i, frame in enumerate(frames):
|
158 |
+
# Draw stumps (three white vertical lines)
|
159 |
+
stump_positions = [
|
160 |
+
(stumps_x - stumps_width_pixels / 2, stumps_y), # Left stump
|
161 |
+
(stumps_x, stumps_y), # Middle stump
|
162 |
+
(stumps_x + stumps_width_pixels / 2, stumps_y) # Right stump
|
163 |
+
]
|
164 |
+
for x, y in stump_positions:
|
165 |
+
cv2.line(frame, (int(x), int(y)), (int(x), int(y - stumps_height_pixels)), (255, 255, 255), 2)
|
166 |
+
|
167 |
# Draw trajectory (blue line) only for detected frames
|
168 |
if i in detection_frames and trajectory_points.size > 0:
|
169 |
idx = detection_frames.index(i) + 1
|
|
|
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__":
|