-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_annotator.py
More file actions
314 lines (253 loc) · 10.6 KB
/
Copy pathcreate_annotator.py
File metadata and controls
314 lines (253 loc) · 10.6 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
import json
import os
import sys
from dataclasses import dataclass
from datetime import datetime
from pathlib import Path
from typing import Dict, List, Literal, Optional, Sequence, Union
import joblib
import numpy as np
import pandas as pd
from sklearn.pipeline import Pipeline
from tap import Tap
from tqdm import tqdm
import config
from components import (
AddFrameRangeColumnsToAnnotations,
CreateStyleWords,
CreateTextures,
FeatureAverager,
MinMedianMaxScaler,
NormalizeMinus1To1,
ToDF,
)
from functional import expand_annotations_df
from loadbvh import Skeleton, loadbvh
@dataclass
class LMAAnnotator:
joints_to_lma_pipeline: Pipeline
@classmethod
def create(
cls,
fps: int,
downsample_step: int,
window_style_word: int,
step_style_word: int,
) -> "LMAAnnotator":
create_textures_component = CreateTextures(
step=downsample_step, joint_indices=config.JOINT_INDICES
)
style_words_fps = fps // downsample_step
create_style_words_component = CreateStyleWords(
window=window_style_word, step=step_style_word, fps=style_words_fps
)
feature_averager = FeatureAverager(
groups=[
config.LMA_BODY_INDICES,
config.LMA_EFFORT_WEIGHT_STRONG_INDICES,
config.LMA_EFFORT_TIME_SUDDEN_INDICES,
config.LMA_EFFORT_FLOW_BOUND_INDICES,
config.LMA_SHAPE_INDICES,
config.LMA_SPACE_INDICES,
]
)
to_df = ToDF(
[
"BODY",
"EFFORT_WEIGHT_STRONG",
"EFFORT_TIME_SUDDEN",
"EFFORT_FLOW_BOUND",
"SHAPE",
"SPACE",
]
)
joints_to_lma = Pipeline(
[
("create_textures", create_textures_component),
("create_style_words", create_style_words_component),
("scaler", MinMedianMaxScaler()),
("averager", feature_averager),
("to_dataframe", to_df),
("normalize_to_0_1", NormalizeMinus1To1()),
(
"add_frame_range_columns",
AddFrameRangeColumnsToAnnotations(
step=step_style_word, window=window_style_word
),
),
]
)
return cls(joints_to_lma)
def annotate(
self, joints: np.ndarray, return_expanded: bool = False
) -> pd.DataFrame:
"""joints should be"""
if not return_expanded:
return self.joints_to_lma(joints)
else:
textures = self.joints_to_textures(joints)
annotations = self.texture_to_lma(textures)
downsampled_frames = textures.shape[-1]
expanded_annotations = expand_annotations_df(
annotations, pad_to_length=downsampled_frames
)
return expanded_annotations
def annotate_bvh(
self, bvh_file: Union[os.PathLike, str], return_expanded: bool = False
) -> pd.DataFrame:
skeleton, _, _, fps = loadbvh(bvh_file)
return self.annotate(skeleton.d_xyz, return_expanded)
def fit_to_joints(self, x: np.ndarray) -> pd.DataFrame:
return self.joints_to_lma_pipeline.fit_transform(x)
def fit_to_textures(self, textures: np.ndarray) -> pd.DataFrame:
return self.textures_to_lma_pipeline.fit_transform(textures)
def fit_to_style_words(self, x: np.ndarray) -> pd.DataFrame:
return self.style_words_to_lma_pipeline.fit_transform(x)
@property
def scaler(self) -> MinMedianMaxScaler:
return self.joints_to_lma_pipeline.steps[2][1]
def joints_to_lma(self, x: np.ndarray) -> pd.DataFrame:
return self.joints_to_lma_pipeline.transform(x)
@property
def joints_to_textures_pipeline(self) -> Pipeline:
return self.joints_to_lma_pipeline[:1]
def joints_to_textures(self, x: np.ndarray) -> np.ndarray:
return self.joints_to_textures_pipeline.transform(x)
@property
def textures_to_style_words_pipeline(self) -> Pipeline:
return self.joints_to_lma_pipeline[1:2]
def textures_to_style_words(self, x: np.ndarray) -> np.ndarray:
return self.textures_to_style_words_pipeline.transform(x)
@property
def joints_to_style_words_pipeline(self) -> Pipeline:
return self.joints_to_lma_pipeline[:2]
def joints_to_style_words(self, x: np.ndarray) -> np.ndarray:
return self.joints_to_style_words_pipeline.transform(x)
@property
def style_words_to_lma_pipeline(self) -> Pipeline:
return self.joints_to_lma_pipeline[2:]
def style_words_to_lma(self, x: np.ndarray) -> pd.DataFrame:
return self.style_words_to_lma_pipeline.transform(x)
@property
def textures_to_lma_pipeline(self) -> Pipeline:
return self.joints_to_lma_pipeline[1:]
def texture_to_lma(self, x: np.ndarray) -> pd.DataFrame:
return self.textures_to_lma_pipeline.transform(x)
def save(self, save_path: Union[os.PathLike, str]) -> None:
joblib.dump(self.joints_to_lma_pipeline, save_path)
@classmethod
def load(cls, save_path: Union[os.PathLike, str]) -> "LMAAnnotator":
save_path = Path(save_path)
joints_to_lma = joblib.load(save_path)
return cls(joints_to_lma)
@property
def style_words_to_scaled_style_words_pipeline(self) -> Pipeline:
return self.joints_to_lma_pipeline[2:3]
def scale_style_words(self, style_words: np.ndarray) -> np.ndarray:
return self.style_words_to_scaled_style_words_pipeline.transform(style_words)
def _repr_mimebundle_(self, **kwargs) -> dict:
return self.joints_to_lma_pipeline._repr_mimebundle_(**kwargs)
def __str__(self):
return str(self.joints_to_lma_pipeline)
# fmt: off
class ArgumentParser(Tap):
bvh_dir: Path = Path("BVHs") # the directory with the bvh files to fit the model to.
window_style_word: int = 16 # window size for style words.
step_style_word: int = 4 # step size for style words.
method: Literal["StyleWords", "MotionWords"] = "StyleWords" # Select which method to use for creating words.
create_annotations: bool = False # If set, extracts annotations for each motion
downsample_step: int = 24 # pick a frame every `downsample_step` frames from the original sequence.
fps: int = 120 # expected fps for original sequences.
# fmt: on
def main(
args: Optional[Sequence[str]] = None,
known_only: bool = False,
):
parser = ArgumentParser(underscores_to_dashes=True)
args = parser.parse_args(args, known_only)
annotator = LMAAnnotator.create(
args.fps, args.downsample_step, args.window_style_word, args.step_style_word
)
print("Annotator Pipeline:", annotator, sep="\n")
bvh_dir = args.bvh_dir
timestamp = timestamp_str()
save_dir = Path("save") / f"{timestamp}-{bvh_dir.stem}"
save_dir.mkdir(exist_ok=True, parents=True)
print(f"Results will be saved in: {save_dir}")
args_json = save_dir / "args.json"
with open(args_json, "w") as fp:
args_dict = make_serializable(args.as_dict())
json.dump(args_dict, fp, indent=2)
textures_save_dir = save_dir / "textures"
textures_save_dir.mkdir(exist_ok=True, parents=True)
style_words_save_dir = save_dir / "style-words"
style_words_save_dir.mkdir(exist_ok=True, parents=True)
joints_save_dir = save_dir / "joints"
joints_save_dir.mkdir(exist_ok=True, parents=True)
skeletons: List[Skeleton] = []
all_joints: List[np.ndarray] = []
all_style_words: List[np.ndarray] = []
results = {}
print(f"Loading Motion data from '{bvh_dir}'.")
bvh_files = sorted(bvh_dir.glob("*.bvh"))
for i, bvh_file in enumerate(tqdm(bvh_files)):
skeleton, frame_times, duration, fps = loadbvh(bvh_file)
skeletons.append(skeleton)
assert fps == args.fps
print(f"Loaded BVH file: {bvh_file}")
print(f"fps: {fps}")
print(f"Duration: {duration} seconds")
print(f"FPS: {frame_times.size // duration if duration > 0 else 'N/A'}")
print("*" * 30)
joints = skeleton.d_xyz
textures = annotator.joints_to_textures(joints)
style_words = annotator.textures_to_style_words(textures)
all_joints.append(joints)
all_style_words.append(style_words)
joints_save_path = joints_save_dir / f"{bvh_file.stem}.npy"
np.save(joints_save_path, joints)
textures_save_path = textures_save_dir / f"{bvh_file.stem}.npy"
np.save(textures_save_path, textures)
style_words_save_path = style_words_save_dir / f"{bvh_file.stem}.npy"
np.save(style_words_save_path, style_words)
results[bvh_file.stem] = {
"bvh_file": bvh_file,
"joint_file": joints_save_path,
"style_words_file": style_words_save_path,
"fps": fps,
"duration": duration,
"total_frames": skeleton.d_xyz.shape[-1],
"downsampled_frames": textures.shape[-1],
}
print(f"Loaded {len(skeletons)} motions")
style_words_stacked = np.vstack(all_style_words)
print("All style words stacked shape", style_words_stacked.shape)
print("Fitting model on all style words...", sep=" ")
annotator.fit_to_style_words(style_words_stacked)
print("Done")
annotator_save_path = save_dir / "lma-annotator.joblib"
annotator.save(annotator_save_path)
print("Annotator model saved as", annotator_save_path)
if args.create_annotations:
annotations_save_dir = save_dir / "annotations"
annotations_save_dir.mkdir(exist_ok=True, parents=True)
print(
f"Creating annotation for each motions. Annotations will be saved in '{annotations_save_dir}'..."
)
for bvh_file, joints in tqdm(zip(bvh_files, all_joints), total=len(bvh_files)):
annotations = annotator.annotate(joints, return_expanded=True)
annotations_file = annotations_save_dir / f"{bvh_file.stem}.csv"
annotations.to_csv(annotations_file, index=False)
results[bvh_file.stem]["annotations_file"] = annotations_file
results_csv_file = save_dir / "results.csv"
results_df = pd.DataFrame.from_dict(results).T
results_df.to_csv(results_csv_file, index=True)
return os.EX_OK
def timestamp_str(date: Optional[datetime] = None) -> str:
date = date or datetime.now()
return date.strftime("%y%m%d-%H%M")
def make_serializable(d: Dict[str, Union[int, bool, str, Path]]):
d = {k: str(v) if isinstance(v, Path) else v for k, v in d.items()}
return d
if __name__ == "__main__":
sys.exit(main())