78 lines
2.8 KiB
Python
78 lines
2.8 KiB
Python
import os
|
|
import sys
|
|
import json
|
|
import librosa
|
|
import numpy as np
|
|
from pathlib2 import Path
|
|
import torch
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
path_data = Path('/mnt/data2/DATA/Obama/processing/processing') # '../data/trinity/source'
|
|
|
|
v_dir_videos = sorted(path_data.glob("*"))
|
|
print(f"Have {len(v_dir_videos)} dir files")
|
|
|
|
# set windows
|
|
nwin_train_secs = 2 # 6, 2
|
|
overlap = 0.8
|
|
fps = 30
|
|
|
|
# for each video
|
|
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_ladmk = ipath_data.joinpath("audio/feat_ldmk_trim_norm.npy") #
|
|
|
|
ipath_save = ipath_data.joinpath("audio/feat_ldmk_trim_norm_split_2s.npy") # feat_mel_trim_split
|
|
|
|
if not ipath_ladmk.exists():
|
|
print(f"ERROR: NOT Exist {ipath_ladmk}")
|
|
continue
|
|
|
|
# load video info
|
|
with open(str(ipath_infors), "r") as f:
|
|
info = json.load(f)
|
|
width = info["width"][0] # 1280 or 1920
|
|
height = info["height"][0] # 720 or 1080
|
|
print("\nwidth=",width, "height=",height, "fps=",info["fps"], "-->", ipath_data)
|
|
|
|
# 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
|
|
data_ladmk = np.load(ipath_ladmk).reshape(-1, 136) # (nframe, 136)
|
|
assert data_ladmk.shape[0] == nframe_trim, f"the length of landmark {data_ladmk.shape[0]} != nframe_trim {nframe_trim}"
|
|
|
|
|
|
# split frames
|
|
nsize_split = int( nwin_train_secs * fps )
|
|
nstep_split = int( nwin_train_secs * fps * (1-overlap) )
|
|
nsplit = (nframe_trim - nsize_split) // nstep_split + 1
|
|
print(f"frame ==> nsize = {nsize_split}, nstep = {nstep_split}, len = {nsplit}")
|
|
|
|
# split landmark
|
|
data_ladmk = torch.from_numpy(data_ladmk).float()
|
|
data_ladmk2 = data_ladmk.unfold(dimension=0, size=nsize_split, step=nstep_split) # (nframe_trim//30, 136, 30)
|
|
data_ladmk2 = data_ladmk2.permute(0, 2, 1) # (nsplits, nsteps, 136)
|
|
print(f"data landmark unfold = {data_ladmk2.shape}")
|
|
# print(f"check data landmark[32, ::50] = {data_ladmk[62, ::50]}, \n data_ladmk2[1, ::50, 2] = {data_ladmk2[2, ::50, 2]}")
|
|
|
|
# save
|
|
np.save(ipath_save, data_ladmk2) # (nsplit, 60, 136)
|
|
print(f"data_ladmk2 = {data_ladmk2.shape}, save to --> {ipath_save}")
|
|
# break
|
|
|
|
|