-
Notifications
You must be signed in to change notification settings - Fork 26
/
bvh2features.py
86 lines (66 loc) · 2.82 KB
/
bvh2features.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
# This code was written by Simon Alexanderson
# and is released here: https://github.com/simonalexanderson/PyMO
import numpy as np
import pandas as pd
from sklearn.pipeline import Pipeline
from argparse import ArgumentParser
import glob
import os
import sys
module_path = os.path.abspath(os.path.join('..'))
if module_path not in sys.path:
sys.path.append(module_path)
from pymo.parsers import BVHParser
from pymo.data import Joint, MocapData
from pymo.preprocessing import *
from pymo.viz_tools import *
from pymo.writers import *
import joblib as jl
def extract_joint_angles(bvh_dir, files, dest_dir, pipeline_dir, fps):
p = BVHParser()
data_all = list()
for f in files:
ff = os.path.join(bvh_dir, f + '.bvh')
print(ff)
data_all.append(p.parse(ff))
data_pipe = Pipeline([
('dwnsampl', DownSampler(tgt_fps=fps, keep_all=False)),
('root', RootTransformer('hip_centric')),
('mir', Mirror(axis='X', append=True)),
('jtsel', JointSelector(['Spine','Spine1','Spine2','Spine3','Neck','Neck1','Head','RightShoulder', 'RightArm', 'RightForeArm', 'RightHand', 'LeftShoulder', 'LeftArm', 'LeftForeArm', 'LeftHand'], include_root=True)),
('exp', MocapParameterizer('expmap')),
('cnst', ConstantsRemover()),
('np', Numpyfier())
])
out_data = data_pipe.fit_transform(data_all)
# the datapipe will append the mirrored files to the end
assert len(out_data) == 2*len(files)
jl.dump(data_pipe, os.path.join(pipeline_dir + 'data_pipe.sav'))
fi=0
for f in files:
ff = os.path.join(dest_dir, f)
print(ff)
np.savez(ff + ".npz", clips=out_data[fi])
np.savez(ff + "_mirrored.npz", clips=out_data[len(files)+fi])
fi=fi+1
if __name__ == '__main__':
# Setup parameter parser
parser = ArgumentParser(add_help=False)
parser.add_argument('--bvh_dir', '-orig', required=True,
help="Path where original motion files (in BVH format) are stored")
parser.add_argument('--dest_dir', '-dest', required=True,
help="Path where extracted motion features will be stored")
parser.add_argument('--pipeline_dir', '-pipe', default="./utils/",
help="Path where the motion data processing pipeline will be stored")
params = parser.parse_args()
files = []
# Go over all BVH files
print("Going to pre-process the following motion files:")
for r, d, f in os.walk(params.bvh_dir):
for file in f:
print(file)
if '.bvh' in file:
ff=os.path.join(r, file)
basename = os.path.splitext(os.path.basename(ff))[0]
files.append(basename)
extract_joint_angles(params.bvh_dir, files, params.dest_dir, params.pipeline_dir , fps=20)