-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexporter.py
120 lines (90 loc) · 3.67 KB
/
exporter.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
import os
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID" # see issue #152
os.environ["CUDA_VISIBLE_DEVICES"] = ""
import sys
import numpy as np
from enum import Enum
class Decoder(Enum):
QUATERNION = 'Quaternion'
ROTATION_MATRIX = 'RotatationMatrix'
EULER = 'Euler'
AXIS_ANGLE = 'AxisAngle'
sys.path.append('motion/')
import BVH as BVH
import Animation as Animation
from Quaternions import Quaternions
from Pivots import Pivots
from itertools import islice
import eulerangles as eang
wdw = 240
step = 120
allDecodes = [Decoder.ROTATION_MATRIX, Decoder.AXIS_ANGLE, Decoder.EULER]
def process_file_rotations(filename, window=240, window_step=120):
anim, names, frametime = BVH.load(filename, order='zyx')
""" Do FK """
print(len(anim.rotations))
""" Remove Uneeded Joints """
# exported
rotations = anim.rotations[:, 0:len(anim.rotations)]
""" Remove Uneeded Joints """
reformatRotations = []
# encoding
for frame in rotations:
joints = []
for joint in frame:
if decodeType is Decoder.QUATERNION:
joints.append(joint)
elif decodeType is Decoder.EULER:
xyz = Quaternions(joint).euler().ravel()
joints.append([xyz[2], xyz[1], xyz[0]]) #we store in zyx
elif decodeType is Decoder.AXIS_ANGLE:
angle, axis = Quaternions(joint).angle_axis()
axis = axis.flatten()
axis = [axis[2], axis[1], axis[0]] #we store in zyx
input = np.insert(axis, 0, angle)
input = np.array(input) # 4 values
joints.append(input)
elif decodeType is Decoder.ROTATION_MATRIX:
euler = Quaternions(joint).euler().ravel() # we get x,y,z
# eang library uses convention z,y,x
m = eang.euler2mat(euler[2], euler[1], euler[0])
input = np.array(m[0].tolist() + m[1].tolist() + m[2].tolist()) # 9 values
joints.append(input)
reformatRotations.append(joints)
rotations = np.array(reformatRotations)
print(rotations.shape)
""" Slide over windows """
windows = []
for j in range(0, len(rotations) - window // 8, window_step):
""" If slice too small pad out by repeating start and end poses """
slice = rotations[j:j + window]
if len(slice) < window:
left = slice[:1].repeat((window - len(slice)) // 2 + (window - len(slice)) % 2, axis=0)
right = slice[-1:].repeat((window - len(slice)) // 2, axis=0)
slice = np.concatenate([left, slice, right], axis=0)
if len(slice) != window: raise Exception()
windows.append(slice)
return windows
def get_files(directory):
return [os.path.join(directory, f) for f in sorted(list(os.listdir(directory)))
if os.path.isfile(os.path.join(directory, f))
and f.endswith('.bvh') and f != 'rest.bvh']
for decodeType in allDecodes:
cmu_files = get_files('data/CMU')
cmu_rot_clips = []
wdw = 240
step = 120
for i, item in enumerate(cmu_files):
print('Processing Rotation %i of %i (%s)' % (i, len(cmu_files), item))
clips = process_file_rotations(item, window=wdw, window_step=step)
cmu_rot_clips += clips
data_clips = np.array(cmu_rot_clips)
print(data_clips.shape)
std = np.std(data_clips)
print(std)
mean = np.mean(data_clips)
data_clips -= mean
data_clips /= std
np.savez_compressed(
'cmu_{}_{}j_w{}x{}'.format(decodeType.value, data_clips.shape[2], wdw, step), clips=data_clips, std=std, mean=mean)
print('done')