update audduio2pose v1.0
This commit is contained in:
1 parent
c2797657f8
commit
3a15011f4d
28 files changed
+1817
-483
No files matched your search
@@ -0,0 +1,184 @@
|
||||
import os
|
||||
import sys
|
||||
import json
|
||||
import librosa
|
||||
import numpy as np
|
||||
from pathlib2 import Path
|
||||
from scipy import interpolate
|
||||
|
||||
import torchaudio
|
||||
|
||||
def convert_fps(y, source_fps = 30, target_fps=25):
|
||||
"""
|
||||
y, nframes x nc_feat (with source_fps)
|
||||
"""
|
||||
|
||||
nsteps = y.shape[0]
|
||||
x = np.arange(0, nsteps, 1)
|
||||
# print("x:", x[::30], x[-10:])
|
||||
func = interpolate.interp1d(x, y=y, axis=0)
|
||||
|
||||
x_new = np.arange(0, nsteps-1, 1.0*source_fps/target_fps)
|
||||
# print("x_new", x_new[::30], x_new[-10:])
|
||||
y_new = func(x_new)
|
||||
|
||||
if source_fps < target_fps:
|
||||
y_new = np.concatenate([y_new, y[-1:]])
|
||||
|
||||
return y_new
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
path_data = Path('/mfs/lsen/DATA/Obama/processing') # '../data/trinity/source'
|
||||
|
||||
path_save = Path('data/Obama/obama-pose-8h') # processing
|
||||
path_save.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
v_dir_videos = sorted(path_data.glob("*/audio/feat_DECA_pos_cam_trim.npy"))
|
||||
print(f"Have {len(v_dir_videos)} dir files")
|
||||
|
||||
v_dir_videos = [ipath.parent.parent for ipath in v_dir_videos]
|
||||
vids = [ipath.stem for ipath in v_dir_videos]
|
||||
print(f"Have {len(v_dir_videos)} video id[:3] > {vids[:3]}")
|
||||
# print(f"vids total= {vids}")
|
||||
|
||||
vid_test = set(vids[ :2])
|
||||
vid_valid = set(vids[2:10])
|
||||
vid_train = set(vids) - vid_test - vid_valid
|
||||
|
||||
print(f"vid test= {vid_test} \nvalid= {vid_valid}")
|
||||
# print(f"vid train= {vid_train}")
|
||||
|
||||
# for each video
|
||||
data_train, data_valid, data_test = {}, {}, {}
|
||||
durations_train, durations_valid, durations_test = 0, 0, 0
|
||||
for i, ipath_data in enumerate(v_dir_videos):
|
||||
vid = ipath_data.stem
|
||||
|
||||
if vid not in vids: continue
|
||||
# if vid in vid_test: continue
|
||||
# if vid in vid_valid:continue
|
||||
print(f"\n processing the {i}-th video {vid} ... ")
|
||||
|
||||
ipath_info = ipath_data.joinpath("video/info.json")
|
||||
ipath_feat_exp = ipath_data.joinpath("audio/feat_DECA_exp_trim.npy")
|
||||
ipath_feat_pos = ipath_data.joinpath("audio/feat_DECA_pos_cam_trim.npy")
|
||||
ipath_feat_idn = ipath_data.joinpath("audio/feat_DECA_idn_trim.npy")
|
||||
ipath_speech = ipath_data.joinpath("audio/audio_trim.wav")
|
||||
|
||||
# check existing
|
||||
if not ipath_speech.exists():
|
||||
print(f"NOT existing {ipath_speech}")
|
||||
continue
|
||||
|
||||
if not ipath_feat_exp.exists():
|
||||
print(f"NOT existing {ipath_feat_exp}")
|
||||
continue
|
||||
|
||||
if not ipath_info.exists():
|
||||
print(f"ERROR: NOT Exist {ipath_info}")
|
||||
continue
|
||||
|
||||
# load video info
|
||||
with open(str(ipath_info), "r") as f:
|
||||
info = json.load(f)
|
||||
|
||||
fps = info["fps"]
|
||||
if len(info["active_frames"]) == 2:
|
||||
ibeg = info["active_frames"][0]
|
||||
iend = info["active_frames"][1]
|
||||
nframe_trim = iend - ibeg
|
||||
print(f"frame trim = {nframe_trim}, info['active_frames'] = {info['active_frames']}, duration ={nframe_trim / fps // 60}")
|
||||
else:
|
||||
print(f"ERROR: info['active_frames'] = {info['active_frames']}")
|
||||
continue
|
||||
|
||||
|
||||
# laod motion
|
||||
data_exp = np.load(ipath_feat_exp) # (nframe, 50)
|
||||
data_pos = np.load(ipath_feat_pos) # (nframe, 9)
|
||||
data_idn = np.load(ipath_feat_idn) # (nframe, 100)
|
||||
assert data_exp.shape[0] == data_pos.shape[0], f"data_exp shape={data_exp.shape}, data_pos shape={data_pos.shape}"
|
||||
assert data_exp.shape[0] == data_idn.shape[0], f"data_exp shape={data_exp.shape}, data_idn shape={data_idn.shape}"
|
||||
|
||||
# resample to 30fps
|
||||
fps_convert = 50
|
||||
data_exp = convert_fps(data_exp, source_fps=fps, target_fps=fps_convert)
|
||||
data_pos = convert_fps(data_pos, source_fps=fps, target_fps=fps_convert)
|
||||
data_idn = convert_fps(data_idn, source_fps=fps, target_fps=fps_convert)
|
||||
|
||||
nframe_convert = data_pos.shape[0]
|
||||
print(f"after convert motion = {data_pos.shape}, duration = {nframe_convert / fps_convert} vs. orignal duration = {nframe_trim / fps}")
|
||||
# assert data_motion.shape[0] == data_shape.shape[0], f"motion = {data_motion.shape[0]} shape = {data_shape.shape[0]}"
|
||||
|
||||
|
||||
# load raw audio
|
||||
audio_raw, sample_rate = torchaudio.load(ipath_speech)
|
||||
print(f"audio_raw shape = {audio_raw.shape}, sr = {sample_rate}, duration = {(len(audio_raw[0])/sample_rate)}")
|
||||
|
||||
if len(audio_raw.shape) == 2:
|
||||
audio_raw = audio_raw[0]
|
||||
|
||||
if sample_rate != 16000:
|
||||
resampler = torchaudio.transforms.Resample(sample_rate, 16_000)
|
||||
audio_raw = resampler(audio_raw).squeeze().numpy()
|
||||
print(f"audio_raw shape = {audio_raw.shape}, sr = {16000}, duration = {(len(audio_raw)/16000)}")
|
||||
|
||||
|
||||
if vid in vid_train:
|
||||
durations_train += nframe_convert/fps_convert
|
||||
|
||||
data_train[vid] = {
|
||||
"fps": fps_convert,
|
||||
"sr": 16000,
|
||||
"nframe": nframe_convert,
|
||||
"data_exp": data_exp,
|
||||
"data_idn": data_idn,
|
||||
"data_pos": data_pos,
|
||||
"data_speech": audio_raw,
|
||||
}
|
||||
|
||||
if durations_train / 3600 > 8:
|
||||
break
|
||||
|
||||
|
||||
if vid in vid_valid:
|
||||
durations_valid += nframe_convert/fps_convert
|
||||
|
||||
data_valid[vid] = {
|
||||
"fps": fps_convert,
|
||||
"sr": 16000,
|
||||
"nframe": nframe_convert,
|
||||
"data_exp": data_exp,
|
||||
"data_idn": data_idn,
|
||||
"data_pos": data_pos,
|
||||
"data_speech": audio_raw,
|
||||
}
|
||||
|
||||
|
||||
if vid in vid_test:
|
||||
durations_test += nframe_convert/fps_convert
|
||||
|
||||
data_test[vid] = {
|
||||
"fps": fps_convert,
|
||||
"sr": 16000,
|
||||
"nframe": nframe_convert,
|
||||
"data_exp": data_exp,
|
||||
"data_idn": data_idn,
|
||||
"data_pos": data_pos,
|
||||
"data_speech": audio_raw,
|
||||
}
|
||||
print(f"frames: data_train ({durations_train / 60 } min) ({durations_train / 3600 } h) , ids = ({len(data_train)})")
|
||||
print(f"frames: data_valid ({durations_valid / 60 } min) ({durations_valid / 3600 } h) , ids = ({len(data_valid)})")
|
||||
print(f"frames: data_test ({durations_test / 60 } min) ({durations_test / 3600 } h) , ids = ({len(data_test)})")
|
||||
|
||||
|
||||
np.savez(path_save.joinpath("data_train.npz"), data=data_train)
|
||||
np.savez(path_save.joinpath("data_valid.npz"), data=data_valid)
|
||||
np.savez(path_save.joinpath("data_test.npz"), data=data_test)
|
||||
|
||||
print(f"Total frames: data_train ({durations_train / 60 } min) ({durations_train / 3600 } h) , ids = ({len(data_train)})")
|
||||
print(f"Total frames: data_valid ({durations_valid / 60 } min) ({durations_valid / 3600 } h) , ids = ({len(data_valid)})")
|
||||
print(f"Total frames: data_test ({durations_test / 60 } min) ({durations_test / 3600 } h) , ids = ({len(data_test)})")
|
||||
print(f"--> {path_save}")
|
||||
Reference in new issue
Block a user