RabbitRUI/ruispace
0
1# check the sync of 3dmm feature and the audio2import cv23import numpy as np4from src.face3d.models.bfm import ParametricFaceModel5from src.face3d.models.facerecon_model import FaceReconModel6import torch7import subprocess, platform8import scipy.io as scio9from tqdm import tqdm 10 11# draft12def gen_composed_video(args, device, first_frame_coeff, coeff_path, audio_path, save_path, exp_dim=64):13 14 coeff_first = scio.loadmat(first_frame_coeff)['full_3dmm']15 16 coeff_pred = scio.loadmat(coeff_path)['coeff_3dmm']17 18 coeff_full = np.repeat(coeff_first, coeff_pred.shape[0], axis=0) # 25719 20 coeff_full[:, 80:144] = coeff_pred[:, 0:64]21 coeff_full[:, 224:227] = coeff_pred[:, 64:67] # 3 dim translation22 coeff_full[:, 254:] = coeff_pred[:, 67:] # 3 dim translation23 24 tmp_video_path = '/tmp/face3dtmp.mp4'25 26 facemodel = FaceReconModel(args)27 28 video = cv2.VideoWriter(tmp_video_path, cv2.VideoWriter_fourcc(*'mp4v'), 25, (224, 224))29 30 for k in tqdm(range(coeff_pred.shape[0]), 'face3d rendering:'):31 cur_coeff_full = torch.tensor(coeff_full[k:k+1], device=device)32 33 facemodel.forward(cur_coeff_full, device)34 35 predicted_landmark = facemodel.pred_lm # TODO.36 predicted_landmark = predicted_landmark.cpu().numpy().squeeze()37 38 rendered_img = facemodel.pred_face39 rendered_img = 255. * rendered_img.cpu().numpy().squeeze().transpose(1,2,0)40 out_img = rendered_img[:, :, :3].astype(np.uint8)41 42 video.write(np.uint8(out_img[:,:,::-1]))43 44 video.release()45 46 command = 'ffmpeg -v quiet -y -i {} -i {} -strict -2 -q:v 1 {}'.format(audio_path, tmp_video_path, save_path)47 subprocess.call(command, shell=platform.system() != 'Windows')48 49 