Files
audio2motion/scripts/GRID/run-40-generate-test-data.py
T
2022-01-21 17:42:41 +08:00

114 lines
4.4 KiB
Python

"""
generated train annotation data
"""
import os
import pickle
import numpy as np
from pathlib import Path
from collections import OrderedDict
from common.landmark import landmark_normalize
pid_for_train = ["s1", "s2", "s3", "s4", "s5", "s6", "s7", "s8", "s9", "s10",
"s11", "s12", "s13", "s14", "s15", "s16", "s17", "s18", "s19", "s20",
"s21", "s22", "s23", "s24", "s25", "s26", "s27", "s28", "s29", "s30"]
pid_for_test = ["s31", "s32", "s33", "s34"]
if __name__ == "__main__":
path_data = Path("/mnt/data/DATA/GRID/processing")
path_anno = Path("data/GRID/anno_test_half.pickle")
annodb = OrderedDict()
# if not existing
vpath_ids = sorted(path_data.glob("*"))
print(f"Have {len(vpath_ids)} person folders --> {vpath_ids[0]}")
# iter
for i, ipath_id in enumerate(vpath_ids):
pid = ipath_id.stem # person id
# select train pid
if pid not in pid_for_test:
continue
# video
vpath_videos = sorted(ipath_id.glob("*/video/video_orignal.mp4"))
print(f"\nid= {pid}, Have {len(vpath_videos)} video files, --> {vpath_videos[0]}")
# for each video
list_dict = []
for iv, ipath_video in enumerate(vpath_videos):
vid = ipath_video.parent.parent.stem # video id
if iv % 50 == 0:
print(f"--> processing the {iv}-th video --> {ipath_video}")
ipath_data = ipath_video.parent.parent
ipath_frame = ipath_data.joinpath("frames")
ipath_ladmk = ipath_data.joinpath("landmk")
# ipath_coeff = ipath_data.joinpath("coeff")
ipath_mfccs = ipath_data.joinpath("audio/mfcc_feat.npy")
# STEP1: check if missing data
vpath_frame = sorted(ipath_frame.glob("*.jpg"), key=lambda ipath: int(ipath.stem[-4:]))
# vpath_coeff = sorted(ipath_coeff.glob("*.txt"), key=lambda ipath: int(ipath.stem[-4:]))
vpath_ladmk = sorted(ipath_ladmk.glob("*.txt"), key=lambda ipath: int(ipath.stem[-4:]))
nframes = len(vpath_frame)
# assert
if (nframes < 25) or (nframes != len(vpath_ladmk)) : # or (nframes != len(vpath_coeff)):
print(f"DEBUG: nframes={nframes}, nladmk={len(vpath_ladmk)}, --> {ipath_video}") # ncoeff={len(vpath_coeff)}
continue
# load mfcc
list_mfcc = np.load(ipath_mfccs) # nframes x 28 x 12
# # load 3DMM & landmarks
# list_idn = np.zeros((nframes, 100), dtype=float) # coeff idn, nframes x 100
# list_exp = np.zeros((nframes, 79), dtype=float) # coeff exp, nframes x 79
v_ldmk = np.zeros((nframes, 68, 2), dtype=int)
v_ldmk_norm = np.zeros((nframes, 68, 2), dtype=float) # normalize
v_ldmk_auxs = np.zeros((nframes, 3), dtype=float) # [x_mean, y_mean, side]
for k, kpath_frame in enumerate(vpath_frame):
fid = int(kpath_frame.stem[-4:]) # frame id
# # 3dmm
# kpath_3dmm = ipath_coeff.joinpath(f"frame-{fid:04d}.txt")
# coeff = np.loadtxt(kpath_3dmm, dtype=float) # 316: idn-100, exp-79, tex-100, euler-3, trans-3, illum-27, camr-4
# list_idn[k, :] = coeff[:100] # 100 identity coeff
# list_exp[k, :] = coeff[100:100+79] # 79 expression coeff
# ldmk
kpath_ldmk = ipath_ladmk.joinpath(f"frame-{fid:04d}.txt")
ldmk = np.loadtxt(kpath_ldmk) # shape = (68, 2)
ldmk_norm, pos_mean, side = landmark_normalize(ldmk)
v_ldmk[k, :, :] = ldmk
v_ldmk_norm[k, :, :] = ldmk_norm
v_ldmk_auxs[k, :] = np.array([pos_mean[0], pos_mean[1], side])
# STEP 3: concat data
list_dict.append({"pid": pid, "vid": vid,
"mfcc": list_mfcc,
# "exp": list_exp, "idn":list_idn,
"ldmk": v_ldmk, "ldmk_norm": v_ldmk_norm, "ldmk_auxs":v_ldmk_auxs
})
annodb[pid] = list_dict
# pickle and save
with open(str(path_anno), "wb") as f:
pickle.dump(annodb, f)
print(f"Total have {len(annodb)} item anno, save to --> {path_anno}")