forked from axinc-ai/ailia-models
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathst_gcn.py
388 lines (327 loc) · 12.3 KB
/
st_gcn.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
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
import sys
import time
import numpy as np
import cv2
import ailia
# import original modules
from st_gcn_util import naive_pose_tracker, render_video, render_image
from st_gcn_labels import KINETICS_LABEL
sys.path.append('../../util')
from utils import get_base_parser, update_parser # noqa: E402
from utils import check_file_existance # noqa: E402
from model_utils import check_and_download_models # noqa: E402
from webcamera_utils import get_capture, get_writer # noqa: E402
# ======================
# Parameters
# ======================
WEIGHT_PATH = 'st_gcn.onnx'
MODEL_PATH = 'st_gcn.onnx.prototxt'
REMOTE_PATH = 'https://storage.googleapis.com/ailia-models/st_gcn/'
VIDEO_PATH = 'skateboarding.mp4'
POSE_KEY = [
ailia.POSE_KEYPOINT_NOSE,
ailia.POSE_KEYPOINT_SHOULDER_CENTER,
ailia.POSE_KEYPOINT_SHOULDER_RIGHT,
ailia.POSE_KEYPOINT_ELBOW_RIGHT,
ailia.POSE_KEYPOINT_WRIST_RIGHT,
ailia.POSE_KEYPOINT_SHOULDER_LEFT,
ailia.POSE_KEYPOINT_ELBOW_LEFT,
ailia.POSE_KEYPOINT_WRIST_LEFT,
ailia.POSE_KEYPOINT_HIP_RIGHT,
ailia.POSE_KEYPOINT_KNEE_RIGHT,
ailia.POSE_KEYPOINT_ANKLE_RIGHT,
ailia.POSE_KEYPOINT_HIP_LEFT,
ailia.POSE_KEYPOINT_KNEE_LEFT,
ailia.POSE_KEYPOINT_ANKLE_LEFT,
ailia.POSE_KEYPOINT_EYE_RIGHT,
ailia.POSE_KEYPOINT_EYE_LEFT,
ailia.POSE_KEYPOINT_EAR_RIGHT,
ailia.POSE_KEYPOINT_EAR_LEFT,
]
PYOPENPOSE_PATH = '/usr/local/python'
MODEL_LISTS = [
'openpose',
'pyopenpose',
'lw_human_pose'
]
# ======================
# Arguemnt Parser Config
# ======================
parser = get_base_parser('ST-GCN model', VIDEO_PATH, None)
parser.add_argument(
'--fps', default=30, type=int,
help='FPS of video.'
)
parser.add_argument(
'-a', '--arch', metavar='ARCH', default='openpose', choices=MODEL_LISTS,
help='model lists: ' + ' | '.join(MODEL_LISTS)
)
parser.add_argument(
'--img-save', action='store_true',
help='Instead of show video, save image file.'
)
args = update_parser(parser)
if args.arch == "pyopenpose":
sys.path.insert(0, PYOPENPOSE_PATH)
try:
from openpose import pyopenpose as op
except ImportError:
print('Can not find Openpose Python API.')
sys.exit(-1)
MODEL_POSE_PATH = 'pose/coco/pose_deploy_linevec.prototxt'
WEIGHT_POSE_PATH = 'pose/coco/pose_iter_440000.caffemodel'
REMOTE_POSE_PATH = 'http://posefs1.perception.cs.cmu.edu/OpenPose/models/pose/coco/'
elif args.arch == "lw_human_pose":
POSE_ALGORITHM = ailia.POSE_ALGORITHM_LW_HUMAN_POSE
MODEL_POSE_PATH = 'lightweight-human-pose-estimation.opt.onnx.prototxt'
WEIGHT_POSE_PATH = 'lightweight-human-pose-estimation.opt.onnx'
REMOTE_POSE_PATH = 'https://storage.googleapis.com/ailia-models/lightweight-human-pose-estimation/'
else:
POSE_ALGORITHM_OPEN_POSE_SINGLE_SCALE = (12)
# POSE_ALGORITHM = ailia.POSE_ALGORITHM_OPEN_POSE
POSE_ALGORITHM = POSE_ALGORITHM_OPEN_POSE_SINGLE_SCALE
MODEL_POSE_PATH = 'pose_deploy.prototxt'
WEIGHT_POSE_PATH = 'pose_iter_440000.caffemodel'
REMOTE_POSE_PATH = 'https://storage.googleapis.com/ailia-models/openpose/'
# ======================
# Secondaty Functions
# ======================
def pose_postprocess(pose_keypoints):
pose_keypoints[:, :, 0:2] = pose_keypoints[:, :, 0:2] - 0.5
pose_keypoints[:, :, 0][pose_keypoints[:, :, 2] == 0] = 0
pose_keypoints[:, :, 1][pose_keypoints[:, :, 2] == 0] = 0
return pose_keypoints
def postprocess(output, feature, num_person):
intensity = (feature * feature).sum(axis=0) ** 0.5
# get result
# classification result of the full sequence
voting_label = output.sum(axis=3). \
sum(axis=2).sum(axis=1).argmax(axis=0)
voting_label_name = KINETICS_LABEL[voting_label]
# FIXME: latest_frame_label_name is never used!
# classification result for each person of the latest frame
# latest_frame_label = [
# output[:, :, :, m].sum(axis=2)[:, -1].argmax(axis=0)
# for m in range(num_person)
# ]
# latest_frame_label_name = [KINETICS_LABEL[l] for l in latest_frame_label]
_, num_frame, _, num_person = output.shape
video_label_name = list()
for t in range(num_frame):
frame_label_name = list()
for m in range(num_person):
person_label = output[:, t, :, m].sum(axis=1).argmax(axis=0)
person_label_name = KINETICS_LABEL[person_label]
frame_label_name.append(person_label_name)
video_label_name.append(frame_label_name)
return voting_label_name, video_label_name, output, intensity
# ======================
# Main functions
# ======================
def recognize_offline(input, pose, net):
input = input[0]
capture = cv2.VideoCapture(input)
video_length = int(capture.get(cv2.CAP_PROP_FRAME_COUNT))
pose_tracker = naive_pose_tracker(data_frame=video_length)
# pose estimation
frame_index = 0
frames = list()
while True:
ret, frame = capture.read()
if frame is None:
break
if video_length <= frame_index:
break
source_H, source_W, _ = frame.shape
img = cv2.resize(
frame, (256 * source_W // source_H, 256))
frames.append(img)
H, W, _ = img.shape
# pose estimate
if args.arch == "pyopenpose":
datum = op.Datum()
datum.cvInputData = img
pose.emplaceAndPop([datum])
pose_keypoints = datum.poseKeypoints # (num_person, num_joint, 3)
if len(pose_keypoints.shape) != 3:
continue
# normalization
pose_keypoints[:, :, 0] = pose_keypoints[:, :, 0] / W
pose_keypoints[:, :, 1] = pose_keypoints[:, :, 1] / H
else:
img = cv2.cvtColor(img, cv2.COLOR_BGR2BGRA)
pose.compute(img)
count = pose.get_object_count()
pose_keypoints = np.zeros((count, 18, 3))
# pose_keypoints.shape : (num_person, num_joint, 3)
for idx in range(count):
person = pose.get_object_pose(idx)
for i, key in enumerate(POSE_KEY):
p = person.points[key]
pose_keypoints[idx, i, :] = [p.x, p.y, p.score]
pose_keypoints = pose_postprocess(pose_keypoints)
pose_tracker.update(pose_keypoints, frame_index)
frame_index += 1
print('Pose estimation ({}/{}).'.format(frame_index, video_length))
# action recognition
data = pose_tracker.get_skeleton_sequence()
input_data = np.expand_dims(data, 0)
net.set_input_shape(input_data.shape)
outputs = net.predict({
'data': input_data
})
output, feature = outputs
output = output[0]
feature = feature[0]
# classification result for each person of the latest frame
_, _, _, num_person = data.shape
out = postprocess(output, feature, num_person)
voting_label_name, video_label_name, output, intensity = out
return data, voting_label_name, video_label_name, output, intensity, frames
def recognize_from_file(input, pose, net):
# inference
print('Start inference...')
if args.benchmark:
print('BENCHMARK mode')
for i in range(5):
start = int(round(time.time() * 1000))
result = recognize_offline(input, pose, net)
end = int(round(time.time() * 1000))
print(f'\tailia processing time {end - start} ms')
else:
result = recognize_offline(input, pose, net)
print('Script finished successfully.')
# render the video
data, voting_label_name, video_label_name, output, intensity, frames = result
images = render_video(
data, voting_label_name,
video_label_name, intensity, frames)
# visualize or save
writer = None
for i, image in enumerate(images):
image = image.astype(np.uint8)
# init writer
if (writer is None) and (args.savepath is not None):
shape = image.shape
writer = get_writer(args.savepath, image.shape[0], image.shape[1])
if writer is not None:
writer.write(image)
elif args.img_save:
cv2.imwrite("output/ST-GCN-%08d.png" % i, image)
else:
cv2.imshow("ST-GCN", image)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
if cv2.getWindowProperty('ST-GCN', cv2.WND_PROP_VISIBLE) == 0:
break
if writer is not None:
writer.release()
def recognize_realtime(video, pose, net):
capture = get_capture(args.video)
pose_tracker = naive_pose_tracker()
# start recognition
start_time = time.time()
frame_index = 0
frame_shown = False
while True:
tic = time.time()
ret, frame = capture.read()
if cv2.waitKey(1) & 0xFF == ord('q') or not ret:
break
if frame_shown and cv2.getWindowProperty('ST-GCN', cv2.WND_PROP_VISIBLE) == 0:
break
source_H, source_W, _ = frame.shape
img = cv2.resize(
frame, (256 * source_W // source_H, 256))
H, W, _ = img.shape
# pose estimate
if args.arch == "pyopenpose":
datum = op.Datum()
datum.cvInputData = img
pose.emplaceAndPop([datum])
pose_keypoints = datum.poseKeypoints # (num_person, num_joint, 3)
if len(pose_keypoints.shape) != 3:
continue
# normalization
pose_keypoints[:, :, 0] = pose_keypoints[:, :, 0] / W
pose_keypoints[:, :, 1] = pose_keypoints[:, :, 1] / H
else:
img = cv2.cvtColor(img, cv2.COLOR_BGR2BGRA)
pose.compute(img)
count = pose.get_object_count()
if count == 0:
continue
pose_keypoints = np.zeros((count, 18, 3))
# pose_keypoints.shape : (num_person, num_joint, 3)
for idx in range(count):
person = pose.get_object_pose(idx)
for i, key in enumerate(POSE_KEY):
p = person.points[key]
pose_keypoints[idx, i, :] = [p.x, p.y, p.score]
# pose tracking
if video == '0':
frame_index = int((time.time() - start_time) * args.fps)
else:
frame_index += 1
pose_keypoints = pose_postprocess(pose_keypoints)
pose_tracker.update(pose_keypoints, frame_index)
# action recognition
data = pose_tracker.get_skeleton_sequence()
input_data = np.expand_dims(data, 0)
net.set_input_shape(input_data.shape)
outputs = net.predict({
'data': input_data
})
output, feature = outputs
output = output[0]
feature = feature[0]
# classification result for each person of the latest frame
_, _, _, num_person = data.shape
out = postprocess(output, feature, num_person)
voting_label_name, video_label_name, output, intensity = out
# visualization
app_fps = 1 / (time.time() - tic)
image = render_image(
data, voting_label_name,
video_label_name, intensity, frame, app_fps)
# show
if args.img_save:
cv2.imwrite("output/ST-GCN-%08d.png" % frame_index, image)
else:
cv2.imshow('ST-GCN', image)
frame_shown = True
capture.release()
cv2.destroyAllWindows()
print('Script finished successfully.')
def main():
# model files check and download
print("=== ST-GCN model ===")
check_and_download_models(WEIGHT_PATH, MODEL_PATH, REMOTE_PATH)
print("=== OpenPose model ===")
check_and_download_models(
WEIGHT_POSE_PATH, MODEL_POSE_PATH, REMOTE_POSE_PATH
)
# net initialize
net = ailia.Net(MODEL_PATH, WEIGHT_PATH, env_id=args.env_id)
if args.arch == "pyopenpose":
pose = op.WrapperPython()
params = dict(model_folder='.', model_pose='COCO')
pose.configure(params)
pose.start()
else:
pose = ailia.PoseEstimator(
MODEL_POSE_PATH,
WEIGHT_POSE_PATH,
env_id=args.env_id,
algorithm=POSE_ALGORITHM
)
if args.arch == "openpose":
pose.set_threshold(0.1)
if args.video is not None:
# realtime mode
recognize_realtime(args.video, pose, net)
else:
# offline mode
recognize_from_file(args.input, pose, net)
if __name__ == '__main__':
main()