import json import numpy as np from pathlib2 import Path 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) sliced = np.zeros((n_sequences, window_size, data.shape[1])).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/Obama/processing') # '../data/trinity/source' path_save = Path('data/obama-deca-5-10-08') # 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) # set windows nwin_train_secs = 5 # 6 nwin_test_secs = 10 # 20 window_overlap = 0.8 fps = 30 nwin_train = nwin_train_secs * fps # 3 * 30 = 90 nwin_test = nwin_test_secs * fps # 5 * 30 = 150 # 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 >= 5: continue if i % 50 == 0: 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") # 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("width=",width, "height=",height, "fps=",info["fps"], "-->", ipath_data) # 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) data_speech = np.load(ipath_feat_speech) # (nframe, 27) print(f"data_exp data = {data_exp.shape},data_pos = {data_pos.shape}, \n motion = {data_motion.shape}, speech = {data_speech.shape}") assert data_exp.shape[0] == data_speech.shape[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=window_overlap) speech_sliced = slice_data(data_speech, window_size=nwin_train, overlap=window_overlap) 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 data = {motion_sliced.shape}, speech data = {speech_sliced.shape}, minmax=({np.min(data_speech)},{np.max(data_speech)})") # save motion_train = np.concatenate(motion_train, axis=0) # (nframe, 180, 136) speech_train = np.concatenate(speech_train, axis=0) # (nframe, 180, 27) 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}")