import os import sys import json import shutil import numpy as np from pathlib2 import Path if __name__ == "__main__": path_data = Path('/mnt/data2/DATA/Obama/processing') # '../data/trinity/source' v_dir_videos = sorted(path_data.glob("*")) print(f"Have {len(v_dir_videos)} dir files") idx_pos = [0, 1, 2, 5, 15, 16, 17, 18] # for each video counter = 0 for ipath_data in v_dir_videos: ivideo_name = ipath_data.stem # if ivideo_name != "Weekly-Address-Continuing-to-Grow-the-Economy-in-the-New-Year": # continue print(f"\n processing {ivideo_name} ... ") ipath_info = ipath_data.joinpath("video/info.json") ipath_pose = ipath_data.joinpath("openpose") ipath_feat_body = ipath_data.joinpath("audio/openpose_upbody_trim.npy") ipath_feat_face = ipath_data.joinpath("audio/openpose_face_trim.npy") if ipath_feat_body.exists() and ipath_feat_face.exists(): counter += 1 # os.remove(str(ipath_feat_body)) # os.remove(str(ipath_feat_face)) continue # load video info nContinue = 0 with open(str(ipath_info), "r") as f: info = json.load(f) if len(info["active_frames"]) == 2: ibeg = info["active_frames"][0] iend = info["active_frames"][1] nframe_trim = iend - ibeg print(f"frame trim = {nframe_trim}, info['active_frames'] = {info['active_frames']}") else: print(f"ERROR: info['active_frames'] = {info['active_frames']}") continue # load pose feat_body = [] feat_face = [] prev_feat_body = None prev_feat_face = None nContinue = 0 for j in range(ibeg, iend): # print(f"{j}-th frame, of {ibeg}, {iend}") # if j > ibeg + 10: break jpath_pose = ipath_pose.joinpath(f"video_orignal_{j:012d}_keypoints.json") if not jpath_pose.exists(): print("ERROR: Missing the landmark of frame --> ", jpath_pose) if prev_feat_body is None: print("ERRO: The first should be a person") nContinue = 100 break # give two change if nContinue <= 2: ipose_face = prev_feat_face ipose_body = prev_feat_body nContinue += 1 else: nContinue = 100 break else: # load json file with open(str(jpath_pose)) as f: ipose = json.load(f) # read pose and face npid = len(ipose['people']) if npid == 0: print(f"ERROR: detected NO person! of the {j}-th frames") if prev_feat_body is None: print("ERRO: The first should be a person") nContinue = 100 break # give two change if nContinue <= 2: ipose_face = prev_feat_face ipose_body = prev_feat_body nContinue += 1 else: nContinue = 100 break elif npid > 1: print(f"\nERROR: detected MULTI persons, should select one!, the {j}-th in ({ibeg},{iend}), ") # print(ipose) # select the detetected which closest to prev_feat_face nContinue = 0 diff_center = np.zeros((npid,)) for i in range(npid): tmp_prev = 0 if prev_feat_body is None else prev_feat_body ipose_body = np.array(ipose['people'][i]['pose_keypoints_2d']).reshape(-1, 3)[idx_pos, :2] diff_center[i] = np.mean(np.abs(ipose_body - tmp_prev)) idx = np.argmin(diff_center) if prev_feat_body is not None else np.argmax(diff_center) ipose_face = np.array(ipose['people'][idx]['face_keypoints_2d']).reshape(-1, 3)[:, :2] ipose_body = np.array(ipose['people'][idx]['pose_keypoints_2d']).reshape(-1, 3)[idx_pos, :2] print(f"\t selected the {idx}-th pose") else: nContinue = 0 ipose_face = np.array(ipose['people'][0]['face_keypoints_2d']).reshape(-1, 3)[:, :2] ipose_body = np.array(ipose['people'][0]['pose_keypoints_2d']).reshape(-1, 3)[idx_pos, :2] if prev_feat_body is not None: # check pose corrected # print(f"face shpe = {ipose_face.shape}, body = {ipose_body.shape}") for k in range(ipose_body.shape[0]): if ipose_body[k][0] == 0 and ipose_body[k][1] == 0: ipose_body[k, :] = prev_feat_body[k, :] if prev_feat_face is not None: # check landmark corrected for k in range(ipose_face.shape[0]): if ipose_face[k][0] == 0 and ipose_face[k][1] == 0: ipose_face[k, :] = prev_feat_face[k, :] # add to feat feat_body.append(ipose_body) feat_face.append(ipose_face) prev_feat_body = ipose_body prev_feat_face = ipose_face # break # visualize # save if nContinue <= 2: # save feat_body = np.array(feat_body) feat_face = np.array(feat_face) print(f"save... feat_body shape = {feat_body.shape}, feat_face shape = {feat_face.shape}") np.save(ipath_feat_body, feat_body) np.save(ipath_feat_face, feat_face) counter += 1 else: print("nContinue > 2 some landmark broken or cut-off") # (nframe, 68, 2) # break print(f"OVER: processing {counter} videos")