def run_2d_lmk_fitting(model_fname, template_fname, flame_lmk_path, texture_mapping, target_img_path, target_lmk_path, out_path): if 'generic' not in model_fname: print('You are fitting a gender specific model (i.e. female / male). Please make sure you selected the right gender model. Choose the generic model if gender is unknown.') if not os.path.exists(template_fname): print('Template mesh (in FLAME topology) not found - %s' % template_fname) return if not os.path.exists(flame_lmk_path): print('FLAME landmark embedding not found - %s ' % flame_lmk_path) return if not os.path.exists(target_img_path): print('Target image not found - s' % target_img_path) return if not os.path.exists(target_lmk_path): print('Landmarks of target image not found - s' % target_lmk_path) return if not os.path.exists(out_path): os.makedirs(out_path) lmk_face_idx, lmk_b_coords = load_embedding(flame_lmk_path) target_img = cv2.imread(target_img_path) lmk_2d = np.load(target_lmk_path) weights = {} # Weight of the landmark distance term weights['lmk'] = 1.0 # Weight of the shape regularizer weights['shape'] = 1e-3 # Weight of the expression regularizer weights['expr'] = 1e-3 # Weight of the neck pose (i.e. neck rotationh around the neck) regularizer weights['neck_pose'] = 100.0 # Weight of the jaw pose (i.e. jaw rotation for opening the mouth) regularizer weights['jaw_pose'] = 1e-3 # Weight of the eyeball pose (i.e. eyeball rotations) regularizer weights['eyeballs_pose'] = 10.0 result_mesh, result_scale = fit_lmk2d(target_img, lmk_2d, template_fname, model_fname, lmk_face_idx, lmk_b_coords, weights) if sys.version_info >= (3, 0): texture_data = np.load(texture_mapping, allow_pickle=True, encoding='latin1').item() else: texture_data = np.load(texture_mapping, allow_pickle=True).item() texture_map = compute_texture_map(target_img, result_mesh, result_scale, texture_data) out_mesh_fname = os.path.join(out_path, os.path.splitext(os.path.basename(target_img_path))[0] + '.obj') out_img_fname = os.path.join(out_path, os.path.splitext(os.path.basename(target_img_path))[0] + '.png') cv2.imwrite(out_img_fname, texture_map) result_mesh.set_vertex_colors('white') result_mesh.vt = texture_data['vt'] result_mesh.ft = texture_data['ft'] result_mesh.set_texture_image(out_img_fname) result_mesh.write_obj(out_mesh_fname) np.save(os.path.join(out_path, os.path.splitext(os.path.basename(target_img_path))[0] + '_scale.npy'), result_scale) mv = MeshViewers(shape=[1,2], keepalive=True) mv[0][0].set_static_meshes([Mesh(result_mesh.v, result_mesh.f)]) mv[0][1].set_static_meshes([result_mesh])
def build_texture_from_image(source_img_fname, target_mesh_fname, target_scale_fname, texture_mapping, out_path): if not os.path.exists(source_img_fname): print('Source image not found - %s' % source_img_fname) return if not os.path.exists(target_mesh_fname): print('Target mesh not found - %s' % target_mesh_fname) return if not os.path.exists(target_scale_fname): print('Scale information for target mesh not found %s' % target_scale_fname) return if not os.path.exists(texture_mapping): print('Pre-computed FLAME texture mapping not found %s' % texture_mapping) return if not os.path.exists(out_path): os.makedirs(out_path) source_img = cv2.imread(source_img_fname) target_mesh = Mesh(filename=target_mesh_fname) target_mesh.set_vertex_colors('white') target_scale = np.load(target_scale_fname) if sys.version_info >= (3, 0): texture_data = np.load(texture_mapping, allow_pickle=True, encoding='latin1').item() else: texture_data = np.load(texture_mapping, allow_pickle=True).item() texture_map = compute_texture_map(source_img, target_mesh, target_scale, texture_data) out_mesh_fname = os.path.join(out_path, os.path.splitext(os.path.basename(target_mesh_fname))[0] + '.obj') out_img_fname = os.path.join(out_path, os.path.splitext(os.path.basename(target_mesh_fname))[0] + '.png') cv2.imwrite(out_img_fname, texture_map) target_mesh.vt = texture_data['vt'] target_mesh.ft = texture_data['ft'] target_mesh.set_texture_image(out_img_fname) target_mesh.write_obj(out_mesh_fname) target_mesh.show()