-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmotion_utils.py
executable file
·161 lines (153 loc) · 6.87 KB
/
motion_utils.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
import os
import pickle
import gzip
import re
import random
from fairmotion.core.motion import Motion
from fairmotion.core.velocity import MotionWithVelocity
from fairmotion.data import bvh,asfamc
from fairmotion.utils import utils
def load_motions(motion_files, skel, char_info, verbose):
assert motion_files is not None
motion_all = []
motion_file_names = []
motion_file_asf_names = []
amc = False
for names in motion_files:
if type(names) is list:
asf_head,asf_tail = os.path.split(names[0])
motion_head,motion_tail = os.path.split(names[1])
motion_file_asf_names.append(asf_tail)
motion_file_names.append(motion_tail)
amc = True
else:
head, tail = os.path.split(names)
motion_file_names.append(tail)
if len(motion_files) > 0:
if isinstance(motion_files[0], str) or isinstance(motion_files[0], list):
motion_dict = {}
for i, file in enumerate(motion_files):
''' If the same file is already loaded, do not load again for efficiency'''
if amc:
asf_file = file[0]
file = file[1]
if file in motion_dict:
m = motion_dict[file]
else:
if file.endswith('bvh'):
if skel is not None:
m = bvh.load(motion=Motion(name=file, skel=skel),
file=file,
scale=1.0,
load_skel=False,
v_up_skel=char_info.v_up,
v_face_skel=char_info.v_face,
v_up_env=char_info.v_up_env)
else:
m = bvh.load(file=file,
scale=1.0,
v_up_skel=char_info.v_up,
v_face_skel=char_info.v_face,
v_up_env=char_info.v_up_env)
m = MotionWithVelocity.from_motion(m)
elif file.endswith('amc'):
m = asfamc.load(
file=asf_file,
motion= file,
scale=1.0,
v_up_skel=char_info.v_up,
v_face_skel=char_info.v_face,
v_up_env=char_info.v_up_env)
m = MotionWithVelocity.from_motion(m)
elif file.endswith('bin'):
m = pickle.load(open(file, "rb"))
elif file.endswith('gzip') or file.endswith('gz'):
with gzip.open(file, "rb") as f:
m = pickle.load(f)
else:
raise Exception('Unknown Motion File Type')
if verbose:
print('Loaded: %s'%file)
motion_all.append(m)
elif isinstance(motion_files[0], MotionWithVelocity):
motion_all = motion_files
else:
raise Exception('Unknown Type for Reference Motion')
return motion_all, motion_file_names, motion_file_asf_names
def collect_motion_files(project_dir, ref_motion_db):
ref_motion_file = []
for i, mdb in enumerate(ref_motion_db):
motions = []
if mdb.get('cluster_info'):
''' Read reference motions based on the cluster labels '''
assert mdb.get('data') is None, \
'This should not be specified when cluster_info is used'
dir = mdb['cluster_info'].get('dir')
label_file = mdb['cluster_info'].get('label_file')
sample_id = mdb['cluster_info'].get('sample_id')
labels = {}
assert label_file
if project_dir:
label_file = os.path.join(project_dir, label_file)
with open(label_file, 'r') as file:
for line in file:
l = re.split('[\t|\n|,|:| ]+', line)
id, rank, score, filename = int(l[0]), int(l[1]), float(l[2]), str(l[3])
if id not in labels.keys(): labels[id] = []
labels[id].append({'rank': rank, 'socre': score, 'filename': filename})
num_cluster = len(labels.keys())
for j in range(num_cluster):
if sample_id and j!=sample_id:
continue
for label in labels[j]:
if project_dir:
file = os.path.join(project_dir, dir, label['filename'])
motions.append(file)
else:
''' Read reference motions from the specified list of files and dirs '''
ref_motion_data = mdb.get('data')
motions = []
if ref_motion_data.get('file'):
motions += ref_motion_data.get('file')
if ref_motion_data.get('dir'):
for d in ref_motion_data.get('dir'):
if project_dir:
d = os.path.join(project_dir, d)
motions += utils.files_in_dir(d, ext=".bvh", sort=True)
if project_dir:
for j in range(len(motions)):
if type(motions[j]) is list:
motions[j][0] = os.path.join(project_dir, motions[j][0])
motions[j][1] = os.path.join(project_dir, motions[j][1])
else:
motions[j] = os.path.join(project_dir, motions[j])
'''
If num_sample is specified, we use only num_sample motions
from the entire reference motions.
'random' chooses randomly, 'top' chooses the first num_sample
'''
num_sample = mdb.get('num_sample')
if num_sample:
sample_method = mdb.get('sample_method')
if sample_method == 'random':
motions = random.choices(motions, k=num_sample)
elif sample_method == 'top':
motions = motions[:num_sample]
else:
raise NotImplementedError
ref_motion_file.append(motions)
return ref_motion_file
# for i, ref_motion in enumerate(self._ref_motion_all):
# for j, m in enumerate(ref_motion):
# _, p = conversions.T2Rp(m.get_pose_by_frame(0).get_root_transform())
# v = np.zeros(3)
# if i==0:
# v[1] = -p[1] - 2.0
# R = conversions.Az2R(-0.5*np.pi)
# else:
# v[1] = -p[1] + 2.0
# R = conversions.Az2R(0.5*np.pi)
# T = conversions.Rp2T(R, v)
# motion.transform(m, T, 0)
# bvh.save(m, "data/temp/"+self._ref_motion_file_names[i][j]+"_edited.bvh", verbose=True)
# exit(0)