158 lines
6.7 KiB
Python
158 lines
6.7 KiB
Python
import os, sys
|
|
import numpy as np
|
|
import cv2
|
|
from time import time
|
|
import argparse
|
|
import json
|
|
from pathlib import Path
|
|
from torch.utils.data import DataLoader
|
|
|
|
sys.path.insert(0, '/ssdata1/lsen/CODE/DECA_old')
|
|
from decalib.deca import DECA
|
|
from decalib.datasets.datasets2 import ObamaData
|
|
from decalib.utils import util
|
|
from decalib.utils.config import cfg as deca_cfg
|
|
|
|
|
|
def main(args):
|
|
|
|
# device = args.device
|
|
device = "cuda:9"
|
|
# run DECA
|
|
deca_cfg.model.use_tex = args.useTex
|
|
deca = DECA(config = deca_cfg, device=device)
|
|
|
|
# path_data = Path("/yrfs/lsen/DATA/TestData/processing")
|
|
path_data = Path("/ssdata1/lsen/DATA/Actor6/processing2")
|
|
|
|
v_dir_videos = sorted(path_data.glob("*"), reverse=True)
|
|
print(f"Have {len(v_dir_videos)} dir files")
|
|
|
|
|
|
# IDs = ["actor-2HD", "actor-3HD"]
|
|
# IDs = ["clip_full"]
|
|
|
|
# for each video
|
|
for i, ipath_data in enumerate(v_dir_videos):
|
|
ivideo_name = ipath_data.stem
|
|
print(f"\n processing {i}/{len(v_dir_videos)} ...", ivideo_name)
|
|
# if ivideo_name not in IDs: continue
|
|
# if i > 25: break
|
|
|
|
# if ivideo_name not in ["4R3A0139"]: continue
|
|
|
|
ipath_infor = ipath_data.joinpath("video/info.json")
|
|
# ipath_frame = ipath_data.joinpath("frames")
|
|
ipath_frame = ipath_data.joinpath("frames_crop_512")
|
|
|
|
# ipath_kpt2d = ipath_data.joinpath("DECA_kpt2d")
|
|
# ipath_kpt3d = ipath_data.joinpath("DECA_kpt3d")
|
|
ipath_coeff = ipath_data.joinpath("deca_coeff_512")
|
|
# ipath_vis = ipath_data.joinpath("DECA_vis2")
|
|
|
|
# ipath_kpt2d.mkdir(parents=True, exist_ok=True)
|
|
# ipath_kpt3d.mkdir(parents=True, exist_ok=True)
|
|
ipath_coeff.mkdir(parents=True, exist_ok=True)
|
|
# ipath_vis.mkdir(parents=True, exist_ok=True)
|
|
|
|
# load video info
|
|
if not ipath_infor.exists():
|
|
print(f"\n ERROR: info.json not existing {ipath_infor}")
|
|
continue
|
|
|
|
with open(str(ipath_infor), "r") as f:
|
|
info = json.load(f)
|
|
nframes = info['nframes']
|
|
|
|
nframe_real = len(list(ipath_frame.glob("*.jpg")))
|
|
|
|
if nframe_real != nframes:
|
|
print(f"\n ERROR: nframe_real ={nframe_real} / nframe_info ={nframes}")
|
|
continue
|
|
|
|
# check if finish
|
|
nkpt2d = len(list(ipath_coeff.glob("*.npy")))
|
|
if nkpt2d + 10 > nframes:
|
|
print(f"Have finised before, nkpt2d={nkpt2d} nframes = {nframes}")
|
|
continue
|
|
|
|
|
|
datatest = ObamaData(ipath_frame, iscrop=args.iscrop, face_detector=args.detector, device=device)
|
|
dataloader = DataLoader(datatest, batch_size=128, shuffle=False, drop_last=False)
|
|
|
|
for i, ibatch_data in enumerate(dataloader):
|
|
batch_names = ibatch_data["names"]
|
|
batch_image = ibatch_data["image"].to(device)
|
|
|
|
if i%10 == 0:
|
|
print(f"batch {i:04}/{len(dataloader)}... name = ", batch_names[:5])
|
|
|
|
codedict = deca.encode(batch_image) # encoder
|
|
# opdict, visdict = deca.decode(codedict, batch_image) # decoder
|
|
|
|
for j, jname in enumerate(batch_names):
|
|
# jpath_kpt2d = ipath_kpt2d.joinpath(f"{jname}.txt")
|
|
# jpath_kpt3d = ipath_kpt3d.joinpath(f"{jname}.txt")
|
|
jpath_coeff = ipath_coeff.joinpath(f"{jname}.json")
|
|
|
|
# np.savetxt(jpath_kpt2d, opdict['landmarks2d'][j].cpu().numpy(), fmt="%.6f")
|
|
# np.savetxt(jpath_kpt3d, opdict['landmarks3d'][j].cpu().numpy(), fmt="%.6f")
|
|
|
|
# save coeff
|
|
jcodedict = {}
|
|
for key, value in codedict.items():
|
|
jcodedict[key] = value[j].cpu().numpy()
|
|
|
|
# print("json = ", jcodedict)
|
|
np.save(jpath_coeff, jcodedict)
|
|
|
|
# # save render
|
|
# render_names = ['inputs', 'rendered_images']
|
|
# # render_names = ['inputs', 'rendered_images', 'albedo_images', 'shape_images', 'shape_detail_images']
|
|
# for vis_name in render_names:
|
|
# if vis_name not in visdict.keys():
|
|
# continue
|
|
|
|
# img = util.tensor2image(visdict[vis_name][j])
|
|
# cv2.imwrite(str(ipath_vis.joinpath(f"{jname}_{vis_name}.jpg")), img)
|
|
|
|
# save vis
|
|
# cv2.imwrite(str(ipath_vis.joinpath(f"{jname}_vis.jpg")), deca.visualize(visdict))
|
|
|
|
# break
|
|
|
|
if __name__ == '__main__':
|
|
parser = argparse.ArgumentParser(description='DECA: Detailed Expression Capture and Animation')
|
|
|
|
parser.add_argument('-i', '--inputpath', default='TestSamples/examples', type=str,
|
|
help='path to the test data, can be image folder, image path, image list, video')
|
|
parser.add_argument('-d', '--device', default='cuda:0', type=str,
|
|
help='set device, cpu for using cpu' )
|
|
# process test images
|
|
parser.add_argument('--iscrop', default=True, type=lambda x: x.lower() in ['true', '1'],
|
|
help='whether to crop input image, set false only when the test image are well cropped' )
|
|
parser.add_argument('--detector', default='fan', type=str,
|
|
help='detector for cropping face, check decalib/detectors.py for details' )
|
|
# save
|
|
parser.add_argument('--useTex', default=True, type=lambda x: x.lower() in ['true', '1'],
|
|
help='whether to use FLAME texture model to generate uv texture map, \
|
|
set it to True only if you downloaded texture model' )
|
|
parser.add_argument('--saveVis', default=True, type=lambda x: x.lower() in ['true', '1'],
|
|
help='whether to save visualization of output' )
|
|
parser.add_argument('--saveKpt', default=False, type=lambda x: x.lower() in ['true', '1'],
|
|
help='whether to save 2D and 3D keypoints' )
|
|
parser.add_argument('--saveDepth', default=False, type=lambda x: x.lower() in ['true', '1'],
|
|
help='whether to save depth image' )
|
|
parser.add_argument('--saveObj', default=False, type=lambda x: x.lower() in ['true', '1'],
|
|
help='whether to save outputs as .obj, detail mesh will end with _detail.obj. \
|
|
Note that saving objs could be slow' )
|
|
parser.add_argument('--saveMat', default=False, type=lambda x: x.lower() in ['true', '1'],
|
|
help='whether to save outputs as .mat' )
|
|
parser.add_argument('--saveImages', default=False, type=lambda x: x.lower() in ['true', '1'],
|
|
help='whether to save visualization output as seperate images' )
|
|
|
|
main(parser.parse_args())
|
|
|
|
|
|
|