-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathinference.py
251 lines (213 loc) · 9.15 KB
/
inference.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import argparse
import subprocess
import python_speech_features
from scipy.io import wavfile
from scipy.interpolate import interp1d
import numpy as np
import pyworld
import torch
from modules.audio2pose import get_pose_from_audio
from skimage import io, img_as_float32
import cv2
from modules.generator import OcclusionAwareGenerator
from modules.keypoint_detector import KPDetector
from modules.audio2kp import AudioModel3D
import yaml,os,imageio
def draw_annotation_box( image, rotation_vector, translation_vector, color=(255, 255, 255), line_width=2):
"""Draw a 3D box as annotation of pose"""
camera_matrix = np.array(
[[233.333, 0, 128],
[0, 233.333, 128],
[0, 0, 1]], dtype="double")
dist_coeefs = np.zeros((4, 1))
point_3d = []
rear_size = 75
rear_depth = 0
point_3d.append((-rear_size, -rear_size, rear_depth))
point_3d.append((-rear_size, rear_size, rear_depth))
point_3d.append((rear_size, rear_size, rear_depth))
point_3d.append((rear_size, -rear_size, rear_depth))
point_3d.append((-rear_size, -rear_size, rear_depth))
front_size = 100
front_depth = 100
point_3d.append((-front_size, -front_size, front_depth))
point_3d.append((-front_size, front_size, front_depth))
point_3d.append((front_size, front_size, front_depth))
point_3d.append((front_size, -front_size, front_depth))
point_3d.append((-front_size, -front_size, front_depth))
point_3d = np.array(point_3d, dtype=np.float).reshape(-1, 3)
# Map to 2d image points
(point_2d, _) = cv2.projectPoints(point_3d,
rotation_vector,
translation_vector,
camera_matrix,
dist_coeefs)
point_2d = np.int32(point_2d.reshape(-1, 2))
# Draw all the lines
cv2.polylines(image, [point_2d], True, color, line_width, cv2.LINE_AA)
cv2.line(image, tuple(point_2d[1]), tuple(
point_2d[6]), color, line_width, cv2.LINE_AA)
cv2.line(image, tuple(point_2d[2]), tuple(
point_2d[7]), color, line_width, cv2.LINE_AA)
cv2.line(image, tuple(point_2d[3]), tuple(
point_2d[8]), color, line_width, cv2.LINE_AA)
def inter_pitch(y,y_flag):
frame_num = y.shape[0]
i = 0
last = -1
while(i<frame_num):
if y_flag[i] == 0:
while True:
if y_flag[i]==0:
if i == frame_num-1:
if last !=-1:
y[last+1:] = y[last]
i+=1
break
i+=1
else:
break
if i >= frame_num:
break
elif last == -1:
y[:i] = y[i]
else:
inter_num = i-last+1
fy = np.array([y[last],y[i]])
fx = np.linspace(0, 1, num=2)
f = interp1d(fx,fy)
fx_new = np.linspace(0,1,inter_num)
fy_new = f(fx_new)
y[last+1:i] = fy_new[1:-1]
last = i
i+=1
else:
last = i
i+=1
return y
def get_audio_feature_from_audio(audio_path,norm = True):
sample_rate, audio = wavfile.read(audio_path)
if len(audio.shape) == 2:
if np.min(audio[:, 0]) <= 0:
audio = audio[:, 1]
else:
audio = audio[:, 0]
if norm:
audio = audio - np.mean(audio)
audio = audio / np.max(np.abs(audio))
a = python_speech_features.mfcc(audio, sample_rate)
b = python_speech_features.logfbank(audio, sample_rate)
c, _ = pyworld.harvest(audio, sample_rate, frame_period=10)
c_flag = (c == 0.0) ^ 1
c = inter_pitch(c, c_flag)
c = np.expand_dims(c, axis=1)
c_flag = np.expand_dims(c_flag, axis=1)
frame_num = np.min([a.shape[0], b.shape[0], c.shape[0]])
cat = np.concatenate([a[:frame_num], b[:frame_num], c[:frame_num], c_flag[:frame_num]], axis=1)
return cat
def audio2head(audio_path, img_path, model_path, save_path):
temp_audio="./results/temp.wav"
command = ("ffmpeg -y -i %s -async 1 -ac 1 -vn -acodec pcm_s16le -ar 16000 %s" % (audio_path, temp_audio))
output = subprocess.call(command, shell=True, stdout=None)
audio_feature = get_audio_feature_from_audio(temp_audio)
frames = len(audio_feature) // 4
img = io.imread(img_path)[:, :, :3]
img = cv2.resize(img, (256, 256))
img = np.array(img_as_float32(img))
img = img.transpose((2, 0, 1))
img = torch.from_numpy(img).unsqueeze(0).cuda()
ref_pose_rot, ref_pose_trans = get_pose_from_audio(img, audio_feature, model_path)
torch.cuda.empty_cache()
config_file = r"./config/vox-256.yaml"
with open(config_file) as f:
config = yaml.load(f)
kp_detector = KPDetector(**config['model_params']['kp_detector_params'],
**config['model_params']['common_params'])
generator = OcclusionAwareGenerator(**config['model_params']['generator_params'],
**config['model_params']['common_params'])
kp_detector = kp_detector.cuda()
generator = generator.cuda()
opt = argparse.Namespace(**yaml.load(open("./config/parameters.yaml")))
audio2kp = AudioModel3D(opt).cuda()
checkpoint = torch.load(model_path)
kp_detector.load_state_dict(checkpoint["kp_detector"])
generator.load_state_dict(checkpoint["generator"])
audio2kp.load_state_dict(checkpoint["audio2kp"])
generator.eval()
kp_detector.eval()
audio2kp.eval()
audio_f = []
poses = []
pad = np.zeros((4,41),dtype=np.float32)
for i in range(0, frames, opt.seq_len // 2):
temp_audio = []
temp_pos = []
for j in range(opt.seq_len):
if i + j < frames:
temp_audio.append(audio_feature[(i+j)*4:(i+j)*4+4])
trans = ref_pose_trans[i + j]
rot = ref_pose_rot[i + j]
else:
temp_audio.append(pad)
trans = ref_pose_trans[-1]
rot = ref_pose_rot[-1]
pose = np.zeros([256, 256])
draw_annotation_box(pose, np.array(rot), np.array(trans))
temp_pos.append(pose)
audio_f.append(temp_audio)
poses.append(temp_pos)
audio_f = torch.from_numpy(np.array(audio_f,dtype=np.float32)).unsqueeze(0)
poses = torch.from_numpy(np.array(poses, dtype=np.float32)).unsqueeze(0)
bs = audio_f.shape[1]
predictions_gen = []
total_frames = 0
for bs_idx in range(bs):
t = {}
t["audio"] = audio_f[:, bs_idx].cuda()
t["pose"] = poses[:, bs_idx].cuda()
t["id_img"] = img
kp_gen_source = kp_detector(img)
gen_kp = audio2kp(t)
if bs_idx == 0:
startid = 0
end_id = opt.seq_len // 4 * 3
else:
startid = opt.seq_len // 4
end_id = opt.seq_len // 4 * 3
for frame_bs_idx in range(startid, end_id):
tt = {}
tt["value"] = gen_kp["value"][:, frame_bs_idx]
if opt.estimate_jacobian:
tt["jacobian"] = gen_kp["jacobian"][:, frame_bs_idx]
out_gen = generator(img, kp_source=kp_gen_source, kp_driving=tt)
out_gen["kp_source"] = kp_gen_source
out_gen["kp_driving"] = tt
del out_gen['sparse_deformed']
del out_gen['occlusion_map']
del out_gen['deformed']
predictions_gen.append(
(np.transpose(out_gen['prediction'].data.cpu().numpy(), [0, 2, 3, 1])[0] * 255).astype(np.uint8))
total_frames += 1
if total_frames >= frames:
break
if total_frames >= frames:
break
log_dir = save_path
if not os.path.exists(os.path.join(log_dir, "temp")):
os.makedirs(os.path.join(log_dir, "temp"))
image_name = os.path.basename(img_path)[:-4]+ "_" + os.path.basename(audio_path)[:-4] + ".mp4"
video_path = os.path.join(log_dir, "temp", image_name)
imageio.mimsave(video_path, predictions_gen, fps=25.0)
save_video = os.path.join(log_dir, image_name)
cmd = r'ffmpeg -y -i "%s" -i "%s" -vcodec copy "%s"' % (video_path, audio_path, save_video)
os.system(cmd)
os.remove(video_path)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("--audio_path",default=r"./demo/audio/intro.wav",help="audio file sampled as 16k hz")
parser.add_argument("--img_path",default=r"./demo/img/paint.jpg", help="reference image")
parser.add_argument("--save_path",default=r"./results", help="save path")
parser.add_argument("--model_path",default=r"./checkpoints/audio2head.pth.tar", help="pretrained model path")
parse = parser.parse_args()
os.makedirs(parse.save_path,exist_ok=True)
audio2head(parse.audio_path,parse.img_path,parse.model_path,parse.save_path)