def render_mesh(sample_mesh, res=256, scale=1): if sample_mesh.shape[-1] != 3: sample_colours = sample_mesh[..., 3:] else: sample_colours = np.ones_like(sample_mesh) * [0, 0, 1] sample_mesh = sample_mesh[..., :3] sample_mesh = Homogeneous( dm.utils.rotation_matrix(np.deg2rad(90), [0, 0, -1])).apply(sample_mesh) sample_mesh = ColouredTriMesh(sample_mesh * scale * res / 2 + res / 2, trilist=trilist, colours=sample_colours) sample_mesh = lambertian_shading(sample_mesh, ambient_colour=0) store_path = Path(LOGDIR) / str(epoch) if not store_path.exists(): store_path.mkdir() m3io.export_mesh(sample_mesh, store_path / '{}.obj'.format(time.time())) mesh_img = rasterize_mesh(sample_mesh, [res, res]) mesh_img = mesh_img.rotate_ccw_about_centre(180) return mesh_img.pixels_with_channels_at_back()
def uv_in_img(image, mesh, uv_template): from menpo3d.rasterize import rasterize_mesh shape = image.shape u_mesh = ColouredTriMesh( mesh.points, trilist=mesh.trilist, colours=uv_template.points[:, 0, None]) u_image = rasterize_mesh(u_mesh, shape) v_mesh = ColouredTriMesh( mesh.points, trilist=mesh.trilist, colours=uv_template.points[:, 1, None]) v_image = rasterize_mesh(v_mesh, shape) IUV_image = Image(np.concatenate( [u_image.mask.pixels, u_image.pixels / 255., v_image.pixels / 255.]).clip(0, 1)) return IUV_image
def rasterize_mesh_at_template(mesh, img_shape=(640, 480), pose_angle_deg=0, shaded=False): camera = perspective_camera_for_template(img_shape, pose_angle_deg=pose_angle_deg) mesh_aligned = AlignmentSimilarity(mesh, load_template()).apply(mesh) if shaded: mesh_aligned = lambertian_shading(mesh_aligned) return rasterize_mesh(camera.apply(mesh_aligned), img_shape)
def render_initialization(images, mm, id_indices, exp_indices, template_camera, p, qs, cs, img_index): c_i = cs[img_index] q_i = qs[img_index] i_in_img = instance_for_params(mm, id_indices, exp_indices, template_camera, p, q_i, c_i)['instance_in_img'] [x_r, y_r, z_r] = i_in_img.range() av_xy_r = (x_r + y_r) / 2.0 i_in_img = Scale([1, 1, av_xy_r / z_r]).apply(i_in_img) mesh_in_img_lit = lambertian_shading(as_colouredtrimesh(i_in_img)) return rasterize_mesh(mesh_in_img_lit, images[0].shape).as_unmasked()
def render_iteration(mm, id_ind, exp_ind, img_shape, camera, params, img_index, iteration): params_i = params[iteration] c_i = params_i['cs'][img_index] p_i = params_i['p'] q_i = params_i['qs'][img_index] i_in_img = instance_for_params(mm, id_ind, exp_ind, camera, p_i, q_i, c_i)['instance_in_img'] [x_r, y_r, z_r] = i_in_img.range() av_xy_r = (x_r + y_r) / 2.0 i_in_img = Scale([1, 1, av_xy_r / z_r]).apply(i_in_img) mesh_in_img_lit = lambertian_shading(as_colouredtrimesh(i_in_img)) return rasterize_mesh(mesh_in_img_lit, img_shape).as_unmasked()
def render_mesh(sample_mesh, trilist=None, scale=128, offset=128, shape=[256, 256], store_path=None, return_image=True, **kwargs): if isinstance(sample_mesh, ColouredTriMesh): sample_mesh = sample_mesh elif isinstance(sample_mesh, TriMesh): sample_mesh = ColouredTriMesh(sample_mesh.points, trilist=sample_mesh.trilist) sample_mesh = lambertian_shading(sample_mesh, **kwargs) else: if sample_mesh.shape[-1] != 3: sample_colours = sample_mesh[..., 3:] else: sample_colours = np.ones_like(sample_mesh) * [0, 0, 1] sample_mesh = sample_mesh[..., :3] sample_mesh = ColouredTriMesh(sample_mesh * scale + offset, trilist=trilist, colours=sample_colours) sample_mesh = lambertian_shading(sample_mesh, **kwargs) mesh_img = rasterize_mesh(sample_mesh, shape) # mesh_img = mesh_img.rotate_ccw_about_centre(180) if store_path: if not store_path.exists(): store_path.mkdir() m3io.export_mesh(sample_mesh, store_path / '{}.obj'.format(time.time())) if return_image: return mesh_img return mesh_img.pixels_with_channels_at_back()
def render_mesh_in_img(trimesh_3d_in_img, img_shape): [x_r, y_r, z_r] = trimesh_3d_in_img.range() av_xy_r = (x_r + y_r) / 2.0 trimesh_3d_in_img = Scale([1, 1, av_xy_r / z_r]).apply(trimesh_3d_in_img) mesh_in_img_lit = lambertian_shading(as_colouredtrimesh(trimesh_3d_in_img)) return rasterize_mesh(mesh_in_img_lit, img_shape)
def fit(imagepath): image = mio.import_image(imagepath, normalize=False) if len(image.pixels.shape) == 2: image.pixels = np.stack([image.pixels, image.pixels, image.pixels]) if image.pixels.shape[0] == 1: image.pixels = np.concatenate( [image.pixels, image.pixels, image.pixels], axis=0) print(image.pixels_with_channels_at_back().shape) bb = detect(image.pixels_with_channels_at_back())[0] initial_shape = aam_fitter.fit_from_bb(image, bb).final_shape result = fitter.fit_from_shape(image, initial_shape, max_iters=40, camera_update=True, focal_length_update=False, reconstruction_weight=1, shape_prior_weight=.4e8, texture_prior_weight=1., landmarks_prior_weight=1e5, return_costs=True, init_shape_params_from_lms=False) mesh = ColouredTriMesh(result.final_mesh.points, result.final_mesh.trilist) def transform(mesh): return result._affine_transforms[-1].apply( result.camera_transforms[-1].apply(mesh)) mesh_in_img = transform(lambertian_shading(mesh)) expr_dir = image.path.parent p = image.path.stem raster = rasterize_mesh(mesh_in_img, image.shape) uv_shape = (600, 1000) template = shape_model.mean() unwrapped_template = optimal_cylindrical_unwrap(template).apply(template) minimum = unwrapped_template.bounds(boundary=0)[0] unwrapped_template = Translation(-minimum).apply(unwrapped_template) unwrapped_template.points = unwrapped_template.points[:, [1, 0]] unwrapped_template.points[:, 0] = unwrapped_template.points[:, 0].max( ) - unwrapped_template.points[:, 0] unwrapped_template.points *= np.array([.40, .31]) unwrapped_template.points *= np.array([uv_shape]) bcoords_img, tri_index_img = rasterize_barycentric_coordinate_images( unwrapped_template, uv_shape) TI = tri_index_img.as_vector() BC = bcoords_img.as_vector(keep_channels=True).T def masked_texture(mesh_in_image, background): sample_points_3d = mesh_in_image.project_barycentric_coordinates( BC, TI) texture = bcoords_img.from_vector( background.sample(sample_points_3d.points[:, :2])) return texture uv = masked_texture(mesh_in_img, image) t = TexturedTriMesh( result.final_mesh.points, image_coords_to_tcoords(uv.shape).apply(unwrapped_template).points, uv, mesh_in_img.trilist) m3io.export_textured_mesh(t, str(expr_dir / Path(p).with_suffix('.mesh.obj')), overwrite=True) mio.export_image(raster, str(expr_dir / Path(p).with_suffix('.render.jpg')), overwrite=True)