-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinference.py
More file actions
332 lines (253 loc) · 11.4 KB
/
inference.py
File metadata and controls
332 lines (253 loc) · 11.4 KB
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
# This file is a modified version of https://github.com/TaatiTeam/MotionAGFormer/blob/master/demo/vis.py
# License: Apache 2.0
import sys
import argparse
import cv2
from demo.lib.preprocess import h36m_coco_format
from demo.lib.hrnet.gen_kpts import gen_video_kpts as hrnet_pose
import os
import numpy as np
import torch
import glob
from tqdm import tqdm
import copy
from utils.learning import load_model
from utils.tools import get_config
sys.path.append(os.getcwd())
from demo.lib.utils import normalize_screen_coordinates, camera_to_world
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
plt.switch_backend('agg')
matplotlib.rcParams['pdf.fonttype'] = 42
matplotlib.rcParams['ps.fonttype'] = 42
torch.set_float32_matmul_precision('high')
def show2Dpose(kps, img):
connections = [[0, 1], [1, 2], [2, 3], [0, 4], [4, 5],
[5, 6], [0, 7], [7, 8], [8, 9], [9, 10],
[8, 11], [11, 12], [12, 13], [8, 14], [14, 15], [15, 16]]
LR = np.array([0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0], dtype=bool)
lcolor = (255, 0, 0)
rcolor = (0, 0, 255)
thickness = 3
for j,c in enumerate(connections):
start = map(int, kps[c[0]])
end = map(int, kps[c[1]])
start = list(start)
end = list(end)
cv2.line(img, (start[0], start[1]), (end[0], end[1]), lcolor if LR[j] else rcolor, thickness)
cv2.circle(img, (start[0], start[1]), thickness=-1, color=(0, 255, 0), radius=3)
cv2.circle(img, (end[0], end[1]), thickness=-1, color=(0, 255, 0), radius=3)
return img
def show3Dpose(vals, ax):
ax.view_init(elev=15., azim=70)
lcolor=(0,0,1)
rcolor=(1,0,0)
I = np.array( [0, 0, 1, 4, 2, 5, 0, 7, 8, 8, 14, 15, 11, 12, 8, 9])
J = np.array( [1, 4, 2, 5, 3, 6, 7, 8, 14, 11, 15, 16, 12, 13, 9, 10])
LR = np.array([0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0], dtype=bool)
for i in np.arange( len(I) ):
x, y, z = [np.array( [vals[I[i], j], vals[J[i], j]] ) for j in range(3)]
ax.plot(x, y, z, lw=2, color = lcolor if LR[i] else rcolor)
RADIUS = 0.72
RADIUS_Z = 0.7
xroot, yroot, zroot = vals[0,0], vals[0,1], vals[0,2]
ax.set_xlim3d([-RADIUS+xroot, RADIUS+xroot])
ax.set_ylim3d([-RADIUS+yroot, RADIUS+yroot])
ax.set_zlim3d([-RADIUS_Z+zroot, RADIUS_Z+zroot])
ax.set_aspect('auto') # works fine in matplotlib==2.2.2
white = (1.0, 1.0, 1.0, 0.0)
ax.xaxis.set_pane_color(white)
ax.yaxis.set_pane_color(white)
ax.zaxis.set_pane_color(white)
ax.tick_params('x', labelbottom = False)
ax.tick_params('y', labelleft = False)
ax.tick_params('z', labelleft = False)
def get_pose2D(video_path, output_dir):
cap = cv2.VideoCapture(video_path)
width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
print('\nGenerating 2D pose...')
keypoints, scores = hrnet_pose(video_path, det_dim=416, num_peroson=1, gen_output=True)
keypoints, scores, valid_frames = h36m_coco_format(keypoints, scores)
# Add conf score to the last dim
keypoints = np.concatenate((keypoints, scores[..., None]), axis=-1)
output_dir += 'input_2D/'
os.makedirs(output_dir, exist_ok=True)
output_npz = output_dir + 'keypoints.npz'
np.savez_compressed(output_npz, reconstruction=keypoints)
def img2video(video_path, output_dir):
cap = cv2.VideoCapture(video_path)
fps = int(cap.get(cv2.CAP_PROP_FPS)) + 5
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
names = sorted(glob.glob(os.path.join(output_dir + 'pose/', '*.png')))
img = cv2.imread(names[0])
size = (img.shape[1], img.shape[0])
videoWrite = cv2.VideoWriter(output_dir + video_name + '.mp4', fourcc, fps, size)
for name in names:
img = cv2.imread(name)
videoWrite.write(img)
videoWrite.release()
def showimage(ax, img):
ax.set_xticks([])
ax.set_yticks([])
plt.axis('off')
ax.imshow(img)
def resample(n_frames):
even = np.linspace(0, n_frames, num=243, endpoint=False)
result = np.floor(even)
result = np.clip(result, a_min=0, a_max=n_frames - 1).astype(np.uint32)
return result
def turn_into_clips(keypoints):
clips = []
n_frames = keypoints.shape[1]
if n_frames <= 243:
new_indices = resample(n_frames)
clips.append(keypoints[:, new_indices, ...])
downsample = np.unique(new_indices, return_index=True)[1]
else:
for start_idx in range(0, n_frames, 243):
keypoints_clip = keypoints[:, start_idx:start_idx + 243, ...]
clip_length = keypoints_clip.shape[1]
if clip_length != 243:
new_indices = resample(clip_length)
clips.append(keypoints_clip[:, new_indices, ...])
downsample = np.unique(new_indices, return_index=True)[1]
else:
clips.append(keypoints_clip)
return clips, downsample
def turn_into_h36m(keypoints):
new_keypoints = np.zeros_like(keypoints)
new_keypoints[..., 0, :] = (keypoints[..., 11, :] + keypoints[..., 12, :]) * 0.5
new_keypoints[..., 1, :] = keypoints[..., 11, :]
new_keypoints[..., 2, :] = keypoints[..., 13, :]
new_keypoints[..., 3, :] = keypoints[..., 15, :]
new_keypoints[..., 4, :] = keypoints[..., 12, :]
new_keypoints[..., 5, :] = keypoints[..., 14, :]
new_keypoints[..., 6, :] = keypoints[..., 16, :]
new_keypoints[..., 8, :] = (keypoints[..., 5, :] + keypoints[..., 6, :]) * 0.5
new_keypoints[..., 7, :] = (new_keypoints[..., 0, :] + new_keypoints[..., 8, :]) * 0.5
new_keypoints[..., 9, :] = keypoints[..., 0, :]
new_keypoints[..., 10, :] = (keypoints[..., 1, :] + keypoints[..., 2, :]) * 0.5
new_keypoints[..., 11, :] = keypoints[..., 6, :]
new_keypoints[..., 12, :] = keypoints[..., 8, :]
new_keypoints[..., 13, :] = keypoints[..., 10, :]
new_keypoints[..., 14, :] = keypoints[..., 5, :]
new_keypoints[..., 15, :] = keypoints[..., 7, :]
new_keypoints[..., 16, :] = keypoints[..., 9, :]
return new_keypoints
def flip_data(data, left_joints=[1, 2, 3, 14, 15, 16], right_joints=[4, 5, 6, 11, 12, 13]):
"""
data: [N, F, 17, D] or [F, 17, D]
"""
flipped_data = copy.deepcopy(data)
flipped_data[..., 0] *= -1 # flip x of all joints
flipped_data[..., left_joints + right_joints, :] = flipped_data[..., right_joints + left_joints, :] # Change orders
return flipped_data
@torch.no_grad()
def get_pose3D(video_path, output_dir, args):
## Reload
model = load_model(args)
if torch.cuda.is_available():
# model = torch.nn.DataParallel(model)
model = model.cuda()
model = torch.compile(model)
# Put the pretrained model of MotionAGFormer in 'checkpoint/'
checkpoint_path = sorted(glob.glob(os.path.join('checkpoints/h36m/SHARPE-tiny', 'best_epoch.pth.tr')))[0]
if os.path.exists(checkpoint_path):
checkpoint = torch.load(checkpoint_path, lambda storage, loc: storage, weights_only=False)
model.load_state_dict(checkpoint['model'], strict=True)
else:
print("[WARN] Checkpoint path is empty")
model.eval()
## input
keypoints = np.load(output_dir + 'input_2D/keypoints.npz', allow_pickle=True)['reconstruction']
clips, downsample = turn_into_clips(keypoints)
cap = cv2.VideoCapture(video_path)
video_length = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
## 3D
print('\nGenerating 2D pose image...')
for i in tqdm(range(video_length)):
ret, img = cap.read()
if img is None:
continue
img_size = img.shape
input_2D = keypoints[0][i]
image = show2Dpose(input_2D, copy.deepcopy(img))
output_dir_2D = output_dir +'pose2D/'
os.makedirs(output_dir_2D, exist_ok=True)
cv2.imwrite(output_dir_2D + str(('%04d'% i)) + '_2D.png', image)
print('\nGenerating 3D pose...')
for idx, clip in enumerate(clips):
input_2D = normalize_screen_coordinates(clip, w=img_size[1], h=img_size[0])
input_2D_aug = flip_data(input_2D)
input_2D = torch.from_numpy(input_2D.astype('float32')).cuda()
input_2D_aug = torch.from_numpy(input_2D_aug.astype('float32')).cuda()
output_3D_non_flip = model(input_2D)
output_3D_flip = flip_data(model(input_2D_aug))
output_3D = (output_3D_non_flip + output_3D_flip) / 2
if idx == len(clips) - 1:
output_3D = output_3D[:, downsample]
output_3D[:, :, 0, :] = 0
post_out_all = output_3D[0].cpu().detach().numpy()
for j, post_out in enumerate(post_out_all):
rot = [0.1407056450843811, -0.1500701755285263, -0.755240797996521, 0.6223280429840088]
rot = np.array(rot, dtype='float32')
post_out = camera_to_world(post_out, R=rot, t=0)
post_out[:, 2] -= np.min(post_out[:, 2])
max_value = np.max(post_out)
post_out /= max_value
fig = plt.figure(figsize=(9.6, 5.4))
gs = gridspec.GridSpec(1, 1)
gs.update(wspace=-0.00, hspace=0.05)
ax = plt.subplot(gs[0], projection='3d')
show3Dpose(post_out, ax)
output_dir_3D = output_dir +'pose3D/'
os.makedirs(output_dir_3D, exist_ok=True)
str(('%04d'% (idx * 243 + j)))
plt.savefig(output_dir_3D + str(('%04d'% (idx * 243 + j))) + '_3D.png', dpi=200, format='png', bbox_inches='tight')
plt.close(fig)
print('Generating 3D pose successful!')
## all
image_2d_dir = sorted(glob.glob(os.path.join(output_dir_2D, '*.png')))
image_3d_dir = sorted(glob.glob(os.path.join(output_dir_3D, '*.png')))
print('\nGenerating demo...')
for i in tqdm(range(len(image_2d_dir))):
image_2d = plt.imread(image_2d_dir[i])
image_3d = plt.imread(image_3d_dir[i])
## crop
edge = (image_2d.shape[1] - image_2d.shape[0]) // 2
image_2d = image_2d[:, edge:image_2d.shape[1] - edge]
edge = 130
image_3d = image_3d[edge:image_3d.shape[0] - edge, edge:image_3d.shape[1] - edge]
## show
font_size = 12
fig = plt.figure(figsize=(15.0, 5.4))
ax = plt.subplot(121)
showimage(ax, image_2d)
ax.set_title("Input", fontsize = font_size)
ax = plt.subplot(122)
showimage(ax, image_3d)
ax.set_title("Reconstruction", fontsize = font_size)
## save
output_dir_pose = output_dir +'pose/'
os.makedirs(output_dir_pose, exist_ok=True)
plt.subplots_adjust(top=1, bottom=0, right=1, left=0, hspace=0, wspace=0)
plt.margins(0, 0)
plt.savefig(output_dir_pose + str(('%04d'% i)) + '_pose.png', dpi=200, bbox_inches = 'tight')
plt.close(fig)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--video', type=str, default='sample_video.mp4', help='input video')
parser.add_argument('--gpu', type=str, default='0', help='input video')
parser.add_argument("--config", type=str, default="configs/h36m/SHARPE-tiny.yaml", help="Path to the config file.")
opts = parser.parse_args()
args = get_config(opts.config)
os.environ["CUDA_VISIBLE_DEVICES"] = '0'
video_path = './demo/video/' + opts.video
video_name = video_path.split('/')[-1].split('.')[0]
output_dir = './demo/output/' + video_name + '/'
get_pose2D(video_path, output_dir)
get_pose3D(video_path, output_dir, args)
img2video(video_path, output_dir)
print('Generating demo successful!')