-
Notifications
You must be signed in to change notification settings - Fork 15
/
demo.py
75 lines (59 loc) · 2.68 KB
/
demo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# This script demos with pre-processed MFCC.
import os
import glob
import torch
import scipy.io as sio
import numpy as np
import cv2
from config import NETWORKS_PARAMETERS
from network import get_network, SynergyNet
from utils import voice2face_processed
from utilf.render import render_vert
# initialization
e_net, _ = get_network('e', NETWORKS_PARAMETERS, train=False)
g_net, _ = get_network('g', NETWORKS_PARAMETERS, train=False)
# building models: unsupervised
image3D = SynergyNet(pretrained=False, last_CN=None).cuda().eval()
backbone_ckpt = torch.load(NETWORKS_PARAMETERS['image3D']['model_path'])
image3D.load_state_dict(backbone_ckpt)
# SynergyNet pretrained network for getting pose
image3D_pretrained = SynergyNet(pretrained=True).cuda().eval()
# data and config
voice_list = sorted(glob.glob('data/preprocessed_MFCC/*'))
up_layer = torch.nn.Upsample((120,120), mode='bilinear', align_corners=True)
tri = sio.loadmat('./train.configs/tri.mat')['tri']
# [TODO] Change this variable to yout result output folder
FOLDER_ROOT = 'data/results/'
if not os.path.exists(FOLDER_ROOT):
os.mkdir(FOLDER_ROOT)
for folder in voice_list:
index = folder.rsplit('/',1)[-1]
print(index)
if not os.path.exists(FOLDER_ROOT+index):
os.mkdir(FOLDER_ROOT + index)
all_sequences = sorted(glob.glob(folder+'/*'))
for sequence in all_sequences:
all_fbanks = sorted(glob.glob(sequence+'/*.npy'))
sequence_name = sequence.rsplit('/',1)[-1]
for fbank in all_fbanks:
fbank_name = fbank.rsplit('/',1)[-1][:-4]
with torch.no_grad():
# voice2face
face_image = voice2face_processed(e_net, g_net, fbank, NETWORKS_PARAMETERS['GPU'])
face_image = up_layer(face_image)
# Pose from 3DDFA-V2
pose = image3D_pretrained(face_image, return_onlypose=True)
R, off = image3D_pretrained.parse_param_102_pose(pose)
#Alignment with synthesized image
prediction = image3D(face_image)
prediction = R @ prediction + off
# transform to image coordinate space
prediction[:, 1, :] = 127 - prediction[:, 1, :]
save_name = FOLDER_ROOT+ index + '/' + sequence_name + '_' + fbank_name
img = (((face_image[0].clamp(-1,1))*127.5)+128).detach().cpu().numpy().astype(np.uint8)
img = np.transpose(img, (1,2,0))
img = img[:,:,[2,1,0]]
pred = prediction[0].detach().cpu().numpy()
# save
cv2.imwrite(save_name+'_image.png', img)
render_vert(img, pred, alpha=1.0, wfp=save_name+'_overlap.png')