def export_keypoints_from_BFM_pos(pos, uv_coords, out_path='kpt_vertices_bfm.ply'): bfm_kpt_ind = np.loadtxt('../Data/uv-data/bfm_kpt_ind.txt').astype(np.int32) # same as bfm.kpt_ind kpts_uv = np.array(uv_coords[bfm_kpt_ind]).astype(np.int32).T[:2] kpts = pos[kpts_uv[1], kpts_uv[0], :] mesh = trimesh.Trimesh(vertices=kpts) mesh.export(out_path)
def export_keypoints_from_BFM_and_fg(pos_bfm, fg_obj_path, uv_coords, out_path_bfm='kpt_vertices_bfm.ply', out_path_fg='kpt_vertices_fg.ply'): # BFM bfm_kpt_ind = np.loadtxt('../Data/uv-data/bfm_kpt_ind.txt').astype(np.int32) # same as bfm.kpt_ind bfm_kpt_ind = bfm_kpt_ind[3:] bfm_kpt_ind = np.concatenate((bfm_kpt_ind[:11], bfm_kpt_ind[14:])) kpts_uv = np.array(uv_coords[bfm_kpt_ind]).astype(np.int32).T[:2] kpts = pos_bfm[kpts_uv[1], kpts_uv[0], :] print(kpts.shape) mesh = trimesh.Trimesh(vertices=kpts) mesh.export(out_path_bfm) # FG with open(fg_obj_path) as f: lines = f.readlines() vertices = [line for line in lines if line.startswith('v ')] vertices_array = strip_obj_string(vertices, 3, 2, -2) fg_kpt_ind = np.loadtxt('../Data/uv-data/00011_l68.txt', dtype=int) fg_kpt_ind = fg_kpt_ind[3:] fg_kpt_ind = np.concatenate((fg_kpt_ind[:11], fg_kpt_ind[14:])) vertices_fg = vertices_array[fg_kpt_ind[:]] print(vertices_fg.shape) mesh = trimesh.Trimesh(vertices=vertices_fg) mesh.export(out_path_fg)
def fit_facegen_with_BFM(bfm, obj_path): with open(obj_path) as f: lines = f.readlines() vertices = [line for line in lines if line.startswith('v ')] vertices_array = strip_obj_string(vertices, 3, 2, -2) fg_vertices = get_image_vertices_from_facegen(vertices_array, obj_path.replace('.obj', '_front.png')) # , True) # fg_kpt_ind = np.loadtxt('../Data/uv-data/00011_l68.txt', dtype=int) fg_kpts_ind = np.loadtxt('../Data/uv-data/kpt_ind_fg_anim.txt', dtype=int) fg_kpts = fg_vertices[fg_kpts_ind] ''' face_detector_path = '../Data/net-data/mmod_human_face_detector.dat' shape_predictor_path = '../Data/net-data/shape_predictor_68_face_landmarks.dat' img_path = obj_path.replace('.obj', '_front.png') img = imread(img_path, mode='RGB')[:,:,:3] face_detector = dlib.cnn_face_detection_model_v1(face_detector_path) shape_predictor = dlib.shape_predictor(shape_predictor_path) kpts = generate_keypoints(img, face_detector, shape_predictor) print(kpts) ''' # kpts = to_image(kpts, 512, 512) # print(kpts) # bfm_kpt_ind = np.loadtxt('../Data/uv-data/bfm_kpt_ind.txt').astype(np.int32) # same as bfm.kpt_ind sp, ep, s, angles, t = bfm.fit(fg_kpts[:31, :2], bfm.kpt_ind[:31], max_iter=4) vertices = bfm.generate_vertices(sp, ep) transformed_vertices = bfm.transform(vertices, s, angles, t) mesh = trimesh.Trimesh(vertices=transformed_vertices) mesh.export('fitted_vertices_bfm.ply') mesh = trimesh.Trimesh(vertices=transformed_vertices[bfm.kpt_ind[:31]]) mesh.export('fitted_vertices_kpts.ply') mesh = trimesh.Trimesh(vertices=fg_kpts[:31, :]) mesh.export('kpts_fg_anim.ply')
def export_keypoints_from_fg(vertices): l68_ind_fg = np.loadtxt('../Data/uv-data/00011_l68.txt', dtype=int) kpts_fg = vertices[l68_ind_fg[:]] mesh = trimesh.Trimesh(vertices=kpts_fg) mesh.export(out_path)
def export_vertices_from_pos(pos, out_path='pos_vertices.ply'): vertices = np.reshape(pos, [-1, 3]) print(vertices.shape) mesh = trimesh.Trimesh(vertices=vertices) mesh.export(out_path)