85 lines
2.9 KiB
Python
85 lines
2.9 KiB
Python
import json
|
|
import numpy as np
|
|
from pathlib import Path
|
|
|
|
if __name__ == "__main__":
|
|
|
|
path_data = Path('/mfs/lsen/DATA/Obama/processing') # '../data/trinity/source'
|
|
|
|
v_dir_videos = sorted(path_data.glob("*"), reverse=True)
|
|
print(f"Have {len(v_dir_videos)} dir files")
|
|
|
|
# for each video
|
|
duration_frame, duration_coeff = 0, 0
|
|
for ipath_data in v_dir_videos:
|
|
ivideo_name = ipath_data.stem
|
|
|
|
print(f"\n processing {ivideo_name} ... ")
|
|
ipath_infors = ipath_data.joinpath("video/info.json")
|
|
ipath_coeff = ipath_data.joinpath("DECA_coeff")
|
|
ipath_frame = ipath_data.joinpath("frames")
|
|
ipath_save_pos = ipath_data.joinpath("audio/feat_DECA_pos_cam_trim.npy")
|
|
|
|
if ipath_save_pos.exists():
|
|
print(f"ipath_save_exp have already save > {ipath_save_pos}")
|
|
continue
|
|
|
|
if not ipath_infors.exists():
|
|
continue
|
|
|
|
# load video info
|
|
with open(str(ipath_infors), "r") as f:
|
|
info = json.load(f)
|
|
|
|
fps = info["fps"]
|
|
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']}, duration ={nframe_trim / fps // 60}")
|
|
else:
|
|
print(f"ERROR: info['active_frames'] = {info['active_frames']}")
|
|
continue
|
|
|
|
# check
|
|
# vframe_frame = list(ipath_frame.glob("*.jpg"))
|
|
# vframe_coeff = list(ipath_coeff.glob("*.npy"))
|
|
# print(f"vframe_frame len = {len(vframe_frame)}, vframe_coeff={len(vframe_coeff)}")
|
|
|
|
# if len(vframe_frame) != nframe_trim:
|
|
# print(f"\n ERROR: nframe_real ={len(vframe_frame)} / nframe_info ={nframe_trim}")
|
|
# continue
|
|
|
|
|
|
feat_pos = []
|
|
feat_cam = []
|
|
for j in range(ibeg, iend):
|
|
jpath_coeff = ipath_coeff.joinpath(f"frame-{j:05d}.json.npy")
|
|
|
|
if j%1000 == 0: print(f"processing the {j}-th frame ... ")
|
|
|
|
if not jpath_coeff.exists():
|
|
print(f"\n ERROR: Path NOT FOUND! -> {jpath_coeff}")
|
|
break
|
|
|
|
data_coeff = np.load(jpath_coeff, allow_pickle=True).item()
|
|
feat_pos.append(data_coeff["pose"])
|
|
feat_cam.append(data_coeff["cam"])
|
|
|
|
# save
|
|
feat = np.concatenate([np.array(feat_pos), np.array(feat_cam)], axis=1) # (nframe_trim, 6+3)
|
|
|
|
# pad one
|
|
if nframe_trim == feat.shape[0]:
|
|
np.save(ipath_save_pos, feat)
|
|
print(f"SAVE OK! pos = {feat.shape}, nframe={nframe_trim}\n save to -> {ipath_save_pos}") # (nframe, 9)
|
|
|
|
duration_coeff += nframe_trim / fps / 60
|
|
print(f"add duration_coeff={duration_coeff} min")
|
|
|
|
else:
|
|
print("ERROR, skip! ")
|
|
|
|
|
|
|