106 lines
3.7 KiB
Python
106 lines
3.7 KiB
Python
import os
|
|
import sys
|
|
import json
|
|
import librosa
|
|
import numpy as np
|
|
from pathlib import Path
|
|
|
|
def load_mask(path_parse):
|
|
|
|
parsing_anno = np.load(path_parse) # (512, 512)
|
|
mask = np.zeros_like(parsing_anno).astype(np.uint8) # (512, 512)
|
|
|
|
atts = ['skin', 'l_brow', 'r_brow', 'l_eye', 'r_eye', 'eye_g', 'l_ear', 'r_ear', 'ear_r',
|
|
'nose', 'mouth', 'u_lip', 'l_lip', 'neck', 'neck_l', 'cloth', 'hair', 'hat']
|
|
|
|
# atts_selected = ['skin', 'l_brow', 'r_brow', 'nose', 'u_lip', 'l_lip']
|
|
atts_selected = ['skin', 'l_brow', 'r_brow', 'l_eye', 'r_eye', 'eye_g', 'l_ear', 'r_ear', 'ear_r',
|
|
'nose', 'mouth', 'u_lip', 'l_lip', ]
|
|
|
|
class_contains = np.unique(parsing_anno)
|
|
|
|
for iclass, iatt in enumerate(atts, 1):
|
|
if (iatt in atts_selected) and (iclass in class_contains):
|
|
index = np.where(parsing_anno == iclass)
|
|
mask[index[0], index[1]] = 1
|
|
|
|
return mask
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
path_data = Path("/ssdata1/lsen/DATA/Actor5/smile")
|
|
|
|
v_dir_videos = sorted(path_data.glob("*"), reverse=False)
|
|
print(f"Have {len(v_dir_videos)} dir files")
|
|
|
|
# for each video
|
|
duration_frame, duration_parse = 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_frame = ipath_data.joinpath("frames")
|
|
ipath_parse = ipath_data.joinpath("frames_crop_parse_512")
|
|
ipath_parse_skin = ipath_data.joinpath("frames_crop_parse_skin_512")
|
|
ipath_parse_skin.mkdir(parents=True, exist_ok=True)
|
|
|
|
# if ipath_save_parse.exists():
|
|
# print(f"ipath_save_exp have already save > {ipath_save_parse}")
|
|
# 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 equal
|
|
vframe_frame = list(ipath_frame.glob("*.jpg"))
|
|
vframe_parse = list(ipath_parse.glob("*.npy"))
|
|
print(f"vframe_frame len = {len(vframe_frame)}, vframe_parse={len(vframe_parse)}")
|
|
|
|
duration_frame += len(vframe_frame) / 50 / 60
|
|
duration_parse += len(vframe_parse) / 50 / 60
|
|
print(f"add duration_frame = {duration_frame} min, duration_parse={duration_parse} min")
|
|
|
|
if len(vframe_frame) != nframes:
|
|
print(f"\n ERROR: nframe_real ={len(vframe_frame)} / nframe_info ={nframes}")
|
|
continue
|
|
|
|
# check have done
|
|
# vframe_parse = list(ipath_parse_skin.glob("*.npy"))
|
|
# if len(vframe_parse) == nframes:
|
|
# print(f"ipath_parse_skin have already done!")
|
|
# continue
|
|
|
|
# feat_parse = []
|
|
for j in range(len(vframe_frame)):
|
|
jpath_parse = ipath_parse.joinpath(f"frame-{j:06d}.npy")
|
|
jpath_parse_save = ipath_parse_skin.joinpath(f"frame-{j:06d}.npy")
|
|
|
|
if not jpath_parse.exists():
|
|
print(f"\n ERROR: Path NOT FOUND! -> {jpath_parse}")
|
|
break
|
|
|
|
data_mask = load_mask(jpath_parse) # (512, 512)
|
|
np.save(jpath_parse_save, data_mask)
|
|
|
|
# feat_parse.append(data_mask)
|
|
|
|
# feat_parse = np.stack(feat_parse, axis=0)
|
|
# if nframes == feat_parse.shape[0]:
|
|
# np.save(ipath_save_parse, feat_parse)
|
|
# print(f"SAVE OK! feat shape = {feat_parse.shape}, dtype={feat_parse.dtype}, nframe={nframes}\n save to -> {ipath_save_parse}") # (nframe, 68, 2)
|
|
# else:
|
|
# print("ERROR, skip! ")
|
|
|
|
|