-
Notifications
You must be signed in to change notification settings - Fork 138
/
demo.py
205 lines (166 loc) · 9.06 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import numpy as np
import scipy.io.wavfile as wav
import librosa
import os,sys,shutil,argparse,copy,pickle
import math,scipy
from faceformer import Faceformer
from transformers import Wav2Vec2FeatureExtractor,Wav2Vec2Processor
import torch
import torch.nn as nn
import torch.nn.functional as F
import cv2
import tempfile
from subprocess import call
os.environ['PYOPENGL_PLATFORM'] = 'osmesa' # egl
import pyrender
from psbody.mesh import Mesh
import trimesh
@torch.no_grad()
def test_model(args):
if not os.path.exists(args.result_path):
os.makedirs(args.result_path)
#build model
model = Faceformer(args)
model.load_state_dict(torch.load(os.path.join(args.dataset, '{}.pth'.format(args.model_name))))
model = model.to(torch.device(args.device))
model.eval()
template_file = os.path.join(args.dataset, args.template_path)
with open(template_file, 'rb') as fin:
templates = pickle.load(fin,encoding='latin1')
train_subjects_list = [i for i in args.train_subjects.split(" ")]
one_hot_labels = np.eye(len(train_subjects_list))
iter = train_subjects_list.index(args.condition)
one_hot = one_hot_labels[iter]
one_hot = np.reshape(one_hot,(-1,one_hot.shape[0]))
one_hot = torch.FloatTensor(one_hot).to(device=args.device)
temp = templates[args.subject]
template = temp.reshape((-1))
template = np.reshape(template,(-1,template.shape[0]))
template = torch.FloatTensor(template).to(device=args.device)
wav_path = args.wav_path
test_name = os.path.basename(wav_path).split(".")[0]
speech_array, sampling_rate = librosa.load(os.path.join(wav_path), sr=16000)
processor = Wav2Vec2Processor.from_pretrained("facebook/wav2vec2-base-960h")
audio_feature = np.squeeze(processor(speech_array,sampling_rate=16000).input_values)
audio_feature = np.reshape(audio_feature,(-1,audio_feature.shape[0]))
audio_feature = torch.FloatTensor(audio_feature).to(device=args.device)
prediction = model.predict(audio_feature, template, one_hot)
prediction = prediction.squeeze() # (seq_len, V*3)
np.save(os.path.join(args.result_path, test_name), prediction.detach().cpu().numpy())
# The implementation of rendering is borrowed from VOCA: https://github.com/TimoBolkart/voca/blob/master/utils/rendering.py
def render_mesh_helper(args,mesh, t_center, rot=np.zeros(3), tex_img=None, z_offset=0):
if args.dataset == "BIWI":
camera_params = {'c': np.array([400, 400]),
'k': np.array([-0.19816071, 0.92822711, 0, 0, 0]),
'f': np.array([4754.97941935 / 8, 4754.97941935 / 8])}
elif args.dataset == "vocaset":
camera_params = {'c': np.array([400, 400]),
'k': np.array([-0.19816071, 0.92822711, 0, 0, 0]),
'f': np.array([4754.97941935 / 2, 4754.97941935 / 2])}
frustum = {'near': 0.01, 'far': 3.0, 'height': 800, 'width': 800}
mesh_copy = Mesh(mesh.v, mesh.f)
mesh_copy.v[:] = cv2.Rodrigues(rot)[0].dot((mesh_copy.v-t_center).T).T+t_center
intensity = 2.0
rgb_per_v = None
primitive_material = pyrender.material.MetallicRoughnessMaterial(
alphaMode='BLEND',
baseColorFactor=[0.3, 0.3, 0.3, 1.0],
metallicFactor=0.8,
roughnessFactor=0.8
)
tri_mesh = trimesh.Trimesh(vertices=mesh_copy.v, faces=mesh_copy.f, vertex_colors=rgb_per_v)
render_mesh = pyrender.Mesh.from_trimesh(tri_mesh, material=primitive_material,smooth=True)
if args.background_black:
scene = pyrender.Scene(ambient_light=[.2, .2, .2], bg_color=[0, 0, 0])
else:
scene = pyrender.Scene(ambient_light=[.2, .2, .2], bg_color=[255, 255, 255])
camera = pyrender.IntrinsicsCamera(fx=camera_params['f'][0],
fy=camera_params['f'][1],
cx=camera_params['c'][0],
cy=camera_params['c'][1],
znear=frustum['near'],
zfar=frustum['far'])
scene.add(render_mesh, pose=np.eye(4))
camera_pose = np.eye(4)
camera_pose[:3,3] = np.array([0, 0, 1.0-z_offset])
scene.add(camera, pose=[[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 1, 1],
[0, 0, 0, 1]])
angle = np.pi / 6.0
pos = camera_pose[:3,3]
light_color = np.array([1., 1., 1.])
light = pyrender.DirectionalLight(color=light_color, intensity=intensity)
light_pose = np.eye(4)
light_pose[:3,3] = pos
scene.add(light, pose=light_pose.copy())
light_pose[:3,3] = cv2.Rodrigues(np.array([angle, 0, 0]))[0].dot(pos)
scene.add(light, pose=light_pose.copy())
light_pose[:3,3] = cv2.Rodrigues(np.array([-angle, 0, 0]))[0].dot(pos)
scene.add(light, pose=light_pose.copy())
light_pose[:3,3] = cv2.Rodrigues(np.array([0, -angle, 0]))[0].dot(pos)
scene.add(light, pose=light_pose.copy())
light_pose[:3,3] = cv2.Rodrigues(np.array([0, angle, 0]))[0].dot(pos)
scene.add(light, pose=light_pose.copy())
flags = pyrender.RenderFlags.SKIP_CULL_FACES
try:
r = pyrender.OffscreenRenderer(viewport_width=frustum['width'], viewport_height=frustum['height'])
color, _ = r.render(scene, flags=flags)
except:
print('pyrender: Failed rendering frame')
color = np.zeros((frustum['height'], frustum['width'], 3), dtype='uint8')
return color[..., ::-1]
def render_sequence(args):
wav_path = args.wav_path
test_name = os.path.basename(wav_path).split(".")[0]
predicted_vertices_path = os.path.join(args.result_path,test_name+".npy")
if args.dataset == "BIWI":
template_file = os.path.join(args.dataset, args.render_template_path, "BIWI.ply")
elif args.dataset == "vocaset":
template_file = os.path.join(args.dataset, args.render_template_path, "FLAME_sample.ply")
print("rendering: ", test_name)
template = Mesh(filename=template_file)
predicted_vertices = np.load(predicted_vertices_path)
predicted_vertices = np.reshape(predicted_vertices,(-1,args.vertice_dim//3,3))
output_path = args.output_path
if not os.path.exists(output_path):
os.makedirs(output_path)
num_frames = predicted_vertices.shape[0]
tmp_video_file = tempfile.NamedTemporaryFile('w', suffix='.mp4', dir=output_path)
writer = cv2.VideoWriter(tmp_video_file.name, cv2.VideoWriter_fourcc(*'mp4v'), args.fps, (800, 800), True)
center = np.mean(predicted_vertices[0], axis=0)
for i_frame in range(num_frames):
render_mesh = Mesh(predicted_vertices[i_frame], template.f)
pred_img = render_mesh_helper(args,render_mesh, center)
pred_img = pred_img.astype(np.uint8)
writer.write(pred_img)
writer.release()
file_name = test_name+"_"+args.subject+"_condition_"+args.condition
video_fname = os.path.join(output_path, file_name+'.mp4')
cmd = ('ffmpeg' + ' -i {0} -pix_fmt yuv420p -qscale 0 {1}'.format(
tmp_video_file.name, video_fname)).split()
call(cmd)
def main():
parser = argparse.ArgumentParser(description='FaceFormer: Speech-Driven 3D Facial Animation with Transformers')
parser.add_argument("--model_name", type=str, default="biwi")
parser.add_argument("--dataset", type=str, default="BIWI", help='vocaset or BIWI')
parser.add_argument("--fps", type=float, default=25, help='frame rate - 30 for vocaset; 25 for BIWI')
parser.add_argument("--feature_dim", type=int, default=128, help='64 for vocaset; 128 for BIWI')
parser.add_argument("--period", type=int, default=25, help='period in PPE - 30 for vocaset; 25 for BIWI')
parser.add_argument("--vertice_dim", type=int, default=23370*3, help='number of vertices - 5023*3 for vocaset; 23370*3 for BIWI')
parser.add_argument("--device", type=str, default="cuda")
parser.add_argument("--train_subjects", type=str, default="F2 F3 F4 M3 M4 M5")
parser.add_argument("--test_subjects", type=str, default="F1 F5 F6 F7 F8 M1 M2 M6")
parser.add_argument("--output_path", type=str, default="demo/output", help='path of the rendered video sequence')
parser.add_argument("--wav_path", type=str, default="demo/wav/test.wav", help='path of the input audio signal')
parser.add_argument("--result_path", type=str, default="demo/result", help='path of the predictions')
parser.add_argument("--condition", type=str, default="M3", help='select a conditioning subject from train_subjects')
parser.add_argument("--subject", type=str, default="M1", help='select a subject from test_subjects or train_subjects')
parser.add_argument("--background_black", type=bool, default=True, help='whether to use black background')
parser.add_argument("--template_path", type=str, default="templates.pkl", help='path of the personalized templates')
parser.add_argument("--render_template_path", type=str, default="templates", help='path of the mesh in BIWI/FLAME topology')
args = parser.parse_args()
test_model(args)
render_sequence(args)
if __name__=="__main__":
main()