Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -28,6 +28,12 @@ def process_video(video_path):
|
|
28 |
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
29 |
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
30 |
fps = int(cap.get(cv2.CAP_PROP_FPS))
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
output_path = "replay.mp4"
|
32 |
output_video = cv2.VideoWriter(output_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, (frame_width, frame_height))
|
33 |
|
@@ -61,7 +67,7 @@ def process_video(video_path):
|
|
61 |
ball_detected = False
|
62 |
for result in results:
|
63 |
for box in result.boxes:
|
64 |
-
if result.names[int(box.cls)] == 'cricket_ball' and box.conf > 0.
|
65 |
x, y, w, h = box.xywh[0]
|
66 |
if last_valid_pos is None or (
|
67 |
abs(x - last_valid_pos[0]) < 100 and abs(y - last_valid_pos[1]) < 100
|
@@ -73,6 +79,8 @@ def process_video(video_path):
|
|
73 |
ball_detected = True
|
74 |
logger.info(f"Frame {frame_idx}: Detected ball at ({x:.2f}, {y:.2f}), conf={box.conf:.2f}")
|
75 |
break
|
|
|
|
|
76 |
|
77 |
if not ball_detected:
|
78 |
prediction = kalman.predict()
|
@@ -84,23 +92,26 @@ def process_video(video_path):
|
|
84 |
# Pose detection
|
85 |
pose_results = pose.process(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
|
86 |
if pose_results.pose_landmarks:
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
|
|
93 |
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
|
|
|
|
104 |
|
105 |
# Pitch point
|
106 |
if ball_detected and pitch_frame is None:
|
@@ -114,7 +125,15 @@ def process_video(video_path):
|
|
114 |
cap.release()
|
115 |
|
116 |
# Smooth trajectory
|
117 |
-
confident_positions = [(idx, x, y) for idx, x, y, conf in ball_positions if conf > 0.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
118 |
smooth_trajectory = []
|
119 |
if len(confident_positions) > 2:
|
120 |
frames_range, x_coords, y_coords = zip(*confident_positions)
|
@@ -130,9 +149,9 @@ def process_video(video_path):
|
|
130 |
logger.info(f"Generated smooth trajectory with {len(smooth_trajectory)} points")
|
131 |
except Exception as e:
|
132 |
logger.error(f"Interpolation failed: {str(e)}")
|
133 |
-
smooth_trajectory = [(int(x), int(y)) for idx, x, y, conf in ball_positions if conf > 0.
|
134 |
else:
|
135 |
-
smooth_trajectory = [(int(x), int(y)) for idx, x, y, conf in ball_positions if conf > 0.
|
136 |
logger.warning(f"Insufficient confident detections ({len(confident_positions)}), using raw confident positions")
|
137 |
|
138 |
# LBW Decision
|
@@ -248,7 +267,7 @@ def process_video(video_path):
|
|
248 |
fig.layout['yaxis2'].update(title="Y Coordinate (pixels)", range=[frame_height, 0])
|
249 |
else:
|
250 |
fig = go.Figure()
|
251 |
-
fig.add_annotation(text="No confident detections for trajectory plot", showarrow=False)
|
252 |
fig.update_layout(template="plotly_dark")
|
253 |
|
254 |
logger.info("Video processing completed")
|
|
|
28 |
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
29 |
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
30 |
fps = int(cap.get(cv2.CAP_PROP_FPS))
|
31 |
+
frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
32 |
+
if frame_count < 10: # Ensure video has enough frames
|
33 |
+
logger.error("Video too short, requires at least 10 frames")
|
34 |
+
cap.release()
|
35 |
+
return None, "Error: Video too short", None
|
36 |
+
|
37 |
output_path = "replay.mp4"
|
38 |
output_video = cv2.VideoWriter(output_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, (frame_width, frame_height))
|
39 |
|
|
|
67 |
ball_detected = False
|
68 |
for result in results:
|
69 |
for box in result.boxes:
|
70 |
+
if result.names[int(box.cls)] == 'cricket_ball' and box.conf > 0.5: # Lowered threshold
|
71 |
x, y, w, h = box.xywh[0]
|
72 |
if last_valid_pos is None or (
|
73 |
abs(x - last_valid_pos[0]) < 100 and abs(y - last_valid_pos[1]) < 100
|
|
|
79 |
ball_detected = True
|
80 |
logger.info(f"Frame {frame_idx}: Detected ball at ({x:.2f}, {y:.2f}), conf={box.conf:.2f}")
|
81 |
break
|
82 |
+
if ball_detected:
|
83 |
+
break
|
84 |
|
85 |
if not ball_detected:
|
86 |
prediction = kalman.predict()
|
|
|
92 |
# Pose detection
|
93 |
pose_results = pose.process(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
|
94 |
if pose_results.pose_landmarks:
|
95 |
+
try:
|
96 |
+
if frame_idx < 10 and release_frame is None:
|
97 |
+
hand = pose_results.pose_landmarks.landmark[mp_pose.PoseLandmark.RIGHT_WRIST]
|
98 |
+
if hand.visibility > 0.7:
|
99 |
+
release_x, release_y = hand.x * frame_width, hand.y * frame_height
|
100 |
+
release_frame = frame_idx
|
101 |
+
logger.info(f"Release point detected at frame {frame_idx}: ({release_x:.2f}, {release_y:.2f})")
|
102 |
|
103 |
+
if ball_detected and impact_frame is None:
|
104 |
+
for landmark in [mp_pose.PoseLandmark.LEFT_KNEE, mp_pose.PoseLandmark.RIGHT_KNEE]:
|
105 |
+
knee = pose_results.pose_landmarks.landmark[landmark]
|
106 |
+
if knee.visibility > 0.7:
|
107 |
+
knee_x, knee_y = knee.x * frame_width, knee.y * frame_height
|
108 |
+
if abs(knee_x - x) < 50 and abs(knee_y - y) < 50:
|
109 |
+
impact_x, impact_y = x, y
|
110 |
+
impact_frame = frame_idx
|
111 |
+
logger.info(f"Impact point detected at frame {frame_idx}: ({impact_x:.2f}, {impact_y:.2f})")
|
112 |
+
break
|
113 |
+
except Exception as e:
|
114 |
+
logger.error(f"Pose detection error: {str(e)}")
|
115 |
|
116 |
# Pitch point
|
117 |
if ball_detected and pitch_frame is None:
|
|
|
125 |
cap.release()
|
126 |
|
127 |
# Smooth trajectory
|
128 |
+
confident_positions = [(idx, x, y) for idx, x, y, conf in ball_positions if conf > 0.5]
|
129 |
+
if not confident_positions:
|
130 |
+
logger.error("No confident ball detections found in the video")
|
131 |
+
output_video.release()
|
132 |
+
fig = go.Figure()
|
133 |
+
fig.add_annotation(text="No confident ball detections for trajectory plot", showarrow=False)
|
134 |
+
fig.update_layout(template="plotly_dark")
|
135 |
+
return output_path, "Error: No ball detections", fig
|
136 |
+
|
137 |
smooth_trajectory = []
|
138 |
if len(confident_positions) > 2:
|
139 |
frames_range, x_coords, y_coords = zip(*confident_positions)
|
|
|
149 |
logger.info(f"Generated smooth trajectory with {len(smooth_trajectory)} points")
|
150 |
except Exception as e:
|
151 |
logger.error(f"Interpolation failed: {str(e)}")
|
152 |
+
smooth_trajectory = [(int(x), int(y)) for idx, x, y, conf in ball_positions if conf > 0.5 and 0 <= x < frame_width and 0 <= y < frame_height]
|
153 |
else:
|
154 |
+
smooth_trajectory = [(int(x), int(y)) for idx, x, y, conf in ball_positions if conf > 0.5 and 0 <= x < frame_width and 0 <= y < frame_height]
|
155 |
logger.warning(f"Insufficient confident detections ({len(confident_positions)}), using raw confident positions")
|
156 |
|
157 |
# LBW Decision
|
|
|
267 |
fig.layout['yaxis2'].update(title="Y Coordinate (pixels)", range=[frame_height, 0])
|
268 |
else:
|
269 |
fig = go.Figure()
|
270 |
+
fig.add_annotation(text="No confident ball detections for trajectory plot", showarrow=False)
|
271 |
fig.update_layout(template="plotly_dark")
|
272 |
|
273 |
logger.info("Video processing completed")
|