161 lines
7.0 KiB
Python
161 lines
7.0 KiB
Python
import json
|
|
import numpy as np
|
|
from pathlib2 import Path
|
|
|
|
def interpolate_features(features, input_rate, output_rate, output_len=None):
|
|
num_features = features.shape[1]
|
|
input_len = features.shape[0]
|
|
seq_len = input_len / float(input_rate)
|
|
|
|
if output_len is None:
|
|
output_len = int(seq_len * output_rate)
|
|
|
|
input_timestamps = np.arange(input_len) / float(input_rate)
|
|
output_timestamps = np.arange(output_len) / float(output_rate)
|
|
output_features = np.zeros((output_len, num_features))
|
|
for feat in range(num_features):
|
|
output_features[:, feat] = np.interp(output_timestamps,
|
|
input_timestamps,
|
|
features[:, feat])
|
|
return output_features
|
|
|
|
|
|
def slice_data(data, window_size, overlap):
|
|
|
|
nframes = data.shape[0]
|
|
overlap_frames = (int)(overlap*window_size)
|
|
|
|
n_sequences = (nframes-overlap_frames)//(window_size-overlap_frames)
|
|
|
|
if data.ndim == 2:
|
|
sliced = np.zeros((n_sequences, window_size, data.shape[1])).astype(np.float32)
|
|
else:
|
|
sliced = np.zeros((n_sequences, window_size, data.shape[1], data.shape[2])).astype(np.float32)
|
|
|
|
if n_sequences>0:
|
|
# extract sequences from the data
|
|
for i in range(0, n_sequences):
|
|
frameIdx = (window_size-overlap_frames) * i
|
|
sliced[i] = data[frameIdx:frameIdx+window_size].copy()
|
|
else:
|
|
print("WARNING: data too small for window")
|
|
|
|
return sliced
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
path_data = Path('/yrfs/lsen/DATA/Obama/processing') # '../data/trinity/source'
|
|
path_save = Path('data/obama-dsv1-deca-5-10-08-mini8k') # processing
|
|
path_save.mkdir(parents=True, exist_ok=True)
|
|
|
|
v_dir_videos = sorted(path_data.glob("*/audio/feat_DECA_exp_trim.npy"))
|
|
print(f"Have {len(v_dir_videos)} dir files")
|
|
|
|
v_dir_videos = [ipath.parent.parent for ipath in v_dir_videos]
|
|
vids = [ipath.stem for ipath in v_dir_videos]
|
|
|
|
nv_total = len(v_dir_videos)
|
|
nv_valid, nv_test = 1, 1
|
|
nv_train = nv_total - (nv_valid + nv_test)
|
|
|
|
vid_test = vids[:nv_test]
|
|
vid_valid = vids[nv_test:nv_test+nv_valid]
|
|
vid_train = vids[nv_test+nv_valid:]
|
|
print("vid_test = ", vid_test)
|
|
|
|
# set windows
|
|
nwin_train_secs = 5 # 6
|
|
nwin_test_secs = 10 # 20
|
|
window_overlap = 0.8
|
|
fps = 30
|
|
|
|
nwin_train = nwin_train_secs * fps # 3 * 30 = 90
|
|
nwin_test = nwin_test_secs * fps # 5 * 30 = 150
|
|
|
|
# for each video
|
|
motion_train, motion_valid, motion_test = [], [], []
|
|
speech_train, speech_valid, speech_test = [], [], []
|
|
for i, ipath_data in enumerate(v_dir_videos):
|
|
vid = ipath_data.stem
|
|
|
|
if i >= 50: continue
|
|
|
|
if i % 50 == 0:
|
|
print(f"\n processing the {i}-th video {vid} ... ")
|
|
|
|
ipath_info = ipath_data.joinpath("video/info.json")
|
|
ipath_feat_exp = ipath_data.joinpath("audio/feat_DECA_exp_trim.npy")
|
|
ipath_feat_pos = ipath_data.joinpath("audio/feat_DECA_pos_trim.npy")
|
|
# ipath_feat_speech = ipath_data.joinpath("audio/feat_deepspeech010_trim.npy")
|
|
ipath_feat_speech = ipath_data.joinpath("audio/feat_deepspeech_v1_trim.npy")
|
|
|
|
|
|
if not ipath_feat_speech.exists():
|
|
continue
|
|
|
|
# load video info
|
|
with open(str(ipath_info), "r") as f:
|
|
info = json.load(f)
|
|
width = info["width"][0] # 1280 or 1920
|
|
height = info["height"][0] # 720 or 1080
|
|
fps = info["fps"]
|
|
print("width=",width, "height=",height, "fps=",info["fps"], "-->", ipath_data)
|
|
|
|
# laod motion & speech data
|
|
data_exp = np.load(ipath_feat_exp) # (nframe, 50)
|
|
data_pos = np.load(ipath_feat_pos) # (nframe, 6)
|
|
pos_head = data_pos[:, :3] # (nframe, 3)
|
|
pos_lips = data_pos[:, 3:] # (nframe, 3)
|
|
|
|
data_motion = np.concatenate([data_exp, pos_lips], axis=1) # (nframe, 53) --> fps=30
|
|
data_speech = np.load(ipath_feat_speech) # (nframe*2, 16, 29) --> fps=60
|
|
print(f"motion = {data_motion.shape}, speech = {data_speech.shape}")
|
|
|
|
# # downsampling data_speech, 60fps --> fps
|
|
# (nseq, nwind, nfeat) = data_speech.shape # (nframe*2, 16, 29)
|
|
# data_speech = data_speech.reshape(nseq, -1) # (nframe*2, 16x29)
|
|
# data_speech = interpolate_features(data_speech, 60, fps, output_len=data_motion.shape[0])
|
|
# data_speech = data_speech.reshape(-1, nwind, nfeat)
|
|
# print(f"after motion = {data_motion.shape}, speech = {data_speech.shape}")
|
|
|
|
if vid in vid_train:
|
|
motion_sliced = slice_data(data_motion, window_size=nwin_train, overlap=window_overlap)
|
|
speech_sliced = slice_data(data_speech, window_size=nwin_train, overlap=window_overlap)
|
|
motion_train.append(motion_sliced)
|
|
speech_train.append(speech_sliced)
|
|
|
|
if vid in vid_valid:
|
|
motion_sliced = slice_data(data_motion, window_size=nwin_train, overlap=window_overlap)
|
|
speech_sliced = slice_data(data_speech, window_size=nwin_train, overlap=window_overlap)
|
|
motion_valid.append(motion_sliced)
|
|
speech_valid.append(speech_sliced)
|
|
|
|
if vid in vid_test:
|
|
motion_sliced = slice_data(data_motion, window_size=nwin_test, overlap=0)
|
|
speech_sliced = slice_data(data_speech, window_size=nwin_test, overlap=0)
|
|
motion_test.append(motion_sliced)
|
|
speech_test.append(speech_sliced)
|
|
|
|
print(f"sliced motion = {motion_sliced.shape}, speech = {speech_sliced.shape}, minmax=({np.min(data_speech)},{np.max(data_speech)}) \n")
|
|
|
|
# save
|
|
motion_train = np.concatenate(motion_train, axis=0) # (nframe, 180, 136)
|
|
speech_train = np.concatenate(speech_train, axis=0) # (nframe, 180, 27)
|
|
np.savez(path_save.joinpath("motion_train.npz"), data=motion_train)
|
|
np.savez(path_save.joinpath("speech_train.npz"), data=speech_train)
|
|
print(f"save train data motion({motion_train.shape}), speech({speech_train.shape}) -> {path_save}")
|
|
|
|
motion_valid = np.concatenate(motion_valid, axis=0) # (350, 180, 136)
|
|
speech_valid = np.concatenate(speech_valid, axis=0) # (350, 180, 27)
|
|
np.savez(path_save.joinpath("motion_valid.npz"), data=motion_valid)
|
|
np.savez(path_save.joinpath("speech_valid.npz"), data=speech_valid)
|
|
print(f"save valid data motion({motion_valid.shape}), speech({speech_valid.shape}) -> {path_save}")
|
|
|
|
motion_test = np.concatenate(motion_test, axis=0) # (41, 600, 136)
|
|
speech_test = np.concatenate(speech_test, axis=0) # (41, 600, 27)
|
|
np.savez(path_save.joinpath("motion_test.npz"), data=motion_test)
|
|
np.savez(path_save.joinpath("speech_test.npz"), data=speech_test)
|
|
print(f"save test data motion({motion_test.shape}), speech({speech_test.shape}) -> {path_save}\n")
|
|
|