import os import sys import json import numpy as np from pathlib2 import Path 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 total_pose = [] total_shoulder = [] for ipath_data in v_dir_videos: ivideo_name = ipath_data.stem ipath_info = ipath_data.joinpath("video/info.json") ipath_feat_pose = ipath_data.joinpath("audio/openpose_upbody_trim.npy") ibeg, iend, _, W, H, info= load_video_info(ipath_info, verbose=False) if ibeg is None: continue if not ipath_feat_pose.exists(): continue idata_pose = np.load(ipath_feat_pose) # (nframes, 8, 2) imean_pose = np.mean(idata_pose, axis=0) # (8, 2) imean_pose = imean_pose / np.array([W, H]) imean_shoulder = np.sqrt((imean_pose[3,0]-imean_pose[2,0])**2 + (imean_pose[3,1]-imean_pose[2,1])**2) print(f"data pose shape = {idata_pose.shape}, mean shape={imean_pose.shape}, root={imean_pose[1]}, size shoulder = {imean_shoulder}--> {ivideo_name}") total_pose.append(imean_pose) total_shoulder.append(imean_shoulder) # update info info["mean_shoulder_size"] =imean_shoulder info["mean_pose"] = list(imean_pose.reshape(-1)) with open(str(ipath_info), 'w+') as f: json.dump(info, f) print(f"update video info in -> {ipath_info}") total_pose = np.array(total_pose) mean_pose = np.mean(total_pose, axis=0) mean_shoulder = np.mean(total_shoulder) print(f"total_pose shape = {total_pose.shape}, mean pose ={mean_pose.shape}, mean[1,] = {mean_pose[1,:]}, mean shoulder size = {mean_shoulder}") # save mean np.save(path_save.joinpath("mean_pose.npy"), mean_pose) # (8,2) np.save(path_save.joinpath("mean_shoulder.npy"), mean_shoulder)