Files
audio2motion/scripts/Obama/run-21-landmark-features-norm.py
T
2022-01-21 17:42:41 +08:00

93 lines
2.8 KiB
Python

import os
import sys
import json
import numpy as np
from pathlib2 import Path
import sys
sys.path.insert(0, "/mnt/data2/CODE/Audio2Expression")
from common.landmark import landmark_normalize
# from common.animation import save_animation, save_animation_with_gt
if __name__ == "__main__":
path_data = Path('/mnt/data2/DATA/Obama/processing') # processing
v_dir_videos = sorted(path_data.glob("*"))
print(f"Have {len(v_dir_videos)} dir files")
# for each video
for ipath_data in v_dir_videos:
ivideo_name = ipath_data.stem
if ivideo_name not in ["Celebrating-Independence-Day"]:
continue
print(f"\n processing {ivideo_name} ... ")
ipath_info = ipath_data.joinpath("video/info.json")
ipath_ldmk = ipath_data.joinpath("landmk-PFLD")
ipath_save_feat = ipath_data.joinpath("audio/feat_ldmk_trim_norm.npy")
# if ipath_save_feat.exists():
# continue
# load video info
with open(str(ipath_info), "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']}")
else:
print(f"ERROR: info['active_frames'] = {info['active_frames']}")
continue
# load landmark
feat_ldmk = []
prev_ldmk = None
nContinue = 0
for j in range(ibeg, iend):
jpath_ldmk = ipath_ldmk.joinpath(f"frame--{j:05d}.txt")
if not jpath_ldmk.exists():
print("ERROR: Missing the landmark of frame --> ", jpath_ldmk)
if nContinue <= 2:
data_ldmk = prev_ldmk
nContinue += 1
else:
nContinue += 1
break
else:
data_ldmk = np.loadtxt(jpath_ldmk).astype(np.float)
data_ldmk, _, _ = landmark_normalize(data_ldmk) # normalize
prev_ldmk = data_ldmk
nContinue = 0
feat_ldmk.append(data_ldmk)
# if j % 100 == 0:
# print(f"the {j:05d}-th landmark")
# save
feat_ldmk = np.array(feat_ldmk) # (N, 68, 2)
if nContinue <= 2 and feat_ldmk.shape[0] == (iend-ibeg):
np.save(ipath_save_feat, feat_ldmk)
print(f"feat shape = {feat_ldmk.shape}, save to -> {ipath_save_feat}") # (nframe, 68, 2)
# save to animation
else:
print(f"ERROR: some landmark missing or length not same, length = {feat_ldmk.shape}/{iend-ibeg}")