forked from antoinekeller/tennis_shot_recognition
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract_shots_as_features.py
215 lines (178 loc) · 5.97 KB
/
extract_shots_as_features.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
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
"""
Capture shots from annotation as a succession of features into a csv files
Note that we dont save useless features like eyes and ears positions.
"""
from argparse import ArgumentParser
from pathlib import Path
import numpy as np
import cv2
import pandas as pd
from extract_human_pose import (
HumanPoseExtractor,
)
columns = [
"nose_y",
"nose_x",
"left_shoulder_y",
"left_shoulder_x",
"right_shoulder_y",
"right_shoulder_x",
"left_elbow_y",
"left_elbow_x",
"right_elbow_y",
"right_elbow_x",
"left_wrist_y",
"left_wrist_x",
"right_wrist_y",
"right_wrist_x",
"left_hip_y",
"left_hip_x",
"right_hip_y",
"right_hip_x",
"left_knee_y",
"left_knee_x",
"right_knee_y",
"right_knee_x",
"left_ankle_y",
"left_ankle_x",
"right_ankle_y",
"right_ankle_x",
]
def draw_shot(frame, shot):
"""Draw shot name on frame (user-friendly)"""
cv2.putText(
frame,
shot,
(20, 50),
cv2.FONT_HERSHEY_SIMPLEX,
fontScale=0.8,
color=(0, 165, 255),
thickness=2,
)
print(f"Capturing {shot}")
if __name__ == "__main__":
parser = ArgumentParser(
description="Annotate (associate human pose to a tennis shot)"
)
parser.add_argument("video")
parser.add_argument("annotation")
parser.add_argument("out")
parser.add_argument(
"--show",
action="store_const",
const=True,
default=False,
help="Show frame",
)
parser.add_argument(
"--debug",
action="store_const",
const=True,
default=False,
help="Show sub frame",
)
args = parser.parse_args()
shots = pd.read_csv(args.annotation)
CURRENT_ROW = 0
NB_IMAGES = 30
shots_features = []
FRAME_ID = 1
IDX_FOREHAND = 1
IDX_BACKHAND = 1
IDX_NEUTRAL = 1
IDX_SERVE = 1
cap = cv2.VideoCapture(args.video)
assert cap.isOpened()
ret, frame = cap.read()
human_pose_extractor = HumanPoseExtractor(frame.shape)
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
if CURRENT_ROW >= len(shots):
print("Done, no more shots in annotation!")
break
human_pose_extractor.extract(frame)
# dont draw non-significant points/edges by setting probability to 0
human_pose_extractor.discard(["left_eye", "right_eye", "left_ear", "right_ear"])
features = human_pose_extractor.keypoints_with_scores.reshape(17, 3)
if shots.iloc[CURRENT_ROW]["FrameId"] - NB_IMAGES // 2 == FRAME_ID:
shots_features = []
if (
shots.iloc[CURRENT_ROW]["FrameId"] - NB_IMAGES // 2
<= FRAME_ID
<= shots.iloc[CURRENT_ROW]["FrameId"] + NB_IMAGES // 2
):
if np.mean(features[:, 2]) < 0.3:
CURRENT_ROW += 1
shots_features = []
print("Cancel this shot")
FRAME_ID += 1
continue
features = features[features[:, 2] > 0][:, 0:2].reshape(1, 13 * 2)
shot_class = shots.iloc[CURRENT_ROW]["Shot"]
shots_features.append(features)
draw_shot(frame, shot_class)
if FRAME_ID - NB_IMAGES // 2 + 1 == shots.iloc[CURRENT_ROW]["FrameId"]:
# add assert?
shots_df = pd.DataFrame(
np.concatenate(shots_features, axis=0),
columns=columns,
)
shots_df["shot"] = np.full(NB_IMAGES, shot_class)
if shot_class == "forehand":
outpath = Path(args.out).joinpath(
f"forehand_{IDX_FOREHAND:03d}.csv"
)
IDX_FOREHAND += 1
elif shot_class == "backhand":
outpath = Path(args.out).joinpath(
f"backhand_{IDX_BACKHAND:03d}.csv"
)
IDX_BACKHAND += 1
elif shot_class == "serve":
outpath = Path(args.out).joinpath(f"serve_{IDX_SERVE:03d}.csv")
IDX_SERVE += 1
shots_df.to_csv(outpath, index=False)
assert len(shots_df) == NB_IMAGES
print(f"saving {shot_class} to {outpath}")
CURRENT_ROW += 1
shots_features = []
elif (
shots.iloc[CURRENT_ROW]["FrameId"] - shots.iloc[CURRENT_ROW - 1]["FrameId"]
> NB_IMAGES
):
frame_id_between_shots = (
shots.iloc[CURRENT_ROW - 1]["FrameId"]
+ shots.iloc[CURRENT_ROW]["FrameId"]
) // 2
if (
frame_id_between_shots - NB_IMAGES // 2
< FRAME_ID
<= frame_id_between_shots + NB_IMAGES // 2
):
features = features[features[:, 2] > 0][:, 0:2].reshape(1, 13 * 2)
shots_features.append(features)
draw_shot(frame, "neutral")
if FRAME_ID == frame_id_between_shots + NB_IMAGES // 2:
shots_df = pd.DataFrame(
np.concatenate(shots_features, axis=0),
columns=columns,
)
shots_df["shot"] = np.full(NB_IMAGES, "neutral")
outpath = Path(args.out).joinpath(f"neutral_{IDX_NEUTRAL:03d}.csv")
print(f"saving neutral to {outpath}")
IDX_NEUTRAL += 1
shots_df.to_csv(outpath, index=False)
shots_features = []
# Display results on original frame
if args.show:
human_pose_extractor.draw_results_frame(frame)
cv2.imshow("Frame", frame)
human_pose_extractor.roi.update(human_pose_extractor.keypoints_pixels_frame)
k = cv2.waitKey(1)
if k == 27:
break
FRAME_ID += 1
cap.release()
cv2.destroyAllWindows()