-
Notifications
You must be signed in to change notification settings - Fork 16
/
main.py
185 lines (152 loc) · 4.96 KB
/
main.py
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
import timeit
import json
import cv2
import numpy as np
import supervision as sv
from trackers import (
PlayerTracker,
BallTracker,
KeypointsTracker,
Keypoint,
Keypoints,
PlayerKeypointsTracker,
TrackingRunner,
)
from config import *
SELECTED_KEYPOINTS = []
"""
PADEL COURT KEYPOINTS
-> To be selected using the image pop-up
k11--------------------k12
| |
k8-----------k9--------k10
| | |
| | |
| | |
k6----------------------k7
| | |
| | |
| | |
k3-----------k4---------k5
| |
k1----------------------k2
"""
def click_event(event, x, y, flags, params):
# checking for left mouse clicks
if event == cv2.EVENT_LBUTTONDOWN:
# displaying the coordinates
# on the Shell
SELECTED_KEYPOINTS.append((x, y))
# displaying the coordinates
# on the image window
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(img, str(x) + ',' +
str(y), (x,y), font,
1, (255, 0, 0), 2)
cv2.imshow('frame', img)
if __name__ == "__main__":
t1 = timeit.default_timer()
video_info = sv.VideoInfo.from_video_path(video_path=INPUT_VIDEO_PATH)
fps, w, h, total_frames = (
video_info.fps,
video_info.width,
video_info.height,
video_info.total_frames,
)
first_frame_generator = sv.get_video_frames_generator(
INPUT_VIDEO_PATH,
start=0,
stride=1,
end=1,
)
img = next(first_frame_generator)
if FIXED_COURT_KEYPOINTS_LOAD_PATH is not None:
with open(FIXED_COURT_KEYPOINTS_LOAD_PATH, "r") as f:
SELECTED_KEYPOINTS = json.load(f)
else:
cv2.imshow('frame', img)
cv2.setMouseCallback('frame', click_event)
# wait for a key to be pressed to exit
cv2.waitKey(0)
# close the window
cv2.destroyAllWindows()
if FIXED_COURT_KEYPOINTS_SAVE_PATH is not None:
with open(FIXED_COURT_KEYPOINTS_SAVE_PATH, "w") as f:
json.dump(SELECTED_KEYPOINTS, f)
fixed_keypoints_detection = Keypoints(
[
Keypoint(
id=i,
xy=tuple(float(x) for x in v)
)
for i, v in enumerate(SELECTED_KEYPOINTS)
]
)
keypoints_array = np.array(SELECTED_KEYPOINTS)
# Polygon to filter person detections inside padel court
polygon_zone = sv.PolygonZone(
np.concatenate(
(
np.expand_dims(keypoints_array[0], axis=0),
np.expand_dims(keypoints_array[1], axis=0),
np.expand_dims(keypoints_array[-1], axis=0),
np.expand_dims(keypoints_array[-2], axis=0),
),
axis=0
),
frame_resolution_wh=video_info.resolution_wh,
)
# FILTER FRAMES OF INTEREST (TODO)
# Instantiate trackers
players_tracker = PlayerTracker(
PLAYERS_TRACKER_MODEL,
polygon_zone,
batch_size=PLAYERS_TRACKER_BATCH_SIZE,
annotator=PLAYERS_TRACKER_ANNOTATOR,
show_confidence=True,
load_path=PLAYERS_TRACKER_LOAD_PATH,
save_path=PLAYERS_TRACKER_SAVE_PATH,
)
player_keypoints_tracker = PlayerKeypointsTracker(
PLAYERS_KEYPOINTS_TRACKER_MODEL,
train_image_size=PLAYERS_KEYPOINTS_TRACKER_TRAIN_IMAGE_SIZE,
batch_size=PLAYERS_KEYPOINTS_TRACKER_BATCH_SIZE,
load_path=PLAYERS_KEYPOINTS_TRACKER_LOAD_PATH,
save_path=PLAYERS_KEYPOINTS_TRACKER_SAVE_PATH,
)
ball_tracker = BallTracker(
BALL_TRACKER_MODEL,
BALL_TRACKER_INPAINT_MODEL,
batch_size=BALL_TRACKER_BATCH_SIZE,
median_max_sample_num=BALL_TRACKER_MEDIAN_MAX_SAMPLE_NUM,
median=None,
load_path=BALL_TRACKER_LOAD_PATH,
save_path=BALL_TRACKER_SAVE_PATH,
)
keypoints_tracker = KeypointsTracker(
model_path=KEYPOINTS_TRACKER_MODEL,
batch_size=KEYPOINTS_TRACKER_BATCH_SIZE,
model_type=KEYPOINTS_TRACKER_MODEL_TYPE,
fixed_keypoints_detection=fixed_keypoints_detection,
load_path=KEYPOINTS_TRACKER_LOAD_PATH,
save_path=KEYPOINTS_TRACKER_SAVE_PATH,
)
runner = TrackingRunner(
trackers=[
players_tracker,
player_keypoints_tracker,
ball_tracker,
keypoints_tracker,
],
video_path=INPUT_VIDEO_PATH,
inference_path=OUTPUT_VIDEO_PATH,
start=0,
end=MAX_FRAMES,
collect_data=COLLECT_DATA,
)
runner.run()
if COLLECT_DATA:
data = runner.data_analytics.into_dataframe(runner.video_info.fps)
data.to_csv(COLLECT_DATA_PATH)
t2 = timeit.default_timer()
print("Duration (min): ", (t2 - t1) / 60)