177 lines
6.6 KiB
Python
177 lines
6.6 KiB
Python
import json
|
|
import numpy as np
|
|
from pathlib2 import Path
|
|
import torchaudio
|
|
|
|
import os, sys
|
|
sys.path.insert(0, os.getcwd())
|
|
from common.logger import Logger
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
actor= "Actor6"
|
|
sub_folder = "processing2" # processing, smile, body
|
|
|
|
path_data = Path(f"/ssdata1/lsen/DATA/{actor}/{sub_folder}")
|
|
path_save = Path(f'data/{actor}/{actor}-raw-{sub_folder}-10min')
|
|
path_save.mkdir(parents=True, exist_ok=True)
|
|
|
|
# set logging
|
|
logger = Logger(path_save, postfix=f"train-audio2mesh-{sub_folder}")
|
|
|
|
# get files
|
|
v_dir_videos = sorted(path_data.glob("*/audio/feat_DECA_exp_trim.npy"))
|
|
logger.info(f"Have {len(v_dir_videos)} dir files")
|
|
|
|
v_dir_videos = [ipath.parent.parent for ipath in v_dir_videos]
|
|
vids = set([ipath.stem for ipath in v_dir_videos])
|
|
# logger.info(f"vids total= {vids}")
|
|
|
|
# training set
|
|
if sub_folder == "processing2":
|
|
vid_test = set(["4R3A0139", "4R3A0140"])
|
|
vid_valid = set(["4R3A0139", "4R3A0140"])
|
|
vid_train = vids - vid_test - vid_valid
|
|
logger.info(f"vid test= {vid_test} \nvalid= {vid_valid}")
|
|
logger.info(f"vid train= {vid_train}")
|
|
else:
|
|
vid_test = set(["4R3A0110", "4R3A0112", "4R3A0113"])
|
|
vid_valid = set(["4R3A0110", "4R3A0112", "4R3A0113"])
|
|
vid_train = vids - vid_test - vid_valid
|
|
logger.info(f"vid test= {vid_test} \nvalid= {vid_valid}")
|
|
logger.info(f"vid train= {vid_train}")
|
|
|
|
|
|
# for each video
|
|
data_train, data_valid, data_test = {}, {}, {}
|
|
list_train, list_valid, list_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
|
|
# if durations_train / 3600 > 6: break
|
|
|
|
logger.info(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_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():
|
|
logger.info(f"NOT existing {ipath_speech}")
|
|
continue
|
|
|
|
if not ipath_feat_exp.exists():
|
|
logger.info(f"NOT existing {ipath_feat_exp}")
|
|
continue
|
|
|
|
# load video info
|
|
with open(str(ipath_info), "r") as f:
|
|
info = json.load(f)
|
|
fps = info["fps"]
|
|
nframes = info['nframes']
|
|
logger.info(f"info={info}, duration = {nframes/fps}")
|
|
assert fps == 50, "fps should be 50"
|
|
|
|
|
|
# laod motion
|
|
data_exp = np.load(ipath_feat_exp) # (nframe, 50)
|
|
data_pos = np.load(ipath_feat_pos) # (nframe, 6)
|
|
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}"
|
|
|
|
data_pos_head = data_pos[:, :3] # (nframe, 3)
|
|
data_pos_lips = data_pos[:, 3:] # (nframe, 3)
|
|
data_motion = np.concatenate([data_exp, data_pos_lips], axis=1) # (nframe, 53) --> fps=30
|
|
logger.info(f"motion = {data_motion.shape}")
|
|
|
|
# load raw audio
|
|
audio_raw, sample_rate = torchaudio.load(ipath_speech)
|
|
logger.info(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()
|
|
logger.info(f"audio_raw shape = {audio_raw.shape}, sr = {16000}, duration = {(len(audio_raw)/16000)}")
|
|
|
|
# ####################################
|
|
# # clips 4min
|
|
# nclip = 4
|
|
# nframes = nclip * 60 * 50 # min * 60s * fps
|
|
# nsegmts = nclip * 60 * 16000 # min * 60s * sr
|
|
# data_motion = data_motion[:nframes]
|
|
# audio_raw = audio_raw[:nsegmts]
|
|
# data_idn = data_idn[:nframes]
|
|
# ####################################
|
|
|
|
if vid in vid_train:
|
|
# if durations_train / 3600 > 6: break
|
|
if durations_train / 60 > 10: continue
|
|
|
|
durations_train += nframes/fps
|
|
list_train.append(vid)
|
|
|
|
data_train[vid] = {
|
|
"fps": info["fps"],
|
|
"sr": 16000,
|
|
"nframe": nframes,
|
|
"data_motion": data_motion,
|
|
"data_speech": audio_raw,
|
|
"data_shape": data_idn,
|
|
}
|
|
|
|
|
|
if vid in vid_valid:
|
|
durations_valid += nframes/fps
|
|
list_valid.append(vid)
|
|
|
|
data_valid[vid] = {
|
|
"fps": info["fps"],
|
|
"sr": 16000,
|
|
"nframe": nframes,
|
|
"data_motion": data_motion,
|
|
"data_speech": audio_raw,
|
|
"data_shape": data_idn,
|
|
}
|
|
|
|
|
|
if vid in vid_test:
|
|
durations_test += nframes/fps
|
|
list_test.append(vid)
|
|
|
|
data_test[vid] = {
|
|
"fps": info["fps"],
|
|
"sr": 16000,
|
|
"nframe": nframes,
|
|
"data_motion": data_motion,
|
|
"data_speech": audio_raw,
|
|
"data_shape": data_idn,
|
|
}
|
|
|
|
|
|
logger.info(f"Total frames: data_train ({durations_train/60:.2f} min) ({durations_train/3600:.2f} h) , ids = ({len(data_train)})")
|
|
logger.info(f"Total frames: data_valid ({durations_valid/60:.2f} min) ({durations_valid/3600:.2f} h) , ids = ({len(data_valid)})")
|
|
logger.info(f"Total frames: data_test ({durations_test/60:.2f} min) ({durations_test/3600:.2f} h) , ids = ({len(data_test)})")
|
|
|
|
logger.info(f"actual train ID: > {list_train} ")
|
|
logger.info(f"actual valid ID: > {list_valid} ")
|
|
logger.info(f"actual test ID: > {list_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)
|
|
|
|
logger.info(f"--> {path_save}") |