-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo_offline.py
463 lines (379 loc) · 16.6 KB
/
demo_offline.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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
#!/usr/bin/env python
import os
import sys
import argparse
import json
import shutil
import time
import numpy as np
import skvideo.io
from .io import IO
import tools
import tools.utils as utils
from tqdm import tqdm
import cv2
"""HRNet import"""
import torch
import torch.nn.parallel
import torch.backends.cudnn as cudnn
import torch.optim
import torch.utils.data
import torch.utils.data.distributed
import torchvision.transforms as transforms
import torchvision
import HRNet.demo._init_paths
import HRNet.models
from config import cfg
from config import update_config
from core.function import get_final_preds
from utils.transforms import get_affine_transform
# from HRNet.demo.demo import *
from openpose.openpose import Openpose
class DemoOffline(IO):
def start(self):
# initiate
label_name_path = './resource/kinetics_skeleton/label_name.txt'
with open(label_name_path) as f:
label_name = f.readlines()
label_name = [line.rstrip() for line in label_name]
self.label_name = label_name
# pose estimation
# video, data_numpy = self.pose_estimation()
video, data_numpy = self.openpose_pose_estimation()
# action recognition
data = torch.from_numpy(data_numpy)
data = data.unsqueeze(0)
data = data.float().to(self.dev).detach() # (1, channel, frame, joint, person)
# model predict
voting_label_name, video_label_name, output, intensity = self.predict(data)
# render the video
images = self.render_video(data_numpy, voting_label_name,
video_label_name, intensity, video)
# video writer config
save_path = './output/' + "result.mp4"
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter(save_path, fourcc, 30.0, (next(images).shape[1], next(images).shape[0]))
# visualize
print("generating video result, please wait...")
for image in images:
image = image.astype(np.uint8)
# cv2.imshow("ST-GCN", image)
out.write(image)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
out.release()
print("result have been saved.")
def predict(self, data):
# forward
output, feature = self.model.extract_feature(data)
output = output[0]
feature = feature[0]
intensity = (feature * feature).sum(dim=0) ** 0.5
intensity = intensity.cpu().detach().numpy()
# get result
# classification result of the full sequence
voting_label = output.sum(dim=3).sum(
dim=2).sum(dim=1).argmax(dim=0)
voting_label_name = self.label_name[voting_label]
# classification result for each person of the latest frame
num_person = data.size(4)
latest_frame_label = [output[:, :, :, m].sum(
dim=2)[:, -1].argmax(dim=0) for m in range(num_person)]
latest_frame_label_name = [self.label_name[l]
for l in latest_frame_label]
num_person = output.size(3)
num_frame = output.size(1)
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(dim=1).argmax(dim=0)
person_label_name = self.label_name[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
def render_video(self, data_numpy, voting_label_name, video_label_name, intensity, video):
images = utils.visualization.stgcn_visualize(
data_numpy,
self.model.graph.edge,
intensity, video,
voting_label_name,
video_label_name,
self.arg.height)
return images
def pose_estimation(self):
# load openpose python api
if self.arg.openpose is not None:
sys.path.append('{}/python'.format(self.arg.openpose))
sys.path.append('{}/build/python'.format(self.arg.openpose))
try:
from openpose import pyopenpose as op
except:
print('Can not find Openpose Python API.')
return
video_name = self.arg.video.split('/')[-1].split('.')[0]
# initiate
opWrapper = op.WrapperPython() # create openpose wrapper
params = dict(model_folder='./models', model_pose='COCO') # configure params
opWrapper.configure(params) # configure params into wrapper
opWrapper.start() # create wrapper success
self.model.eval()
video_capture = cv2.VideoCapture(self.arg.video)
video_length = int(video_capture.get(cv2.CAP_PROP_FRAME_COUNT))
pose_tracker = naive_pose_tracker(data_frame=video_length)
# pose estimation
start_time = time.time()
frame_index = 0
video = list()
while (True):
# get image
ret, orig_image = video_capture.read()
if orig_image is None:
break
source_H, source_W, _ = orig_image.shape
orig_image = cv2.resize(
orig_image, (256 * source_W // source_H, 256))
H, W, _ = orig_image.shape
video.append(orig_image)
# pose estimation
datum = op.Datum() # manage openpose data class
datum.cvInputData = orig_image
opWrapper.emplaceAndPop([datum]) # start pose estimation
multi_pose = datum.poseKeypoints # (num_person, num_joint, 3)
if len(multi_pose.shape) != 3:
continue
# normalization
multi_pose[:, :, 0] = multi_pose[:, :, 0] / W
multi_pose[:, :, 1] = multi_pose[:, :, 1] / H
multi_pose[:, :, 0:2] = multi_pose[:, :, 0:2] - 0.5
multi_pose[:, :, 0][multi_pose[:, :, 2] == 0] = 0
multi_pose[:, :, 1][multi_pose[:, :, 2] == 0] = 0
# pose tracking
pose_tracker.update(multi_pose, frame_index)
frame_index += 1
print('Pose estimation ({}/{}).'.format(frame_index, video_length))
data_numpy = pose_tracker.get_skeleton_sequence()
return video, data_numpy
def openpose_pose_estimation(self):
# preparing
openpose = Openpose(weights_file="/home/ligaoqi/projects/python_projects/mmskeleton-master/openpose/models/posenet.pth", training=False)
video_name = self.arg.video.split('/')[-1].split('.')[0]
# initiate
# self.model.eval()
video_capture = cv2.VideoCapture(self.arg.video)
video_length = int(video_capture.get(cv2.CAP_PROP_FRAME_COUNT))
pose_tracker = naive_pose_tracker(data_frame=video_length)
# pose estimation
# start_time = time.time()
frame_index = 0
video = list()
for _ in tqdm(range(video_length), desc="pose estimation: "):
# get image
ret, orig_image = video_capture.read()
if orig_image is None:
break
source_H, source_W, _ = orig_image.shape
orig_image = cv2.resize(
orig_image, (256 * source_W // source_H, 256))
H, W, _ = orig_image.shape
video.append(orig_image)
# pose estimation
poses, scores, multi_pose = openpose.detect(orig_image, precise=False)
if len(multi_pose.shape) != 3:
continue
# normalization
multi_pose[:, :, 0] = multi_pose[:, :, 0] / W
multi_pose[:, :, 1] = multi_pose[:, :, 1] / H
multi_pose[:, :, 0:2] = multi_pose[:, :, 0:2] - 0.5
multi_pose[:, :, 0][multi_pose[:, :, 2] == 0] = 0
multi_pose[:, :, 1][multi_pose[:, :, 2] == 0] = 0
# pose tracking
pose_tracker.update(multi_pose, frame_index)
frame_index += 1
# print('Pose estimation ({}/{}).'.format(frame_index, video_length))
data_numpy = pose_tracker.get_skeleton_sequence()
return video, data_numpy
"""
def HRNet_pose_estimation(self):
# preparation
box_model = torchvision.models.detection.fasterrcnn_resnet50_fpn(pretrained=True)
box_model.to(CTX)
box_model.eval()
pose_model = eval('models.' + cfg.MODEL.NAME + '.get_pose_net')(
cfg, is_train=False
)
if cfg.TEST.MODEL_FILE:
print('=> loading model from {}'.format(cfg.TEST.MODEL_FILE))
pose_model.load_state_dict(torch.load(cfg.TEST.MODEL_FILE), strict=False)
else:
print('expected model defined in config at TEST.MODEL_FILE')
pose_model = torch.nn.DataParallel(pose_model, device_ids=cfg.GPUS)
pose_model.to(CTX)
pose_model.eval()
video_name = self.arg.video.split('/')[-1].split('.')[0]
# video message
video_capture = cv2.VideoCapture(self.arg.video)
video_length = int(video_capture.get(cv2.CAP_PROP_FRAME_COUNT))
video_count = int(video_capture.get(cv2.CAP_PROP_FRAME_COUNT))
# pose_tracker
pose_tracker = naive_pose_tracker(data_frame=video_length)
# pose estimation
start_time = time.time()
frame_index = 0
video = list()
for _ in tqdm(range(video_count), desc="Pose estimation processing"):
# get image
ret, image_bgr = video_capture.read()
if image_bgr is None:
break
input = []
image = image_bgr[:, :, [2, 1, 0]]
source_H, source_W, _ = image_bgr.shape
image_bgr = cv2.resize(
image_bgr, (256 * source_W // source_H, 256))
H, W, _ = image_bgr.shape
video.append(image_bgr)
img = cv2.cvtColor(image_bgr, cv2.COLOR_BGR2RGB)
img_tensor = torch.from_numpy(img / 255.).permute(2, 0, 1).float().to(CTX)
input.append(img_tensor
.0)
# object detection box
pred_boxes = get_person_detection_boxes(box_model, input, threshold=0.9)
# pose estimation
if len(pred_boxes) >= 1:
for box in pred_boxes:
center, scale = box_to_center_scale(box, cfg.MODEL.IMAGE_SIZE[0], cfg.MODEL.IMAGE_SIZE[1])
image_pose = image.copy() if cfg.DATASET.COLOR_RGB else image_bgr.copy()
pose_preds = get_pose_estimation_prediction(pose_model, image_pose, center, scale)
# normalization
# multi_pose (num_person, num_joint, 3)
multi_pose[:, :, 0] = multi_pose[:, :, 0] / W
multi_pose[:, :, 1] = multi_pose[:, :, 1] / H
multi_pose[:, :, 0:2] = multi_pose[:, :, 0:2] - 0.5
multi_pose[:, :, 0][multi_pose[:, :, 2] == 0] = 0
multi_pose[:, :, 1][multi_pose[:, :, 2] == 0] = 0
# pose tracking
pose_tracker.update(multi_pose, frame_index)
frame_index += 1
print('Pose estimation ({}/{}).'.format(frame_index, video_length))
data_numpy = pose_tracker.get_skeleton_sequence()
return video, data_numpy
"""
@staticmethod
def get_parser(add_help=False):
# parameter priority: command line > config > default
parent_parser = IO.get_parser(add_help=False)
parser = argparse.ArgumentParser(
add_help=add_help,
parents=[parent_parser],
description='Demo for Spatial Temporal Graph Convolution Network')
# region arguments yapf: disable
parser.add_argument('--video',
default='./resource/media/skateboarding.mp4',
help='Path to video')
parser.add_argument('--openpose',
default=None,
help='Path to openpose')
parser.add_argument('--model_input_frame',
default=128,
type=int)
parser.add_argument('--model_fps',
default=30,
type=int)
parser.add_argument('--height',
default=1080,
type=int,
help='height of frame in the output video.')
parser.set_defaults(
config='./config/st_gcn/kinetics-skeleton/demo_offline.yaml')
parser.set_defaults(print_log=False)
# endregion yapf: enable
return parser
class naive_pose_tracker:
""" A simple tracker for recording person poses and generating skeleton sequences.
For actual occasion, I recommend you to implement a robuster tracker.
Pull-requests are welcomed.
"""
def __init__(self, data_frame=128, num_joint=18, max_frame_dis=np.inf):
self.data_frame = data_frame
self.num_joint = num_joint
self.max_frame_dis = max_frame_dis
self.latest_frame = 0
self.trace_info = list()
def update(self, multi_pose, current_frame):
# multi_pose.shape: (num_person, num_joint, 3)
if current_frame <= self.latest_frame:
return
if len(multi_pose.shape) != 3:
return
score_order = (-multi_pose[:, :, 2].sum(axis=1)).argsort(axis=0)
for p in multi_pose[score_order]:
# match existing traces
matching_trace = None
matching_dis = None
for trace_index, (trace, latest_frame) in enumerate(self.trace_info):
# trace.shape: (num_frame, num_joint, 3)
if current_frame <= latest_frame:
continue
mean_dis, is_close = self.get_dis(trace, p)
if is_close:
if matching_trace is None:
matching_trace = trace_index
matching_dis = mean_dis
elif matching_dis > mean_dis:
matching_trace = trace_index
matching_dis = mean_dis
# update trace information
if matching_trace is not None:
trace, latest_frame = self.trace_info[matching_trace]
# padding zero if the trace is fractured
pad_mode = 'interp' if latest_frame == self.latest_frame else 'zero'
pad = current_frame - latest_frame - 1
new_trace = self.cat_pose(trace, p, pad, pad_mode)
self.trace_info[matching_trace] = (new_trace, current_frame)
else:
new_trace = np.array([p])
self.trace_info.append((new_trace, current_frame))
self.latest_frame = current_frame
def get_skeleton_sequence(self):
# remove old traces
valid_trace_index = []
for trace_index, (trace, latest_frame) in enumerate(self.trace_info):
if self.latest_frame - latest_frame < self.data_frame:
valid_trace_index.append(trace_index)
self.trace_info = [self.trace_info[v] for v in valid_trace_index]
num_trace = len(self.trace_info)
if num_trace == 0:
return None
data = np.zeros((3, self.data_frame, self.num_joint, num_trace))
for trace_index, (trace, latest_frame) in enumerate(self.trace_info):
end = self.data_frame - (self.latest_frame - latest_frame)
d = trace[-end:]
beg = end - len(d)
data[:, beg:end, :, trace_index] = d.transpose((2, 0, 1))
return data
# concatenate pose to a trace
def cat_pose(self, trace, pose, pad, pad_mode):
# trace.shape: (num_frame, num_joint, 3)
num_joint = pose.shape[0]
num_channel = pose.shape[1]
if pad != 0:
if pad_mode == 'zero':
trace = np.concatenate(
(trace, np.zeros((pad, num_joint, 3))), 0)
elif pad_mode == 'interp':
last_pose = trace[-1]
coeff = [(p + 1) / (pad + 1) for p in range(pad)]
interp_pose = [(1 - c) * last_pose + c * pose for c in coeff]
trace = np.concatenate((trace, interp_pose), 0)
new_trace = np.concatenate((trace, [pose]), 0)
return new_trace
# calculate the distance between a existing trace and the input pose
def get_dis(self, trace, pose):
last_pose_xy = trace[-1, :, 0:2]
curr_pose_xy = pose[:, 0:2]
mean_dis = ((((last_pose_xy - curr_pose_xy) ** 2).sum(1)) ** 0.5).mean()
wh = last_pose_xy.max(0) - last_pose_xy.min(0)
scale = (wh[0] * wh[1]) ** 0.5 + 0.0001
is_close = mean_dis < scale * self.max_frame_dis
return mean_dis, is_close