Files
audio-driven-face-animation/scripts/Actor7Data/run-22-concat-3dmm.py
T
2022-05-26 10:02:37 +08:00

104 lines
3.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import os
import sys
import json
import librosa
import numpy as np
from pathlib import Path
from scipy.signal import savgol_filter
import os, sys
sys.path.insert(0, os.getcwd())
from common.logger import Logger
if __name__ == "__main__":
actor = "Actor6"
sub_folder = "processing2" # smile, processing, processing2
# logger
logger = Logger(f"./data/{actor}", postfix=f"concat-coeff-{sub_folder}")
path_data = Path(f"/ssdata1/lsen/DATA/{actor}/{sub_folder}")
v_dir_videos = sorted(path_data.glob("*"), reverse=False)
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_coeff = ipath_data.joinpath("deca_coeff_512")
ipath_frame = ipath_data.joinpath("frames")
ipath_save_coeff = ipath_data.joinpath("audio/feat_deca_coeff_512p.npy")
# if ipath_save_coeff.exists():
# print(f"ipath_save_exp have already save > {ipath_save_coeff}")
# 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"]
nframes = info["nframes"]
print(f"info = {info}, duration ={nframes / fps / 60}")
# 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)}")
duration_frame += len(vframe_frame) / 50 / 60
duration_coeff += len(vframe_coeff) / 50 / 60
print(f"add duration_frame = {duration_frame} min, duration_coeff={duration_coeff} min")
if len(vframe_frame) != nframes:
print(f"\n ERROR: nframe_real ={len(vframe_frame)} / nframe_info ={nframes}")
continue
if len(vframe_coeff)+1 < nframes:
print(f"\n ERROR: nframe_coeff ={len(vframe_coeff)} / nframe_info ={nframes}")
continue
feat_coeff = []
params = ['shape', 'tex', 'exp', 'pose', 'cam', 'light']
for j in range(len(vframe_frame)):
jpath_coeff = ipath_coeff.joinpath(f"frame-{j:06d}.json.npy")
if not jpath_coeff.exists():
print(f"\n ERROR: Path NOT FOUND! -> {jpath_coeff}")
break
codedict = np.load(jpath_coeff, allow_pickle=True).item()
# dim of shape = 100, tex = 50, exp = 50, pose = 6, cam = 3, light = 27
coeff = []
for ikey in params:
coeff += list(codedict[ikey].reshape(-1))
assert len(coeff) == 236, f"coeff len = {len(coeff)} should be 236 !"
feat_coeff.append(np.array(coeff))
# concate
feat_coeff = np.array(feat_coeff) # (N, 236)
# smooth it
feat_coeff = savgol_filter(feat_coeff, window_length=5, polyorder=3, axis=0, mode='nearest')
if nframes == feat_coeff.shape[0]:
np.save(ipath_save_coeff, feat_coeff)
print(f"SAVE OK! feat shape = {feat_coeff.shape}, nframe={nframes}\n save to -> {ipath_save_coeff}") # (nframe, 68, 2)
else:
print("ERROR, skip! ")