-
Notifications
You must be signed in to change notification settings - Fork 15
/
demo.py
300 lines (260 loc) · 11.8 KB
/
demo.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
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
import os
import cv2
import numpy as np
import math
import datetime
import argparse
from ultralytics import YOLO
from ultralytics.yolo.utils.plotting import Annotator, Colors
from copy import deepcopy
sport_list = {
'sit-up': {
'left_points_idx': [6, 12, 14],
'right_points_idx': [5, 11, 13],
'maintaining': 70,
'relaxing': 110,
'concerned_key_points_idx': [5, 6, 11, 12, 13, 14],
'concerned_skeletons_idx': [[14, 12], [15, 13], [6, 12], [7, 13]]
},
'pushup': {
'left_points_idx': [6, 8, 10],
'right_points_idx': [5, 7, 9],
'maintaining': 140,
'relaxing': 120,
'concerned_key_points_idx': [5, 6, 7, 8, 9, 10],
'concerned_skeletons_idx': [[9, 11], [7, 9], [6, 8], [8, 10]]
},
'squat': {
'left_points_idx': [11, 13, 15],
'right_points_idx': [12, 14, 16],
'maintaining': 80,
'relaxing': 140,
'concerned_key_points_idx': [11, 12, 13, 14, 15],
'concerned_skeletons_idx': [[16, 14], [14, 12], [17, 15], [15, 13]]
}
}
def calculate_angle(key_points, left_points_idx, right_points_idx):
def _calculate_angle(line1, line2):
# Calculate the slope of two straight lines
slope1 = math.atan2(line1[3] - line1[1], line1[2] - line1[0])
slope2 = math.atan2(line2[3] - line2[1], line2[2] - line2[0])
# Convert radians to angles
angle1 = math.degrees(slope1)
angle2 = math.degrees(slope2)
# Calculate angle difference
angle_diff = abs(angle1 - angle2)
# Ensure the angle is between 0 and 180 degrees
if angle_diff > 180:
angle_diff = 360 - angle_diff
return angle_diff
left_points = [[key_points.data[0][i][0], key_points.data[0][i][1]] for i in left_points_idx]
right_points = [[key_points.data[0][i][0], key_points.data[0][i][1]] for i in right_points_idx]
line1_left = [
left_points[1][0].item(), left_points[1][1].item(),
left_points[0][0].item(), left_points[0][1].item()
]
line2_left = [
left_points[1][0].item(), left_points[1][1].item(),
left_points[2][0].item(), left_points[2][1].item()
]
angle_left = _calculate_angle(line1_left, line2_left)
line1_right = [
right_points[1][0].item(), right_points[1][1].item(),
right_points[0][0].item(), right_points[0][1].item()
]
line2_right = [
right_points[1][0].item(), right_points[1][1].item(),
right_points[2][0].item(), right_points[2][1].item()
]
angle_right = _calculate_angle(line1_right, line2_right)
angle = (angle_left + angle_right) / 2
return angle
def plot(pose_result, plot_size_redio, show_points=None, show_skeleton=None):
class _Annotator(Annotator):
def kpts(self, kpts, shape=(640, 640), radius=5, line_thickness=2, kpt_line=True):
"""Plot keypoints on the image.
Args:
kpts (tensor): Predicted keypoints with shape [17, 3]. Each keypoint has (x, y, confidence).
shape (tuple): Image shape as a tuple (h, w), where h is the height and w is the width.
radius (int, optional): Radius of the drawn keypoints. Default is 5.
kpt_line (bool, optional): If True, the function will draw lines connecting keypoints
for human pose. Default is True.
line_thickness (int, optional): thickness of the kpt_line. Default is 2.
Note: `kpt_line=True` currently only supports human pose plotting.
"""
if self.pil:
# Convert to numpy first
self.im = np.asarray(self.im).copy()
nkpt, ndim = kpts.shape
is_pose = nkpt == 17 and ndim == 3
kpt_line &= is_pose # `kpt_line=True` for now only supports human pose plotting
colors = Colors()
for i, k in enumerate(kpts):
if show_points is not None:
if i not in show_points:
continue
color_k = [int(x) for x in self.kpt_color[i]] if is_pose else colors(i)
x_coord, y_coord = k[0], k[1]
if x_coord % shape[1] != 0 and y_coord % shape[0] != 0:
if len(k) == 3:
conf = k[2]
if conf < 0.5:
continue
cv2.circle(self.im, (int(x_coord), int(y_coord)),
int(radius * plot_size_redio), color_k, -1, lineType=cv2.LINE_AA)
if kpt_line:
ndim = kpts.shape[-1]
for i, sk in enumerate(self.skeleton):
if show_skeleton is not None:
if sk not in show_skeleton:
continue
pos1 = (int(kpts[(sk[0] - 1), 0]), int(kpts[(sk[0] - 1), 1]))
pos2 = (int(kpts[(sk[1] - 1), 0]), int(kpts[(sk[1] - 1), 1]))
if ndim == 3:
conf1 = kpts[(sk[0] - 1), 2]
conf2 = kpts[(sk[1] - 1), 2]
if conf1 < 0.5 or conf2 < 0.5:
continue
if pos1[0] % shape[1] == 0 or pos1[1] % shape[0] == 0 or pos1[0] < 0 or pos1[1] < 0:
continue
if pos2[0] % shape[1] == 0 or pos2[1] % shape[0] == 0 or pos2[0] < 0 or pos2[1] < 0:
continue
cv2.line(self.im, pos1, pos2, [int(x) for x in self.limb_color[i]],
thickness=int(line_thickness * plot_size_redio), lineType=cv2.LINE_AA)
if self.pil:
# Convert im back to PIL and update draw
self.fromarray(self.im)
annotator = _Annotator(deepcopy(pose_result.orig_img))
if pose_result.keypoints is not None:
for k in reversed(pose_result.keypoints.data):
annotator.kpts(k, pose_result.orig_shape, kpt_line=True)
return annotator.result()
def put_text(frame, exercise, count, fps, redio):
cv2.rectangle(
frame, (int(20 * redio), int(20 * redio)), (int(300 * redio), int(163 * redio)),
(55, 104, 0), -1
)
if exercise in sport_list.keys():
cv2.putText(
frame, f'Exercise: {exercise}', (int(30 * redio), int(50 * redio)), 0, 0.9 * redio,
(255, 255, 255), thickness=int(2 * redio), lineType=cv2.LINE_AA
)
elif exercise == 'No Object':
cv2.putText(
frame, f'No Object', (int(30 * redio), int(50 * redio)), 0, 0.9 * redio,
(255, 255, 255), thickness=int(2 * redio), lineType=cv2.LINE_AA
)
cv2.putText(
frame, f'Count: {count}', (int(30 * redio), int(100 * redio)), 0, 0.9 * redio,
(255, 255, 255), thickness=int(2 * redio), lineType=cv2.LINE_AA
)
cv2.putText(
frame, f'FPS: {fps}', (int(30 * redio), int(150 * redio)), 0, 0.9 * redio,
(255, 255, 255), thickness=int(2 * redio), lineType=cv2.LINE_AA
)
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('--model', default='yolov8s-pose.pt', type=str, help='path to model weight')
parser.add_argument('--sport', default='squat', type=str,
help='Currently supported "sit-up", "pushup" and "squat"')
parser.add_argument('--input', default="0", type=str, help='path to input video')
parser.add_argument('--save_dir', default=None, type=str, help='path to save output')
parser.add_argument('--show', default=True, type=bool, help='show the result')
args = parser.parse_args()
return args
def main():
# Obtain relevant parameters
args = parse_args()
# Load the YOLOv8 model
model = YOLO(args.model)
# Open the video file or camera
if args.input.isnumeric():
cap = cv2.VideoCapture(int(args.input))
else:
cap = cv2.VideoCapture(args.input)
# For save result video
if args.save_dir is not None:
save_dir = os.path.join(
args.save_dir, args.sport,
datetime.datetime.now().strftime('%Y-%m-%d-%H-%M-%S'))
if not os.path.exists(save_dir):
os.makedirs(save_dir)
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
fps = cap.get(cv2.CAP_PROP_FPS)
size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
output = cv2.VideoWriter(os.path.join(save_dir, 'result.mp4'), fourcc, fps, size)
# Set variables to record motion status
reaching = False
reaching_last = False
state_keep = False
counter = 0
# Loop through the video frames
while cap.isOpened():
# Read a frame from the video
success, frame = cap.read()
if success:
# Set plot size redio for inputs with different resolutions
plot_size_redio = max(frame.shape[1] / 960, frame.shape[0] / 540)
# Run YOLOv8 inference on the frame
results = model(frame)
# Preventing errors caused by special scenarios
if results[0].keypoints.shape[1] == 0:
if args.show:
put_text(frame, 'No Object', counter,
round(1000 / results[0].speed['inference'], 2), plot_size_redio)
scale = 640 / max(frame.shape[0], frame.shape[1])
show_frame = cv2.resize(frame, (0, 0), fx=scale, fy=scale)
cv2.imshow("YOLOv8 Inference", show_frame)
if args.save_dir is not None:
output.write(frame)
# Break the loop if 'q' is pressed
if cv2.waitKey(1) & 0xFF == ord("q"):
break
continue
# Get hyperparameters
left_points_idx = sport_list[args.sport]['left_points_idx']
right_points_idx = sport_list[args.sport]['right_points_idx']
# Calculate angle
angle = calculate_angle(results[0].keypoints, left_points_idx, right_points_idx)
# Determine whether to complete once
if angle < sport_list[args.sport]['maintaining']:
reaching = True
if angle > sport_list[args.sport]['relaxing']:
reaching = False
if reaching != reaching_last:
reaching_last = reaching
if reaching:
state_keep = True
if not reaching and state_keep:
counter += 1
state_keep = False
# Visualize the results on the frame
annotated_frame = plot(
results[0], plot_size_redio,
# sport_list[args.sport]['concerned_key_points_idx'],
# sport_list[args.sport]['concerned_skeletons_idx']
)
# annotated_frame = results[0].plot(boxes=False)
# add relevant information to frame
put_text(
annotated_frame, args.sport, counter, round(1000 / results[0].speed['inference'], 2), plot_size_redio)
# Display the annotated frame
if args.show:
scale = 640 / max(annotated_frame.shape[0], annotated_frame.shape[1])
show_frame = cv2.resize(annotated_frame, (0, 0), fx=scale, fy=scale)
cv2.imshow("YOLOv8 Inference", show_frame)
if args.save_dir is not None:
output.write(annotated_frame)
# Break the loop if 'q' is pressed
if cv2.waitKey(1) & 0xFF == ord("q"):
break
else:
# Break the loop if the end of the video is reached
break
# Release the video capture object and close the display window
cap.release()
if args.save_dir is not None:
output.release()
cv2.destroyAllWindows()
if __name__ == '__main__':
main()