-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcreate_video_gt.py
223 lines (179 loc) · 7.56 KB
/
create_video_gt.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
from __future__ import division
from matplotlib import projections
import numpy as np
import h5py
import matplotlib.pyplot as plt
import matplotlib
import matplotlib.animation as animation
from mpl_toolkits.mplot3d import Axes3D
import viz
import time
import copy
import data_utils
import argparse
parser = argparse.ArgumentParser(description="Human Motion Model")
parser.add_argument('--sample_name', type=str, metavar='S', help='input sample file.') #default='samples.h5',
parser.add_argument('--action_name', default='walking_0', type=str, metavar='S', help='input action.')
parser.add_argument('--save_name', default='walking_new.gif', type=str, metavar='S', help='input file name')
parser.add_argument('--save', action='store_true', help='whether to save the gif')
args = parser.parse_args()
def fkl(angles, parent, offset, rotInd, expmapInd):
"""
angles: 99 long vector with 3d position and 3d joint angles in expmap format
parent: 32 long vector with parent child relationships in kinematic tree
offset: 96 long vector with bone length
rotInd: 32 long list with indices into angles
expmapInd: 32 long list with indices into expmap angles
Returns
xyz: 32x3 3d points that represent a person in 3d space.
"""
assert len(angles) == 99
# structure that indicates parents for each joint
njoints = 32
xyzStruct = [dict() for x in range(njoints)]
for i in np.arange(njoints):
if not rotInd[i]:
xangle, yangle, zangle = 0, 0, 0
else:
xangle = angles[rotInd[i][0]-1]
yangle = angles[rotInd[i][1]-1]
zangle = angles[rotInd[i][2]-1]
r = angles[expmapInd[i]]
thisRotation = data_utils.expmap2rotmat(r)
thisPosition = np.array([xangle, yangle, zangle])
if parent[i] == -1: # root node
xyzStruct[i]['rotation'] = thisRotation
xyzStruct[i]['xyz'] = np.reshape(offset[i,:], (1,3)) + thisPosition
else:
xyzStruct[i]['xyz'] = (offset[i,:] + thisPosition).dot(xyzStruct[parent[i]]['rotation']) + xyzStruct[parent[i]]['xyz']
xyzStruct[i]['rotation'] = thisRotation.dot(xyzStruct[parent[i]]['rotation'])
xyz = [xyzStruct[i]['xyz'] for i in range(njoints)]
xyz = np.array(xyz).squeeze()
xyz = xyz[:, [0,2,1]]
return np.reshape(xyz, [-1])
def revert_coordinate_space(channels, R0, T0):
"""
channels = n by 99 matrix of poses
R0 = 3x3 rotation for the first frame
T0 = 1x3 position for the first frame
Returns
channels_rec = the passed poses, but the first have T0 and R0, and the rest of the sequence is modified
"""
n, d = channels.shape
channels_rec = copy.copy(channels)
R_prev = R0
T_prev = T0
rootRotInd = np.arange(3, 6)
#Loop through the passed posses
for ii in range(n):
R_diff = data_utils.expmap2rotmat(channels[ii, rootRotInd])
R = R_diff.dot(R_prev)
channels_rec[ii, rootRotInd] = data_utils.rotmat2expmap(R)
T = T_prev + ((R_prev.T).dot(np.reshape(channels[ii, :3], [3,1]))).reshape(-1)
channels_rec[ii, :3] = T
T_prev = T
R_prev = R
return channels_rec
def _some_variables():
"""
We define some variables that are useful to run the kinematic tree
Args
None
Returns
parent: 32 long vector with parent child relationships in the kinematic tree
offset: 96 long vector with bone lengths
rotInd: 32 long list with indices into angles
expmapInd: 32 long list with indices into expmap angles
"""
parent = np.array([0, 1, 2, 3, 4, 5, 1, 7, 8, 9,10, 1,12,13,14,15,13,
17,18,19,20,21,20,23,13,25,26,27,28,29,28,31])-1
offset = np.array([0.000000,0.000000,0.000000,-132.948591,0.000000,0.000000,0.000000,-442.894612,0.000000,0.000000,-454.206447,0.000000,0.000000,0.000000,162.767078,0.000000,0.000000,74.999437,132.948826,0.000000,0.000000,0.000000,-442.894413,0.000000,0.000000,-454.206590,0.000000,0.000000,0.000000,162.767426,0.000000,0.000000,74.999948,0.000000,0.100000,0.000000,0.000000,233.383263,0.000000,0.000000,257.077681,0.000000,0.000000,121.134938,0.000000,0.000000,115.002227,0.000000,0.000000,257.077681,0.000000,0.000000,151.034226,0.000000,0.000000,278.882773,0.000000,0.000000,251.733451,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,99.999627,0.000000,100.000188,0.000000,0.000000,0.000000,0.000000,0.000000,257.077681,0.000000,0.000000,151.031437,0.000000,0.000000,278.892924,0.000000,0.000000,251.728680,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,99.999888,0.000000,137.499922,0.000000,0.000000,0.000000,0.000000])
offset = offset.reshape(-1,3)
rotInd = [[5, 6, 4],
[8, 9, 7],
[11, 12, 10],
[14, 15, 13],
[17, 18, 16],
[],
[20, 21, 19],
[23, 24, 22],
[26, 27, 25],
[29, 30, 28],
[],
[32, 33, 31],
[35, 36, 34],
[38, 39, 37],
[41, 42, 40],
[],
[44, 45, 43],
[47, 48, 46],
[50, 51, 49],
[53, 54, 52],
[56, 57, 55],
[],
[59, 60, 58],
[],
[62, 63, 61],
[65, 66, 64],
[68, 69, 67],
[71, 72, 70],
[74, 75, 73],
[],
[77, 78, 76],
[]]
expmapInd = np.split(np.arange(4,100)-1, 32)
return parent, offset, rotInd, expmapInd
def main():
#load all the data
parent, offset, rotInd, expmapInd = _some_variables()
#numpy implementation
if args.sample_name:
with h5py.File(args.sample_name, 'r') as h5f:
expmap_gt = h5f['expmap/gt/'+args.action_name][:]
expmap_pred = h5f['expmap/preds/'+args.action_name][:]
else:
data_dir = './data/h3.6m/dataset'
test_subject_ids = [5]
actions = ['discussion']
one_hot = False
test_set, _ = data_utils.load_data(data_dir,test_subject_ids, actions, one_hot)
subject = 5
subaction = 1
expmap_gt = test_set[(subject, 'discussion', subaction, 'even')]
expmap_pred = np.zeros_like(expmap_gt)
nframes_gt, nframes_pred = expmap_gt.shape[0], expmap_pred.shape[0]
#Put them together and revert the coordinate space
expmap_all = revert_coordinate_space(np.vstack((expmap_gt, expmap_pred)), np.eye(3), np.zeros(3))
expmap_gt = expmap_all[:nframes_gt,:]
expmap_pred = expmap_all[nframes_gt:,:]
#compute 3d points for each frame
xyz_gt, xyz_pred = np.zeros((nframes_gt, 96)), np.zeros((nframes_pred, 96))
for i in range(nframes_gt):
xyz_gt[i,:] = fkl(expmap_gt[i,:], parent, offset, rotInd, expmapInd)
for i in range(nframes_pred):
xyz_pred[i,:] = fkl(expmap_pred[i,:], parent, offset, rotInd, expmapInd)
#plot and animate
fig = plt.figure()
ax = plt.gca(projection='3d')
ob = viz.Ax3DPose(ax)
#to_draw = np.append(xyz_gt, xyz_pred, axis=0)
to_draw = xyz_gt
counter = 0
def update(x):
nonlocal counter
if counter < 25:
counter += 1
return ob.update(x)
else:
if counter == 50:
counter = 0
else:
counter += 1
return ob.update(x, lcolor="#9b59b6", rcolor="#9b59b6") ##9b59b6, #2ecc71
anim = animation.FuncAnimation(fig, update, frames=to_draw, interval=40)
if args.save:
anim.save(args.save_name, writer='imagemagick', fps=25)
else:
plt.show()
if __name__ == "__main__":
main()