import face3d from face3d import mesh from face3d.morphable_model import MorphabelModel # --------------------- Forward: parameters(shape, expression, pose) --> 3D obj --------------- # --- 1. load model bfm = MorphabelModel('Data/BFM/Out/BFM.mat') print('init bfm model success') # --- 2. generate face mesh: vertices(represent shape) & colors(represent texture) sp = bfm.get_shape_para('random') ep = bfm.get_exp_para('random') vertices = bfm.generate_vertices(sp, ep) tp = bfm.get_tex_para('random') colors = bfm.generate_colors(tp) colors = np.minimum(np.maximum(colors, 0), 1) # --- 3. create a mesh in blender and load 3dmm vertices and faces mesh = bpy.data.meshes.new("3dmm_face") # add the new mesh obj = bpy.data.objects.new(mesh.name, mesh) col = bpy.data.collections.get("Collection") col.objects.link(obj) bpy.context.view_layer.objects.active = obj edges = [] verts = vertices.tolist() faces = bfm.triangles.tolist() mesh.from_pydata(verts, edges, faces)
from face3d import mesh from face3d import mesh_cython from face3d.morphable_model import MorphabelModel # --------------------- Forward: parameters(shape, expression, pose) --> 3D obj --> 2D image --------------- # --- 1. load model bfm = MorphabelModel('Data/BFM/Out/BFM.mat') print('init bfm model success') # --- 2. generate face mesh: vertices(represent shape) & colors(represent texture) sp = bfm.get_shape_para('random') ep = bfm.get_exp_para('random') vertices = bfm.generate_vertices(sp, ep) tp = bfm.get_tex_para('random') colors = bfm.generate_colors(tp) colors = np.minimum(np.maximum(colors, 0), 1) # --- 3. transform vertices to proper position s = 8e-04 angles = [10, 30, 20] t = [0, 0, 0] transformed_vertices = bfm.transform(vertices, s, angles, t) projected_vertices = transformed_vertices.copy() # using stantard camera & orth projection # --- 4. render(3d obj --> 2d image) # set prop of rendering h = w = 256; c = 3 image_vertices = mesh.transform.to_image(projected_vertices, h, w) image = mesh_cython.render.render_colors(image_vertices, bfm.triangles, colors, h, w)
br_corner_y = rects[0].center().y + rects[0].height()/2 # added rects = [(tl_corner_x, tl_corner_y), (br_corner_x, br_corner_y)] landmarks = np.zeros((68, 2)) for i, p in enumerate(shape.parts()): landmarks[i] = [p.x, p.y] im = cv2.circle(im, (p.x, p.y), radius=3, color=(0, 0, 255), thickness=5) bfm = MorphabelModel('Data/BFM/Out/BFM.mat') x = mesh.transform.from_image(landmarks, h, w) X_ind = bfm.kpt_ind global colors global triangles fitted_sp, fitted_ep, fitted_s, fitted_angles, fitted_t = bfm.fit(x, X_ind, max_iter=200, isShow=False) colors = bfm.generate_colors(np.random.rand(bfm.n_tex_para, 1)) colors = np.minimum(np.maximum(colors, 0), 1) fitted_vertices = bfm.generate_vertices(fitted_sp, fitted_ep) transformed_vertices = bfm.transform(fitted_vertices, fitted_s, fitted_angles, fitted_t) image_vertices = mesh.transform.to_image(transformed_vertices, h, w) vertices = image_vertices triangles = bfm.triangles colors = colors/np.max(colors) # load data #C = sio.loadmat('Data/example1.mat') #vertices = C['vertices']; colors = C['colors']; triangles = C['triangles'] #colors = colors/np.max(colors) # transform
def main(args): with open(args.img_list) as f: img_list = [x.strip() for x in f.readlines()] landmark_list = [] if not os.path.exists(args.save_dir): os.mkdir(args.save_dir) if not os.path.exists(args.save_lmk_dir): os.mkdir(args.save_lmk_dir) for img_idx, img_fp in enumerate(tqdm(img_list)): im = cv2.imread(os.path.join(args.img_prefix, img_fp), 1) gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY) h, w, c = im.shape detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor( "face3d/models/shape_predictor_68_face_landmarks.dat") rects = detector(gray, 1) shape = predictor(gray, rects[0]) tl_corner_x = rects[0].center().x - rects[0].width() / 2 tl_corner_y = rects[0].center().y - rects[0].height() / 2 br_corner_x = rects[0].center().x + rects[0].width() / 2 br_corner_y = rects[0].center().y + rects[0].height() / 2 rects = [(tl_corner_x, tl_corner_y), (br_corner_x, br_corner_y)] landmarks = np.zeros((68, 2)) for i, p in enumerate(shape.parts()): landmarks[i] = [p.x, p.y] im = cv2.circle(im, (p.x, p.y), radius=3, color=(0, 0, 255), thickness=5) bfm = MorphabelModel('face3d/Data/BFM/Out/BFM.mat') x = mesh_numpy.transform.from_image(landmarks, h, w) X_ind = bfm.kpt_ind fitted_sp, fitted_ep, fitted_s, fitted_angles, fitted_t = bfm.fit( x, X_ind, max_iter=200, isShow=False) colors = bfm.generate_colors(np.random.rand(bfm.n_tex_para, 1)) colors = np.minimum(np.maximum(colors, 0), 1) fitted_vertices = bfm.generate_vertices(fitted_sp, fitted_ep) transformed_vertices = bfm.transform(fitted_vertices, fitted_s, fitted_angles, fitted_t) image_vertices = mesh_numpy.transform.to_image(transformed_vertices, h, w) triangles = bfm.triangles colors = colors / np.max(colors) attribute = colors color_image = mesh_numpy.render.render_colors(image_vertices, triangles, attribute, h, w, c=3) io.imsave(os.path.join(args.save_lmk_dir, str(img_idx) + ".png"), img_as_ubyte(color_image))