-
Notifications
You must be signed in to change notification settings - Fork 2
/
fit_multiple_images.py
executable file
·167 lines (137 loc) · 8.26 KB
/
fit_multiple_images.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
import numpy as np
from torch.autograd import Variable
from flame import FlameLandmarks
from config import parser,get_config
from utils.mesh_io import write_obj
import argparse
import os,sys
import cv2
from psbody.mesh import Mesh
from psbody.mesh.meshviewer import MeshViewers
from fitting.landmarks_fitting import *
from utils.video import *
from utils.render_mesh import *
from utils.greenscreen import *
import shutil
import errno
import time
def fit_flame_to_images(images, texture_mapping, out_path, load_shape_path, save_shape, show_live):
if not os.path.exists(out_path):
os.makedirs(out_path)
# Build the flame model
flamelayer = FlameLandmarks(config)
flamelayer.cuda()
faces = flamelayer.faces
# Set a fixed shape if one is specified
if load_shape_path:
shape_params_np = np.load(load_shape_path)
flamelayer.set_shape(shape_params_np)
init_image = cv2.imread(images[0])
# Predict face location once (#important! Will not work well when face moves a lot)
face_detector, face_landmarks_predictor = get_face_detector_and_landmarks_predictor()
rect = dlib_get_face_rectangle(init_image, face_detector)
# generate first guess for the first image
target_2d_lmks = dlib_get_landmarks(init_image, rect, face_landmarks_predictor)
# Guess initial camera parameters (weak perspective = only scale)
_, landmarks_3D, _ = flamelayer()
initial_scale = init_weak_prespective_camera_scale_from_landmarks(landmarks_3D, target_2d_lmks)
scale = Variable(torch.tensor(initial_scale, dtype=landmarks_3D.dtype).cuda(),requires_grad=True)
# Initial guess: fit by optimizing only rigid motion
vars = [scale, flamelayer.transl, flamelayer.global_rot] # Optimize for global scale, translation and rotation
rigid_scale_optimizer = torch.optim.LBFGS(vars, tolerance_change=5e-6, max_iter=500)
vertices, result_scale = fit_flame_to_2D_landmarks(flamelayer, scale, target_2d_lmks, rigid_scale_optimizer)
# Initialize the optimizer once, so that consecutive optimization routines will have a warm start
# If a fixed flame shape is given, use it, otherwise also have it in the optimization
if load_shape_path:
vars = [scale, flamelayer.transl, flamelayer.global_rot, flamelayer.expression_params, flamelayer.jaw_pose, flamelayer.neck_pose]
else:
vars = [scale, flamelayer.transl, flamelayer.global_rot, flamelayer.shape_params, flamelayer.expression_params, flamelayer.jaw_pose, flamelayer.neck_pose]
all_flame_params_optimizer = torch.optim.LBFGS(vars, max_iter=10, history_size=10, line_search_fn = 'strong_wolfe')
#all_flame_params_optimizer = lbfgs2.LBFGS2(vars, max_iter=5, history_size=10)#, line_search_fn = 'strong_wolfe')
vertices, result_scale = fit_flame_to_2D_landmarks(flamelayer, scale, target_2d_lmks, all_flame_params_optimizer)
# set more loose optimization params for consecutive steps
opt_params = all_flame_params_optimizer.param_groups[0]
if (save_shape):
mean_shape = flamelayer.shape_params.detach().cpu().numpy().squeeze()
#opt_params['tolerance_change'] = 1e-4 # Could probably make it real time
#opt_params['tolerance_grad'] = 1e-3 # Could probably make it real time
#opt_params['max_iter'] = 2
# add smoothness/temporal consistency
flamelayer.set_laplacian(vertices,faces)
first_fit = True
for target_img_path in images:
print ('Fitting image at ', target_img_path)
target_img = cv2.imread(target_img_path)
time_before = time.time()
target_2d_lmks = dlib_get_landmarks(target_img, rect, face_landmarks_predictor)
# Fit with all of Flame parameters parameters
vertices, result_scale = fit_flame_to_2D_landmarks(flamelayer, scale, target_2d_lmks, all_flame_params_optimizer)
landmarks_and_fitting_time = time.time() - time_before
print ('Landmarks plus fitting took ', landmarks_and_fitting_time)
# add smoothness/temporal consistency
flamelayer.set_ref(vertices)
if (save_shape):
mean_shape = mean_shape + flamelayer.shape_params.detach().cpu().numpy().squeeze()
if show_live:
rendered_img = render_mesh(Mesh(result_scale*vertices, faces), height=target_img.shape[0], width=target_img.shape[1])
#for (x, y) in plt_opt_lmks:
#cv2.circle(rendered_img, (int(x), int(y)), 4, (255, 0, 0), -1)
target_img = np.hstack((target_img, rendered_img))
scale_percent = 50
width = int(target_img.shape[1] * scale_percent / 100)
height = int(target_img.shape[0] * scale_percent / 100)
dim = (width, height)
# resize image
resized = cv2.resize(target_img, dim, interpolation = cv2.INTER_AREA)
cv2.imshow('Frame',resized)
cv2.waitKey(1)
else:
out_texture_img_fname = os.path.join(out_path, os.path.splitext(os.path.basename(target_img_path))[0] + '.png')
result_mesh = get_weak_perspective_textured_mesh(vertices, faces, target_img, texture_mapping, result_scale, out_texture_img_fname)
save_mesh(result_mesh, result_scale, out_path, target_img_path)
if save_shape:
mean_shape = mean_shape/len(images)
np.save(os.path.join(out_path, 'shape_params.npy'), mean_shape)
def save_mesh(result_mesh, result_scale, out_path, target_img_path):
out_mesh_fname = os.path.join(out_path, os.path.splitext(os.path.basename(target_img_path))[0] + '.obj')
write_obj(result_mesh, out_mesh_fname)
np.save(os.path.join(out_path, os.path.splitext(os.path.basename(target_img_path))[0] + '_scale.npy'), result_scale)
def load_data_and_copy_to_output_folder(inp, image_viewpoint_ending, output_folder, use_greenscreen, max_images):
output_file_paths = [os.path.join(output_folder, os.path.basename(inp) + str(i) + 'orig.png') for i in range(max_images)]
# Get all images
if os.path.isdir(inp):
input_images = [os.path.join(inp,img) for img in os.listdir(inp) if img.endswith(image_viewpoint_ending)]
input_images.sort()
input_images = input_images[:max_images]
output_file_paths = output_file_paths[:len(input_images)]
for i in range(len(output_file_paths)):
shutil.copyfile(input_images[i], output_file_paths[i])
elif os.path.isfile(inp):
frames = video_to_images(inp, max_images)
output_file_paths = output_file_paths[:len(frames)]
for i in range(len(output_file_paths)):
if (use_greenscreen):
frames[i] = greenscreen_bg_to_black(frames[i])
cv2.imwrite(output_file_paths[i], frames[i])
else:
raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), config.input)
return output_file_paths
if __name__ == '__main__':
parser.add_argument('--input', help='Path of the input folder for images, or path of video file')
parser.add_argument('--output_folder', help='Output folder path')
parser.add_argument('--image_viewpoint_ending',default='26_C.jpg', help='Ending of the file from the given angle')
parser.add_argument('--texture_mapping',type = str,default = './data/texture_data.npy',help = 'Texture data')
parser.add_argument('--load_shape_path', type = str,default = '', help = 'Load shape from a given path')
parser.add_argument('--max_images', type = int,default = 200, help='Maximum number of images to fit')
# With shape matching before or without
parser.add_argument('--save_shape', dest='save_shape', action='store_true')
parser.add_argument('--show_live', dest='show_live', action='store_true')
parser.add_argument('--greenscreen', dest='greenscreen', action='store_true')
config = get_config()
config.batch_size = 1
config.flame_model_path = './model/male_model.pkl'
images_p = load_data_and_copy_to_output_folder(config.input, config.image_viewpoint_ending, config.output_folder, config.greenscreen, config.max_images)
# uncomment the following to create a movie from the raw images (not reconstruction)
#save_images_in_video(images, config.input_folder, config.output_folder, config.image_viewpoint_ending)
# Iteratively fit flame to images
fit_flame_to_images(images_p, config.texture_mapping, config.output_folder, config.load_shape_path, config.save_shape, config.show_live)