-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
436 lines (355 loc) · 16.4 KB
/
main.py
File metadata and controls
436 lines (355 loc) · 16.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
# from utils import (
# read_video,
# save_video,
# measure_distance,
# draw_player_stats,
# convert_pixel_distance_to_meters
# )
# import constants
# from trackers import PlayerTracker, BallTracker
# from court_line_detector import CourtLineDetector
# from mini_court import MiniCourt
# import pandas as pd
# from copy import deepcopy
# import cv2
# import os
# def run_analysis(input_video_path, output_video_path):
# input_video_path = os.path.abspath(input_video_path)
# output_video_path = os.path.abspath(output_video_path)
# # Add FPS detection here
# cap = cv2.VideoCapture(input_video_path)
# fps = cap.get(cv2.CAP_PROP_FPS)
# if fps <= 0:
# fps = 24
# cap.release()
# video_frames = read_video(input_video_path)
# if not video_frames:
# raise ValueError(f"No frames found in video: {input_video_path}")
# # Your existing main() logic here, just indent it all one level,
# # replace hardcoded paths with parameters.
# # Detect Players and Ball
# player_tracker = PlayerTracker(model_path='yolov8x')
# ball_tracker = BallTracker(model_path='models/yolo5_last.pt')
# player_detections = player_tracker.detect_frames(
# video_frames,
# read_from_stub=True,
# stub_path="tracker_stubs/player_detections.pkl"
# )
# ball_detections = ball_tracker.detect_frames(
# video_frames,
# read_from_stub=True,
# stub_path="tracker_stubs/ball_detections.pkl"
# )
# ball_detections = ball_tracker.interpolate_ball_positions(ball_detections)
# # Court Line Detection
# court_model_path = "models/keypoints_model.pth"
# court_line_detector = CourtLineDetector(court_model_path)
# court_keypoints = court_line_detector.predict(video_frames[0])
# # Choose players
# player_detections = player_tracker.choose_and_filter_players(
# court_keypoints, player_detections)
# # Mini Court setup
# mini_court = MiniCourt(video_frames[0])
# ball_shot_frames = ball_tracker.get_ball_shot_frames(ball_detections)
# # Convert to mini court coordinates
# player_mini_detections, ball_mini_detections = mini_court.convert_bounding_boxes_to_mini_court_coordinates(
# player_detections, ball_detections, court_keypoints)
# player_stats_data = [{
# 'frame_num': 0,
# 'player_1_number_of_shots': 0,
# 'player_1_total_shot_speed': 0,
# 'player_1_last_shot_speed': 0,
# 'player_1_total_player_speed': 0,
# 'player_1_last_player_speed': 0,
# 'player_2_number_of_shots': 0,
# 'player_2_total_shot_speed': 0,
# 'player_2_last_shot_speed': 0,
# 'player_2_total_player_speed': 0,
# 'player_2_last_player_speed': 0,
# }]
# for ball_shot_ind in range(len(ball_shot_frames) - 1):
# start_frame = ball_shot_frames[ball_shot_ind]
# end_frame = ball_shot_frames[ball_shot_ind + 1]
# # Use actual fps here
# ball_shot_time = (end_frame - start_frame) / fps
# start_frame = ball_shot_frames[ball_shot_ind]
# end_frame = ball_shot_frames[ball_shot_ind + 1]
# ball_shot_time = (end_frame - start_frame) / 24 # fps = 24
# ball_distance_px = measure_distance(
# ball_mini_detections[start_frame][1],
# ball_mini_detections[end_frame][1]
# )
# ball_distance_m = convert_pixel_distance_to_meters(
# ball_distance_px,
# constants.DOUBLE_LINE_WIDTH,
# mini_court.get_width_of_mini_court()
# )
# ball_speed = (ball_distance_m / ball_shot_time) * 3.6 # km/h
# player_positions = player_mini_detections[start_frame]
# player_shot_ball = min(player_positions.keys(), key=lambda pid: measure_distance(
# player_positions[pid], ball_mini_detections[start_frame][1]))
# opponent_id = 1 if player_shot_ball == 2 else 2
# opp_distance_px = measure_distance(
# player_mini_detections[start_frame][opponent_id],
# player_mini_detections[end_frame][opponent_id]
# )
# opp_distance_m = convert_pixel_distance_to_meters(
# opp_distance_px,
# constants.DOUBLE_LINE_WIDTH,
# mini_court.get_width_of_mini_court()
# )
# opp_speed = (opp_distance_m / ball_shot_time) * 3.6
# stats = deepcopy(player_stats_data[-1])
# stats['frame_num'] = start_frame
# stats[f'player_{player_shot_ball}_number_of_shots'] += 1
# stats[f'player_{player_shot_ball}_total_shot_speed'] += ball_speed
# stats[f'player_{player_shot_ball}_last_shot_speed'] = ball_speed
# stats[f'player_{opponent_id}_total_player_speed'] += opp_speed
# stats[f'player_{opponent_id}_last_player_speed'] = opp_speed
# player_stats_data.append(stats)
# df_stats = pd.DataFrame(player_stats_data)
# frames_df = pd.DataFrame({'frame_num': list(range(len(video_frames)))})
# df_stats = pd.merge(frames_df, df_stats, on='frame_num', how='left').ffill()
# df_stats['player_1_average_shot_speed'] = df_stats['player_1_total_shot_speed'] / \
# df_stats['player_1_number_of_shots']
# df_stats['player_2_average_shot_speed'] = df_stats['player_2_total_shot_speed'] / \
# df_stats['player_2_number_of_shots']
# df_stats['player_1_average_player_speed'] = df_stats['player_1_total_player_speed'] / \
# df_stats['player_2_number_of_shots']
# df_stats['player_2_average_player_speed'] = df_stats['player_2_total_player_speed'] / \
# df_stats['player_1_number_of_shots']
# # Visual Output
# output_frames = player_tracker.draw_bboxes(video_frames, player_detections)
# output_frames = ball_tracker.draw_bboxes(output_frames, ball_detections)
# output_frames = court_line_detector.draw_keypoints_on_video(
# output_frames, court_keypoints)
# output_frames = mini_court.draw_mini_court(output_frames)
# output_frames = mini_court.draw_points_on_mini_court(
# output_frames, player_mini_detections)
# output_frames = mini_court.draw_points_on_mini_court(
# output_frames, ball_mini_detections, color=(0, 255, 255))
# output_frames = draw_player_stats(output_frames, df_stats)
# print(f"Total frames to save: {len(output_frames)}")
# # --- Insert your debug prints here ---
# print(f"Total frames: {len(output_frames)}")
# if len(output_frames) > 0:
# print(f"Frame size: {output_frames[0].shape}, dtype: {output_frames[0].dtype}")
# else:
# print("No frames found to save!")
# # Ensure output directory exists
# os.makedirs(os.path.dirname(output_video_path), exist_ok=True)
# # Pass fps when saving video
# save_video(output_frames, output_video_path, fps=fps)
# return output_video_path
# if __name__ == "__main__":
# default_input = "input_videos/input_video.mp4"
# default_output = "output_videos/output_video.mp4"
# run_analysis(default_input, default_output)
from utils import (
read_video,
save_video,
measure_distance,
draw_player_stats,
convert_pixel_distance_to_meters
)
import constants
from trackers import PlayerTracker, BallTracker
from court_line_detector import CourtLineDetector
from mini_court import MiniCourt
import pandas as pd
from copy import deepcopy
import cv2
import os
def run_analysis(input_video_path, output_video_path):
input_video_path = os.path.abspath(input_video_path)
output_video_path = os.path.abspath(output_video_path)
# Add FPS detection here
cap = cv2.VideoCapture(input_video_path)
fps = cap.get(cv2.CAP_PROP_FPS)
if fps <= 0:
fps = 24
cap.release()
print(f"🎬 Detected FPS: {fps}")
video_frames = read_video(input_video_path)
if not video_frames:
raise ValueError(f"No frames found in video: {input_video_path}")
# Detect Players and Ball
player_tracker = PlayerTracker(model_path='yolov8x')
ball_tracker = BallTracker(model_path='models/yolo5_last.pt')
player_detections = player_tracker.detect_frames(
video_frames,
read_from_stub=True,
stub_path="tracker_stubs/player_detections.pkl"
)
ball_detections = ball_tracker.detect_frames(
video_frames,
read_from_stub=True,
stub_path="tracker_stubs/ball_detections.pkl"
)
ball_detections = ball_tracker.interpolate_ball_positions(ball_detections)
# Court Line Detection
court_model_path = "models/keypoints_model.pth"
court_line_detector = CourtLineDetector(court_model_path)
court_keypoints = court_line_detector.predict(video_frames[0])
# Choose players
player_detections = player_tracker.choose_and_filter_players(
court_keypoints, player_detections)
# Mini Court setup
mini_court = MiniCourt(video_frames[0])
ball_shot_frames = ball_tracker.get_ball_shot_frames(ball_detections)
# Convert to mini court coordinates
player_mini_detections, ball_mini_detections = mini_court.convert_bounding_boxes_to_mini_court_coordinates(
player_detections, ball_detections, court_keypoints)
player_stats_data = [{
'frame_num': 0,
'player_1_number_of_shots': 0,
'player_1_total_shot_speed': 0,
'player_1_last_shot_speed': 0,
'player_1_total_player_speed': 0,
'player_1_last_player_speed': 0,
'player_2_number_of_shots': 0,
'player_2_total_shot_speed': 0,
'player_2_last_shot_speed': 0,
'player_2_total_player_speed': 0,
'player_2_last_player_speed': 0,
}]
for ball_shot_ind in range(len(ball_shot_frames) - 1):
start_frame = ball_shot_frames[ball_shot_ind]
end_frame = ball_shot_frames[ball_shot_ind + 1]
# FIX: Use actual fps here instead of hardcoded 24
ball_shot_time = (end_frame - start_frame) / fps
ball_distance_px = measure_distance(
ball_mini_detections[start_frame][1],
ball_mini_detections[end_frame][1]
)
ball_distance_m = convert_pixel_distance_to_meters(
ball_distance_px,
constants.DOUBLE_LINE_WIDTH,
mini_court.get_width_of_mini_court()
)
ball_speed = (ball_distance_m / ball_shot_time) * 3.6 # km/h
player_positions = player_mini_detections[start_frame]
player_shot_ball = min(player_positions.keys(), key=lambda pid: measure_distance(
player_positions[pid], ball_mini_detections[start_frame][1]))
opponent_id = 1 if player_shot_ball == 2 else 2
opp_distance_px = measure_distance(
player_mini_detections[start_frame][opponent_id],
player_mini_detections[end_frame][opponent_id]
)
opp_distance_m = convert_pixel_distance_to_meters(
opp_distance_px,
constants.DOUBLE_LINE_WIDTH,
mini_court.get_width_of_mini_court()
)
opp_speed = (opp_distance_m / ball_shot_time) * 3.6
stats = deepcopy(player_stats_data[-1])
stats['frame_num'] = start_frame
stats[f'player_{player_shot_ball}_number_of_shots'] += 1
stats[f'player_{player_shot_ball}_total_shot_speed'] += ball_speed
stats[f'player_{player_shot_ball}_last_shot_speed'] = ball_speed
stats[f'player_{opponent_id}_total_player_speed'] += opp_speed
stats[f'player_{opponent_id}_last_player_speed'] = opp_speed
player_stats_data.append(stats)
df_stats = pd.DataFrame(player_stats_data)
frames_df = pd.DataFrame({'frame_num': list(range(len(video_frames)))})
df_stats = pd.merge(frames_df, df_stats, on='frame_num', how='left').ffill()
df_stats['player_1_average_shot_speed'] = df_stats['player_1_total_shot_speed'] / \
df_stats['player_1_number_of_shots']
df_stats['player_2_average_shot_speed'] = df_stats['player_2_total_shot_speed'] / \
df_stats['player_2_number_of_shots']
df_stats['player_1_average_player_speed'] = df_stats['player_1_total_player_speed'] / \
df_stats['player_2_number_of_shots']
df_stats['player_2_average_player_speed'] = df_stats['player_2_total_player_speed'] / \
df_stats['player_1_number_of_shots']
# Visual Output
output_frames = player_tracker.draw_bboxes(video_frames, player_detections)
output_frames = ball_tracker.draw_bboxes(output_frames, ball_detections)
output_frames = court_line_detector.draw_keypoints_on_video(
output_frames, court_keypoints)
output_frames = mini_court.draw_mini_court(output_frames)
output_frames = mini_court.draw_points_on_mini_court(
output_frames, player_mini_detections)
output_frames = mini_court.draw_points_on_mini_court(
output_frames, ball_mini_detections, color=(0, 255, 255))
output_frames = draw_player_stats(output_frames, df_stats)
print(f"Total frames to save: {len(output_frames)}")
# Debug prints
print(f"Total frames: {len(output_frames)}")
if len(output_frames) > 0:
print(f"Frame size: {output_frames[0].shape}, dtype: {output_frames[0].dtype}")
else:
print("No frames found to save!")
return None
# Ensure output directory exists
os.makedirs(os.path.dirname(output_video_path), exist_ok=True)
# FIX: Use improved save_video function with H264 codec
success = save_video_h264(output_frames, output_video_path, fps=fps)
if success:
return output_video_path
else:
print("❌ Failed to save video")
return None
def save_video_h264(output_video_frames, output_video_path, fps=24):
"""
Save video frames with H264 codec for Streamlit compatibility
"""
if not output_video_frames:
print("❌ No frames to save")
return False
# Get frame dimensions
height, width, channels = output_video_frames[0].shape
# Try different codec options for better compatibility
codecs_to_try = [
cv2.VideoWriter_fourcc(*'H264'), # H264 - best for web
cv2.VideoWriter_fourcc(*'X264'), # X264 alternative
cv2.VideoWriter_fourcc(*'mp4v'), # Fallback
cv2.VideoWriter_fourcc(*'XVID'), # Last resort
]
for codec in codecs_to_try:
try:
# Create VideoWriter object
out = cv2.VideoWriter(output_video_path, codec, fps, (width, height))
if not out.isOpened():
print(f"❌ Failed to open VideoWriter with codec: {codec}")
continue
print(f"✅ Using codec: {codec}")
print(f"📹 Saving {len(output_video_frames)} frames at {fps} fps")
print(f"📐 Frame size: {width}x{height}")
# Write frames
for i, frame in enumerate(output_video_frames):
if frame is None:
print(f"⚠️ Warning: Frame {i} is None, skipping")
continue
# Ensure frame is in correct format
if frame.dtype != 'uint8':
frame = frame.astype('uint8')
# Ensure frame has correct dimensions
if frame.shape != (height, width, channels):
print(f"⚠️ Warning: Frame {i} has wrong dimensions: {frame.shape}")
continue
out.write(frame)
# Progress indicator
if i % 50 == 0:
print(f"📝 Written {i}/{len(output_video_frames)} frames")
# Release the VideoWriter
out.release()
# Verify the output file exists and has content
if os.path.exists(output_video_path):
file_size = os.path.getsize(output_video_path)
print(f"📁 Video saved: {output_video_path}")
print(f"📊 File size: {file_size / (1024*1024):.2f} MB")
if file_size > 0:
return True
else:
print("❌ Output file is empty")
else:
print("❌ Output file was not created")
except Exception as e:
print(f"❌ Error with codec {codec}: {e}")
continue
print("❌ All codecs failed")
return False
if __name__ == "__main__":
default_input = "input_videos/input_video.mp4"
default_output = "output_videos/output_video.mp4"
run_analysis(default_input, default_output)