-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
375 lines (317 loc) · 12.3 KB
/
main.py
File metadata and controls
375 lines (317 loc) · 12.3 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
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
from audio_handler import addAudioFiles
import cv2
import pandas as pd
from copy import deepcopy, copy
from time import sleep
import pickle
def main():
# Read Video
constants.INPUT_VIDEO_PATH = (
"input_videos/" + input("Enter video file name (without extension): ") + ".mp4"
)
doubles = input("Singles / doubles? (1 / 2): ")
if doubles == "2":
print("Doubles functionality to be added soon")
exit()
if doubles != "1":
exit()
rewrite = input("Need to rewrite detections? (y / n): ")
rewrite = 1 if rewrite == "y" else 0 if rewrite == "n" else -1
if rewrite == -1:
exit()
video_frames = read_video(constants.INPUT_VIDEO_PATH)
total_frames = len(video_frames)
frame_rate = 24 # 24fps
total_time_in_seconds = total_frames / frame_rate
# Detect Players and Ball
player_tracker = PlayerTracker(model_path="yolov8x")
ball_tracker = BallTracker(model_path="models/yolo5_last.pt")
frame_copy = video_frames[0].copy()
# Court Line Detector model
court_model_path = "models/keypoints_model.pth"
court_line_detector = CourtLineDetector(court_model_path)
if not rewrite:
with open("tracker_stubs/player_detections.pkl", "rb") as f:
player_detections = pickle.load(f)
with open("tracker_stubs/ball_detections.pkl", "rb") as f:
ball_detections = pickle.load(f)
with open("tracker_stubs/court_keypoints.pkl", "rb") as f:
court_keypoints_list = pickle.load(f)
else:
player_detections = []
ball_detections = []
court_keypoints_list = []
distances = []
for i in range(len(video_frames)):
if rewrite:
keypoints = court_line_detector.get_keypoints(video_frames[i])
player_detection = player_tracker.detect_frame(video_frames[i], keypoints)
ball_detection = ball_tracker.detect_frame(video_frames[i])
player_detections.append(player_detection)
ball_detections.append(ball_detection)
court_keypoints_list.append(keypoints)
video_frames[i] = ball_tracker.draw_bboxes(video_frames[i], ball_detections[i])
distances.append(
abs(player_detections[i][1][1] - player_detections[i][2][1])
if 2 in player_detections[i]
else 0
)
# Draw frame number on top left corner
cv2.putText(
video_frames[i],
f"Frame: {i}",
(10, 30),
cv2.FONT_HERSHEY_SIMPLEX,
1,
(0, 255, 0),
2,
)
if rewrite:
cv2.imshow("video_tracking", video_frames[i])
# Wait for 25ms and check if 'q' is pressed to exit
if cv2.waitKey(1) & 0xFF == ord("q"):
break
if rewrite:
cv2.destroyAllWindows()
sleep(1)
with open("tracker_stubs/player_detections.pkl", "wb") as f:
pickle.dump(player_detections, f)
with open("tracker_stubs/ball_detections.pkl", "wb") as f:
pickle.dump(ball_detections, f)
with open("tracker_stubs/court_keypoints.pkl", "wb") as f:
pickle.dump(court_keypoints_list, f)
ball_detections = ball_tracker.interpolate_ball_positions(ball_detections)
court_keypoints = court_line_detector.predict(frame_copy)
# choose players
# player_detections = player_tracker.choose_and_filter_players(
# court_keypoints, player_detections
# )
# MiniCourt
mini_court = MiniCourt(frame_copy)
# Detect ball shots
ball_shot_frames, audio_frames = ball_tracker.get_ball_shot_frames(
ball_detections, court_keypoints, distances
)
print("Ball shots: ", len(ball_shot_frames))
# Convert positions to mini court positions
player_mini_court_detections, ball_mini_court_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_1_score": 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,
"player_2_score": 0,
}
]
point_time = 0
audio_ind = 0
rallies = []
rally_display = {}
audioList = []
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]
ball_shot_time_in_seconds = (end_frame - start_frame) / 24 # 24fps
point_time += ball_shot_time_in_seconds
# Get distance covered by the ball
distance_covered_by_ball_pixels = measure_distance(
ball_mini_court_detections[start_frame][1],
ball_mini_court_detections[end_frame][1],
)
distance_covered_by_ball_meters = convert_pixel_distance_to_meters(
distance_covered_by_ball_pixels,
constants.DOUBLE_LINE_WIDTH,
mini_court.get_width_of_mini_court(),
)
# Speed of the ball shot in km/h
speed_of_ball_shot = (
distance_covered_by_ball_meters / ball_shot_time_in_seconds * 3.6
)
# player who the ball
player_positions = player_mini_court_detections[start_frame]
player_shot_ball = min(
player_positions.keys(),
key=lambda player_id: measure_distance(
player_positions[player_id], ball_mini_court_detections[start_frame][1]
),
)
# opponent player speed
opponent_player_id = 1 if player_shot_ball == 2 else 2
if (
opponent_player_id in player_mini_court_detections[start_frame]
and opponent_player_id in player_mini_court_detections[end_frame]
):
distance_covered_by_opponent_pixels = measure_distance(
player_mini_court_detections[start_frame][opponent_player_id],
player_mini_court_detections[end_frame][opponent_player_id],
)
distance_covered_by_opponent_meters = convert_pixel_distance_to_meters(
distance_covered_by_opponent_pixels,
constants.DOUBLE_LINE_WIDTH,
mini_court.get_width_of_mini_court(),
)
speed_of_opponent = (
distance_covered_by_opponent_meters / ball_shot_time_in_seconds * 3.6
)
current_player_stats = deepcopy(player_stats_data[-1])
current_player_stats["frame_num"] = start_frame
current_player_stats[f"player_{player_shot_ball}_number_of_shots"] += 1
current_player_stats[
f"player_{player_shot_ball}_total_shot_speed"
] += speed_of_ball_shot
current_player_stats[f"player_{player_shot_ball}_last_shot_speed"] = (
speed_of_ball_shot
)
current_player_stats[
f"player_{opponent_player_id}_total_player_speed"
] += speed_of_opponent
current_player_stats[f"player_{opponent_player_id}_last_player_speed"] = (
speed_of_opponent
)
if audio_frames[audio_ind] < ball_shot_frames[ball_shot_ind + 1]:
current_player_stats[f"player_{player_shot_ball}_score"] += 1
rallies.append(round(point_time, 3))
rally_display[audio_frames[audio_ind]] = rallies.copy()
audio_ind += 1
point_time = 0
audioList.append(
{
"start_in": (audio_frames[audio_ind] - 200) // 24,
"audio_path": f"audios/pointplayer{player_shot_ball}.mp3",
}
)
player_stats_data.append(current_player_stats)
player_stats_data_df = pd.DataFrame(player_stats_data)
frames_df = pd.DataFrame({"frame_num": list(range(len(video_frames)))})
player_stats_data_df = pd.merge(
frames_df, player_stats_data_df, on="frame_num", how="left"
)
player_stats_data_df = player_stats_data_df.ffill()
player_stats_data_df["player_1_average_shot_speed"] = (
player_stats_data_df["player_1_total_shot_speed"]
/ player_stats_data_df["player_1_number_of_shots"]
)
player_stats_data_df["player_2_average_shot_speed"] = (
player_stats_data_df["player_2_total_shot_speed"]
/ player_stats_data_df["player_2_number_of_shots"]
)
player_stats_data_df["player_1_average_player_speed"] = (
player_stats_data_df["player_1_total_player_speed"]
/ player_stats_data_df["player_2_number_of_shots"]
)
player_stats_data_df["player_2_average_player_speed"] = (
player_stats_data_df["player_2_total_player_speed"]
/ player_stats_data_df["player_1_number_of_shots"]
)
# Calculate total distance traveled by each player
player_1_total_distance = (
player_stats_data_df["player_1_total_player_speed"].iloc[-1]
* total_time_in_seconds
/ 12.6
)
player_2_total_distance = (
player_stats_data_df["player_2_total_player_speed"].iloc[-1]
* total_time_in_seconds
/ 12.6
)
# Draw Player Stats
video_frames = draw_player_stats(video_frames, player_stats_data_df, distances)
rally_index = len(video_frames)
for i, (
frame,
players,
players_mini_court,
ball_mini_court,
keypoints,
) in enumerate(
zip(
video_frames,
player_detections,
player_mini_court_detections,
ball_mini_court_detections,
court_keypoints_list,
)
):
if i in rally_display:
rally_index = i
# Draw bounding boxes for players
if distances[i] > 150:
frame = player_tracker.draw_bboxes(frame, players)
# Draw mini court and points for players and ball
frame = mini_court.draw_mini_court(frame)
frame = mini_court.draw_points_on_mini_court(frame, players_mini_court)
frame = mini_court.draw_points_on_mini_court(
frame, ball_mini_court, color=(0, 255, 255)
)
frame = court_line_detector.draw_keypoints(frame, keypoints)
if rally_index < len(video_frames):
cv2.rectangle(frame, (8, 40), (127, 115), (0, 0, 0), -1)
for i in range(len(rally_display[rally_index])):
frame = cv2.putText(
frame,
f"Rally {i + 1} length: {rally_display[rally_index][i]} s",
(10, 50 + i * 15),
cv2.FONT_HERSHEY_SIMPLEX,
0.3,
(255, 255, 255),
1,
)
cv2.rectangle(frame, (8, 120), (200, 160), (0, 0, 0), -1)
frame = cv2.putText(
frame,
f"Total distance, Player 1: {player_1_total_distance:.2f} m",
(10, 130),
cv2.FONT_HERSHEY_SIMPLEX,
0.3,
(255, 255, 255),
1,
)
frame = cv2.putText(
frame,
f"Total distance, Player 2: {player_2_total_distance:.2f} m",
(10, 145),
cv2.FONT_HERSHEY_SIMPLEX,
0.3,
(255, 255, 255),
1,
)
# Update the frame in the list
video_frames[i] = frame
# Display the frame
cv2.imshow("video_analysis", frame)
# Wait for 25ms and check if 'q' is pressed to exit
if cv2.waitKey(25) & 0xFF == ord("q"):
break
cv2.destroyAllWindows()
save_video(video_frames, "output_videos/output_video.avi")
addAudioFiles(
"output_videos/output_video.avi",
audioList,
output_path="output_videos/output_with_audio.avi",
)
if __name__ == "__main__":
main()