forked from lisiyao21/Bailando
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_prepro_aistpp.py
281 lines (214 loc) · 8.75 KB
/
_prepro_aistpp.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
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
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this open-source project.
import os
import sys
import json
import random
import argparse
import essentia
import essentia.streaming
from essentia.standard import *
import librosa
import numpy as np
from extractor import FeatureExtractor
from aistplusplus_api.aist_plusplus.loader import AISTDataset
from smplx import SMPL
import torch
parser = argparse.ArgumentParser()
parser.add_argument('--input_video_dir', type=str, default='aist_plusplus_final/all_musics')
parser.add_argument('--input_annotation_dir', type=str, default='aist_plusplus_final')
parser.add_argument('--smpl_dir', type=str, default='smpl')
parser.add_argument('--train_dir', type=str, default='data/aistpp_train_wav')
parser.add_argument('--test_dir', type=str, default='data/aistpp_test_full_wav')
parser.add_argument('--split_train_file', type=str, default='aist_plusplus_final/splits/crossmodal_train.txt')
parser.add_argument('--split_test_file', type=str, default='aist_plusplus_final/splits/crossmodal_test.txt')
parser.add_argument('--split_val_file', type=str, default='aist_plusplus_final/splits/crossmodal_val.txt')
parser.add_argument('--sampling_rate', type=int, default=15360*2)
args = parser.parse_args()
extractor = FeatureExtractor()
if not os.path.exists(args.train_dir):
os.mkdir(args.train_dir)
if not os.path.exists(args.test_dir):
os.mkdir(args.test_dir)
split_train_file = args.split_train_file
split_test_file = args.split_test_file
split_val_file = args.split_val_file
def make_music_dance_set(video_dir, annotation_dir):
print('---------- Extract features from raw audio ----------')
# print(annotation_dir)
aist_dataset = AISTDataset(annotation_dir)
musics = []
dances = []
fnames = []
train = []
test = []
# music_dance_keys = []
# onset_beats = []
audio_fnames = sorted(os.listdir(video_dir))
# dance_fnames = sorted(os.listdir(dance_dir))
# audio_fnames = audio_fnames[:20] # for debug
# print(f'audio_fnames: {audio_fnames}')
train_file = open(split_train_file, 'r')
for fname in train_file.readlines():
train.append(fname.strip())
train_file.close()
test_file = open(split_test_file, 'r')
for fname in test_file.readlines():
test.append(fname.strip())
test_file.close()
test_file = open(split_val_file, 'r')
for fname in test_file.readlines():
test.append(fname.strip())
test_file.close()
ii = 0
all_names = train + test
for audio_fname in all_names:
# if ii > 1:
# break
# ii += 1
video_file = os.path.join(video_dir, audio_fname.split('_')[4] + '.wav')
print(f'Process -> {video_file}')
print(audio_fname)
seq_name, _ = AISTDataset.get_seq_name(audio_fname.replace('cAll', 'c02'))
if (seq_name not in train) and (seq_name not in test):
print(f'Not in set!')
continue
if seq_name in fnames:
print(f'Already scaned!')
continue
sr = args.sampling_rate
loader = None
try:
loader = essentia.standard.MonoLoader(filename=video_file, sampleRate=sr)
except RuntimeError:
continue
fnames.append(seq_name)
print(seq_name)
### load audio features ###
audio = loader()
audio = np.array(audio).T
feature = extract_acoustic_feature(audio, sr)
musics.append(feature.tolist())
### load pose sequence ###
# for seq_name in tqdm(seq_names):
print(f'Process -> {seq_name}')
smpl_poses, smpl_scaling, smpl_trans = AISTDataset.load_motion(
aist_dataset.motion_dir, seq_name)
smpl = None
smpl = SMPL(model_path=args.smpl_dir, gender='MALE', batch_size=1)
keypoints3d = smpl.forward(
global_orient=torch.from_numpy(smpl_poses[:, 0:1]).float(),
body_pose=torch.from_numpy(smpl_poses[:, 1:]).float(),
transl=torch.from_numpy(smpl_trans / smpl_scaling).float(),
).joints.detach().numpy()[:, 0:24, :]
nframes = keypoints3d.shape[0]
dances.append(keypoints3d.reshape(nframes, -1).tolist())
print(np.shape(dances[-1])) # (nframes, 72)
# return None, None, None
return musics, dances, fnames
def extract_acoustic_feature(audio, sr):
melspe_db = extractor.get_melspectrogram(audio, sr)
mfcc = extractor.get_mfcc(melspe_db)
mfcc_delta = extractor.get_mfcc_delta(mfcc)
# mfcc_delta2 = get_mfcc_delta2(mfcc)
audio_harmonic, audio_percussive = extractor.get_hpss(audio)
# harmonic_melspe_db = get_harmonic_melspe_db(audio_harmonic, sr)
# percussive_melspe_db = get_percussive_melspe_db(audio_percussive, sr)
chroma_cqt = extractor.get_chroma_cqt(audio_harmonic, sr, octave=7 if sr==15360*2 else 5)
# chroma_stft = extractor.get_chroma_stft(audio_harmonic, sr)
onset_env = extractor.get_onset_strength(audio_percussive, sr)
tempogram = extractor.get_tempogram(onset_env, sr)
onset_beat = extractor.get_onset_beat(onset_env, sr)[0]
# onset_tempo, onset_beat = librosa.beat.beat_track(onset_envelope=onset_env, sr=sr)
# onset_beats.append(onset_beat)
onset_env = onset_env.reshape(1, -1)
feature = np.concatenate([
# melspe_db,
mfcc, # 20
mfcc_delta, # 20
# mfcc_delta2,
# harmonic_melspe_db,
# percussive_melspe_db,
# chroma_stft,
chroma_cqt, # 12
onset_env, # 1
onset_beat, # 1
tempogram
], axis=0)
# mfcc, #20
# mfcc_delta, #20
# chroma_cqt, #12
# onset_env, # 1
# onset_beat, #1
feature = feature.transpose(1, 0)
print(f'acoustic feature -> {feature.shape}')
return feature
def align(musics, dances):
print('---------- Align the frames of music and dance ----------')
assert len(musics) == len(dances), \
'the number of audios should be equal to that of videos'
new_musics=[]
new_dances=[]
for i in range(len(musics)):
min_seq_len = min(len(musics[i]), len(dances[i]))
print(f'music -> {np.array(musics[i]).shape}, ' +
f'dance -> {np.array(dances[i]).shape}, ' +
f'min_seq_len -> {min_seq_len}')
new_musics.append([musics[i][j] for j in range(min_seq_len)])
new_dances.append([dances[i][j] for j in range(min_seq_len)])
return new_musics, new_dances, musics
def split_data(fnames):
train = []
test = []
print('---------- Split data into train and test ----------')
print(fnames)
train_file = open(split_train_file, 'r')
for fname in train_file.readlines():
train.append(fnames.index(fname.strip()))
train_file.close()
test_file = open(split_test_file, 'r')
for fname in test_file.readlines():
test.append(fnames.index(fname.strip()))
test_file.close()
test_file = open(split_val_file, 'r')
for fname in test_file.readlines():
test.append(fnames.index(fname.strip()))
test_file.close()
train = np.array(train)
test = np.array(test)
return train, test
def save(args, musics, dances, fnames, musics_raw):
print('---------- Save to text file ----------')
# fnames = sorted(os.listdir(os.path.join(args.input_dance_dir,inner_dir)))
# # fnames = fnames[:20] # for debug
# assert len(fnames)*2 == len(musics) == len(dances), 'alignment'
# fnames = sorted(fnames)
train_idx, test_idx = split_data(fnames)
# train_idx = sorted(train_idx)
print(f'train ids: {[fnames[idx] for idx in train_idx]}')
# test_idx = sorted(test_idx)
print(f'test ids: {[fnames[idx] for idx in test_idx]}')
print('---------- train data ----------')
for idx in train_idx:
with open(os.path.join(args.train_dir, f'{fnames[idx]}.json'), 'w') as f:
sample_dict = {
'id': fnames[idx],
'music_array': musics[idx],
'dance_array': dances[idx]
}
# print(sample_dict)
json.dump(sample_dict, f)
print('---------- test data ----------')
for idx in test_idx:
with open(os.path.join(args.test_dir, f'{fnames[idx]}.json'), 'w') as f:
sample_dict = {
'id': fnames[idx],
'music_array': musics_raw[idx], # musics[idx+i],
'dance_array': dances[idx]
}
# print(sample_dict)
json.dump(sample_dict, f)
if __name__ == '__main__':
musics, dances, fnames = make_music_dance_set(args.input_video_dir, args.input_annotation_dir)
musics, dances, musics_raw = align(musics, dances)
save(args, musics, dances, fnames, musics_raw)