initial commit

This commit is contained in:
Sen Liang committed 2022-01-21 17:42:41 +08:00
commit c7175dd7ff
127 files changed
+13949

No files matched your search

@@ -0,0 +1,122 @@
import os
import sys
import json
import librosa
import numpy as np
from pathlib2 import Path
if __name__ == "__main__":
path_data = Path('/yrfs/lsen/DATA/Obama/processing') # '../data/trinity/source'
path_save = Path('data/obama-deca-mel') # processing
path_save.mkdir(parents=True, exist_ok=True)
v_dir_videos = sorted(path_data.glob("*/audio/feat_DECA_exp_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]
nv_total = len(v_dir_videos)
nv_valid, nv_test = 1, 1
nv_train = nv_total - (nv_valid + nv_test)
vid_test = vids[:nv_test]
vid_valid = vids[nv_test:nv_test+nv_valid]
vid_train = vids[nv_test+nv_valid:]
print("vid_test = ", vid_test)
# for each video
data_train, data_valid, data_test = {}, {}, {}
for i, ipath_data in enumerate(v_dir_videos):
vid = ipath_data.stem
if i % 50 == 0:
print(f"\n processing the {i}-th video {vid} ... ")
ipath_info = ipath_data.joinpath("video/info.json")
ipath_exp = ipath_data.joinpath("audio/feat_DECA_exp_trim.npy")
ipath_pos = ipath_data.joinpath("audio/feat_DECA_pos_trim.npy")
ipath_speech = ipath_data.joinpath("audio/feat_mel_no_align_trim.npy")
if not ipath_speech.exists():
print(f"ERROR: NOT Exist {ipath_speech}" )
continue
if not ipath_exp.exists() or not ipath_pos.exists():
print(f"ERROR: NOT Exist {ipath_pos} or {ipath_exp}" )
continue
# check existing
if not ipath_speech.exists() or not ipath_info.exists():
msg = "ERROR: NOT exsiting "
if not ipath_info.exists():
msg += f"\n\t {ipath_info} "
if not ipath_speech.exists():
msg += f"\n\t {ipath_speech} "
print(msg)
continue
# load video info
with open(str(ipath_info), "r") as f:
info = json.load(f)
width = info["width"][0] # 1280 or 1920
height = info["height"][0] # 720 or 1080
print("\nwidth=",width, "height=",height, "fps=",info["fps"], "-->", ipath_data)
# 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']}")
else:
print(f"ERROR: info['active_frames'] = {info['active_frames']}")
continue
# load data
# feat_idn = np.load(ipath_idn) # (nframe_trim, 100)
feat_exp = np.load(ipath_exp) # (nframe_trim, 50)
feat_pos = np.load(ipath_pos) # (nframe_trim, 6) # (N, 6)
data_motion = np.concatenate([feat_exp, feat_pos[:, 3:6]], axis=1) # (N, 53)
data_speech = np.load(ipath_speech) # (M, 80) please note that M != N
print(f"motion data = {data_motion.shape}, speech data = {data_speech.shape}")
# assert data_motion.shape[0] == data_speech.shape[0]
if vid in vid_train:
data_train[vid] = {
"W": width, "H": height, "FPS": info["fps"],
"nframe": nframe_trim, "trim": [ibeg, iend],
"data_motion": data_motion,
"data_speech": data_speech,
}
elif vid in vid_valid:
data_valid[vid] = {
"W": width, "H": height, "FPS": info["fps"],
"nframe": nframe_trim, "trim": [ibeg, iend],
"data_motion": data_motion,
"data_speech": data_speech,
}
else:
data_test[vid] = {
"W": width, "H": height, "FPS": info["fps"],
"nframe": nframe_trim, "trim": [ibeg, iend],
"data_motion": data_motion,
"data_speech": data_speech,
}
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"save data_train ({len(data_train)}), data_test ({len(data_test)}) data_valid({len(data_valid)}) --> {path_save}")