import json import numpy as np from pathlib2 import Path def interpolate_features(features, input_rate, output_rate, output_len=None): num_features = features.shape[1] input_len = features.shape[0] seq_len = input_len / float(input_rate) if output_len is None: output_len = int(seq_len * output_rate) input_timestamps = np.arange(input_len) / float(input_rate) output_timestamps = np.arange(output_len) / float(output_rate) output_features = np.zeros((output_len, num_features)) for feat in range(num_features): output_features[:, feat] = np.interp(output_timestamps, input_timestamps, features[:, feat]) return output_features def slice_data(data, window_size, overlap): nframes = data.shape[0] overlap_frames = (int)(overlap*window_size) n_sequences = (nframes-overlap_frames)//(window_size-overlap_frames) if data.ndim == 2: sliced = np.zeros((n_sequences, window_size, data.shape[1])).astype(np.float32) else: sliced = np.zeros((n_sequences, window_size, data.shape[1], data.shape[2])).astype(np.float32) if n_sequences>0: # extract sequences from the data for i in range(0, n_sequences): frameIdx = (window_size-overlap_frames) * i sliced[i] = data[frameIdx:frameIdx+window_size].copy() else: print("WARNING: data too small for window") return sliced if __name__ == "__main__": path_data = Path('/yrfs/lsen/DATA/LuoXData/processing2') # '../data/trinity/source' # path_save = Path('data/LuoXData-ds093raw-deca-3-10-08-mini5') # processing path_save = Path('data/LuoXData-deca-5-10-07-mini') # 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 = set([ipath.stem for ipath in v_dir_videos]) print(f"vids total= {vids}") vid_test = set(["2021-P14"]) vid_valid = set(["2021-P14"]) #set(["2021-P14"]) # vid_train = set(['2021-P01', '2021-P02', '2021-P']) vid_train = vids - vid_test - vid_test print(f"vid test= {vid_test} \nvalid= {vid_valid}") print(f"vid train= {vid_train}") # set windows nwin_train_secs = 5 # 6 nwin_test_secs = 10 # 20 window_overlap = 0.7 fps = 50 nwin_train = nwin_train_secs * fps # 5 * 50 = 250 nwin_test = nwin_test_secs * fps # 10 * 50 = 500 # for each video motion_train, motion_valid, motion_test = [], [], [] speech_train, speech_valid, speech_test = [], [], [] for i, ipath_data in enumerate(v_dir_videos): vid = ipath_data.stem # if i >= 50: 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_trim.npy") ipath_feat_speech = ipath_data.joinpath("audio/feat_mel_trim.npy") # ipath_feat_speech = ipath_data.joinpath("audio/audio_trim_ds093.npy") # ipath_feat_speech = ipath_data.joinpath("audio/audio_trim_ds093_raw.npy") if not ipath_feat_speech.exists(): print(f"NOT existing {ipath_feat_speech}") continue # load video info with open(str(ipath_info), "r") as f: info = json.load(f) fps = info["fps"] print(f"info={info}") assert fps == 50, "fps should be 50" # laod motion & speech data data_exp = np.load(ipath_feat_exp) # (nframe, 50) data_pos = np.load(ipath_feat_pos) # (nframe, 6) pos_head = data_pos[:, :3] # (nframe, 3) pos_lips = data_pos[:, 3:] # (nframe, 3) data_motion = np.concatenate([data_exp, pos_lips], axis=1) # (nframe, 53) --> fps=30 data_speech = np.load(ipath_feat_speech) # (nframe, 256) --> fps=60 print(f"motion = {data_motion.shape}, speech = {data_speech.shape}") # filter data_speech # data_speech[data_speech < 0] = 0 if vid in vid_train: motion_sliced = slice_data(data_motion, window_size=nwin_train, overlap=window_overlap) speech_sliced = slice_data(data_speech, window_size=nwin_train, overlap=window_overlap) motion_train.append(motion_sliced) speech_train.append(speech_sliced) if vid in vid_valid: motion_sliced = slice_data(data_motion, window_size=nwin_train, overlap=0) speech_sliced = slice_data(data_speech, window_size=nwin_train, overlap=0) motion_valid.append(motion_sliced) speech_valid.append(speech_sliced) if vid in vid_test: motion_sliced = slice_data(data_motion, window_size=nwin_test, overlap=0) speech_sliced = slice_data(data_speech, window_size=nwin_test, overlap=0) motion_test.append(motion_sliced) speech_test.append(speech_sliced) print(f"sliced motion = {motion_sliced.shape}, speech = {speech_sliced.shape}, minmax=({np.min(data_speech)},{np.max(data_speech)}) \n") # save motion_train = np.concatenate(motion_train, axis=0) # (nframe, 250, 53) speech_train = np.concatenate(speech_train, axis=0) # (nframe, 250, 256) np.savez(path_save.joinpath("motion_train.npz"), data=motion_train) np.savez(path_save.joinpath("speech_train.npz"), data=speech_train) print(f"save train data motion({motion_train.shape}), speech({speech_train.shape}) -> {path_save}") motion_valid = np.concatenate(motion_valid, axis=0) # (350, 180, 136) speech_valid = np.concatenate(speech_valid, axis=0) # (350, 180, 27) np.savez(path_save.joinpath("motion_valid.npz"), data=motion_valid) np.savez(path_save.joinpath("speech_valid.npz"), data=speech_valid) print(f"save valid data motion({motion_valid.shape}), speech({speech_valid.shape}) -> {path_save}") motion_test = np.concatenate(motion_test, axis=0) # (41, 600, 136) speech_test = np.concatenate(speech_test, axis=0) # (41, 600, 27) np.savez(path_save.joinpath("motion_test.npz"), data=motion_test) np.savez(path_save.joinpath("speech_test.npz"), data=speech_test) print(f"save test data motion({motion_test.shape}), speech({speech_test.shape}) -> {path_save}\n")