import os import pickle import numpy as np from pathlib import Path import sys sys.path.insert(0, "/mnt/data/CODE/FaceDisentangleNetwork") from common.landmark import landmark_normalize if __name__ == "__main__": path_data = Path("/mnt/data2/DATA/LRW/processing") path_save = Path('data/LRW') # processing path_save.mkdir(parents=True, exist_ok=True) nvideo = 0 nvideo_target = 1000 # if not existing vpath_words = sorted(path_data.glob("*")) print(f"Have {len(vpath_words)} words folders, {vpath_words[0]}") motion_test = [] speech_test = [] for i, ipath_wd in enumerate(vpath_words): pid = ipath_wd.stem if nvideo >= nvideo_target: break # video vpath_videos = sorted(ipath_wd.glob("test/*/video/video_orignal.mp4")) print(f"\nid= {pid}, Have {len(vpath_videos)} video files, \n {vpath_videos[0]}") if len(vpath_videos) <= 0: continue for iv, ipath_video in enumerate(vpath_videos): if nvideo >= nvideo_target: break ipath_data = ipath_video.parent.parent vid = ipath_data.stem ipath_frames = ipath_data.joinpath("frames") ipath_landmk = ipath_data.joinpath("landmk") ipath_mfcc = ipath_data.joinpath("audio/feat_mfcc_align.npy") v_path_frames = sorted(ipath_frames.glob("*.jpg")) v_path_ladmks = sorted(ipath_landmk.glob("*.txt")) if not ipath_mfcc.exists(): continue # load speech mfcc = np.load(ipath_mfcc) # 29 x 27 nframes = len(v_path_frames) if not (nframes == 29 and len(v_path_ladmks) ==29 and mfcc.shape[0] == 29): print(f"have {nframes} frames, and {len(v_path_ladmks)} landmarks, {mfcc.shape[0]} mfcc") continue if iv % 50 == 0: print(f"--> pickling the {iv}-th video landmark: pid({vid}) have {len(v_path_frames)} frames in {ipath_frames}") # frames --> read landmarks v_ldmk = np.zeros((nframes, 68, 2), dtype=int) v_ldmk_norm = np.zeros((nframes, 68, 2), dtype=float) # normalize v_ldmk_auxs = np.zeros((nframes, 3), dtype=float) # [x_mean, y_mean, side] for k, kpath_frame in enumerate(v_path_frames): frame_id = int(kpath_frame.stem[-4:]) ipath_landmk_frame = ipath_landmk.joinpath(f"frame-{frame_id:04d}.txt") ldmk = np.loadtxt(ipath_landmk_frame) # shape = (68, 2) ldmk_norm, pos_mean, side = landmark_normalize(ldmk) v_ldmk[k, :, :] = ldmk v_ldmk_norm[k, :, :] = ldmk_norm v_ldmk_auxs[k, :] = np.array([pos_mean[0], pos_mean[1], side]) motion_test.append(v_ldmk_norm) # (29, 68, 2) speech_test.append(mfcc) # (29, 27) # count video numbers nvideo += 1 motion_test = np.array(motion_test) # (N, 29, 68, 2) speech_test = np.array(speech_test) # (N, 29, 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 train data motion({motion_test.shape}), speech({speech_test.shape}) -> {path_save}")