import os import sys import json # import moviepy.editor as mpy import numpy as np from pathlib2 import Path import sys sys.path.insert(0, "/mnt/data2/CODE/TalkingGlow") from common.utils import smooth from common.logger import Logger from common.landmark import landmark_to_images def load_video_info(path_info, verbose=True): with open(str(ipath_info), "r") as f: info = json.load(f) W = info["width"][0] # 1280 or 1920 H = info["height"][0] # 720 or 1080 if len(info["active_frames"]) == 2: ibeg = info["active_frames"][0] iend = info["active_frames"][1] nframe_trim = iend - ibeg if verbose: print(f"frame trim = {nframe_trim}, info['active_frames'] = {info['active_frames']}") return ibeg, iend, nframe_trim, W, H, info else: if verbose: print(f"ERROR: info['active_frames'] = {info['active_frames']}") return None, None, None, W, H, info if __name__ == "__main__": path_data = Path('/mnt/data2/DATA/Obama/processing') # '../data/trinity/source' path_save = Path('data/obama-no-scale-alignment') # processing path_save.mkdir(parents=True, exist_ok=True) v_dir_videos = sorted(path_data.glob("*")) print(f"Have {len(v_dir_videos)} dir files") # get mean pose mean_pose = np.load(path_save.joinpath("mean_pose.npy")) # (8,2) mean_shoulder = np.load(path_save.joinpath("mean_shoulder.npy")) # (1,) print(f"mean pose = {mean_pose.shape}, root pose = {mean_pose[1]}, mean shoulder = {mean_shoulder}") # for each video ncounter = 0 for ipath_data in v_dir_videos: ivideo_name = ipath_data.stem # if ivideo_name != "Weekly-Address-Ensuring-Equal-Pay-for-Equal-Work": # continue ipath_info = ipath_data.joinpath("video/info.json") ipath_feat_ldmk = ipath_data.joinpath("audio/feat_ldmk_trim.npy") ipath_feat_pose = ipath_data.joinpath("audio/openpose_upbody_trim.npy") ipath_feat_ldmk_align = ipath_data.joinpath("audio/feat_ldmk_align_trim.npy") ipath_feat_pose_align = ipath_data.joinpath("audio/openpose_upbody_align_trim.npy") if not ipath_feat_ldmk.exists() or not ipath_feat_pose.exists(): continue elif ipath_feat_ldmk_align.exists() and ipath_feat_pose_align.exists(): ncounter += 1 continue else: print(f"\n processing the {ncounter}-th videp, {ivideo_name} ... ") ncounter += 1 # load video info ibeg, iend, _, W, H, info = load_video_info(ipath_info) if ibeg is None: continue # load landmark feat data_ldmk = np.load(ipath_feat_ldmk) # (nframe, 68, 2) data_pose = np.load(ipath_feat_pose) # (nframe, 8, 2) data_ldmk = data_ldmk / np.array([[[W, H]]]) # scale landmark data_pose = data_pose / np.array([[[W, H]]]) # scale pose print(f"data landmark shape={data_ldmk.shape}, pose shape = {data_pose.shape}") assert data_ldmk.shape[0] == data_pose.shape[0], "nframes length should be same" ################################## # STEP 1: get sacle and smooth scale v_scale = [] for j in range(data_ldmk.shape[0]): jdata_pose = data_pose[j, :, :] # (8, 2) jshoulder = np.sqrt((jdata_pose[3,0]-jdata_pose[2,0])**2 + (jdata_pose[3,1]-jdata_pose[2,1])**2) scale = mean_shoulder / jshoulder v_scale.append(scale) # if j % 100 == 0: # print(f"\nthe {ibeg + j}-th frame!") # print(f"pose root = {data_pose[j, 1, :]}, mean pose root = {mean_pose[1]}") # print(f"should size = {jshoulder:.4f}, mean shoulder = {mean_shoulder:.4f}, scale = {scale:0.4f}") v_scale = np.array(v_scale) v_scale_smooth = smooth(v_scale, window_len=60, window='hanning') print(f"scale shape= {v_scale.shape}, smooth = {v_scale_smooth.shape}") # for v, s in zip(v_scale, v_scale_smooth): # print(f"orignal {v:.4f}, smooth={s:.4f}, mean={mean_shoulder:.4f}") ################################## # STEP 2: sacle landmark and pose data_ldmk_scale = [] data_pose_scale = [] for j in range(data_ldmk.shape[0]): jdata_ldmk_scale = data_ldmk[j, :, :] * v_scale_smooth[j] jdata_pose_scale = data_pose[j, :, :] * v_scale_smooth[j] data_ldmk_scale.append(jdata_ldmk_scale) data_pose_scale.append(jdata_pose_scale) # if j % 100 == 0: # jshoulder_scale = np.sqrt((jdata_pose_scale[3,0]-jdata_pose_scale[2,0])**2 + (jdata_pose_scale[3,1]-jdata_pose_scale[2,1])**2) # print(f"\nthe {ibeg + j}-th frame!") # print(f"pose root = {data_pose[j, 1, :]}, scale root={jdata_pose_scale[1]}, mean pose root = {mean_pose[1]}") # print(f"shoulder size = {jshoulder:.4f}, scale ={jshoulder_scale:.4f} mean shoulder = {mean_shoulder:.4f}, scale = {v_scale[j]:.4f}, smooth scale={v_scale_smooth[j]:.4f}") data_ldmk_scale = np.array(data_ldmk_scale) data_pose_scale = np.array(data_pose_scale) ################################## # STEP 3: alignment to mean-pose data_ldmk_align = [] data_pose_align = [] for j in range(data_ldmk.shape[0]): jpose_root = data_pose_scale[j, 1, :] # (2, ) jmean_root = mean_pose[1, :] # (2, ) jdeta = jmean_root - jpose_root jdata_ldmk_align = data_ldmk_scale[j, :, :] + jdeta.reshape(1, 2) jdata_pose_align = data_pose_scale[j, :, :] + jdeta.reshape(1, 2) data_ldmk_align.append(jdata_ldmk_align) data_pose_align.append(jdata_pose_align) if j % 1000 == 0: jshoulder = np.sqrt((data_pose[j,3,0]-data_pose[j,2,0])**2 + (data_pose[j,3,1]-data_pose[j,2,1])**2) jshoulder_scale = np.sqrt((data_pose_scale[j,3,0]-data_pose_scale[j,2,0])**2 + (data_pose_scale[j,3,1]-data_pose_scale[j,2,1])**2) jshoulder_align = np.sqrt((jdata_pose_align[3,0]-jdata_pose_align[2,0])**2 + (jdata_pose_align[3,1]-jdata_pose_align[2,1])**2) print(f"\nthe {ibeg + j}-th frame!") print(f"orignal root = {data_pose[j, 1, :]}, scale={jdata_pose_scale[1]}, align = {jdata_pose_align[1]}, mean = {mean_pose[1]}") print(f"orignal shoulder = {jshoulder:.4f}, scale ={jshoulder_scale:.4f} mean shoulder = {mean_shoulder:.4f}") print(f"orignal scale = {v_scale[j]:.4f}, smooth scale={v_scale_smooth[j]:.4f}") data_ldmk_align = np.array(data_ldmk_align) data_pose_align = np.array(data_pose_align) ################################## # STEP 4: save & visualization np.save(ipath_feat_ldmk_align, data_ldmk_align) np.save(ipath_feat_pose_align, data_pose_align) print(f"align ldmk shape={data_ldmk_align.shape}, pose shape = {data_pose_align.shape}--> save to {ipath_feat_ldmk_align}") # visualization # images_gt = landmark_to_images(data_ldmk[::10, ...], w=128*5, h=72*5, dpi=100, norm=True, prex="GT") # (N, W, H, 3) # images_pt = landmark_to_images(data_ldmk_align[::10, ...], w=128*5, h=72*5, dpi=100, norm=True, prex="Alignment") # (N, W, H, 3) # print("convert to video") # data_motion_mix = np.concatenate([images_gt, images_pt], axis=1) # (N, W, 2H, 3) # data_motion_mix = data_motion_mix.astype(np.uint8) # print("data shape = ", data_motion_mix.shape) # fps = 10 # N = data_motion_mix.shape[0] # animation = mpy.VideoClip(lambda t: data_motion_mix[int(fps * t + 0.5),...], duration= N // fps) # animation.write_videofile("my_animation.mp4", fps=fps)