def test_local(): from functools import partial import scipy.ndimage import scipy.misc from facemorpher import locator from facemorpher import aligner # Load source image face_points_func = partial(locator.face_points, '../data') base_path = '../females/Screenshot 2015-03-04 17.11.12.png' src_path = '../females/BlDmB5QCYAAY8iw.jpg' src_img = scipy.ndimage.imread(src_path)[:, :, :3] # Define control points for warps src_points = face_points_func(src_path) base_img = scipy.ndimage.imread(base_path)[:, :, :3] base_points = face_points_func(base_path) size = (600, 500) src_img, src_points = aligner.resize_align(src_img, src_points, size) base_img, base_points = aligner.resize_align(base_img, base_points, size) result_points = locator.weighted_average_points(src_points, base_points, 0.2) # Perform transform dst_img1 = warp_image(src_img, src_points, result_points, size) dst_img2 = warp_image(base_img, base_points, result_points, size) from facemorpher import blender ave = blender.weighted_average(dst_img1, dst_img2, 0.6) mask = blender.mask_from_points(size, result_points) blended_img = blender.poisson_blend(dst_img1, dst_img2, mask)
def morph_image( src_img, src_points, dest_img, dest_points, percent, width=500, height=600, ): """ 모핑 이미지로 변환 :param src_img: ndarray source image :param src_points: source image array of x,y face points :param dest_img: ndarray destination image :param dest_points: destination image array of x,y face points :param percent: 변환 percent """ size = (height, width) points = locator.weighted_average_points(src_points, dest_points, percent) src_face = warper.warp_image(src_img, src_points, points, size) end_face = warper.warp_image(dest_img, dest_points, points, size) average_face = blender.weighted_average(src_face, end_face, percent) average_face = alpha_image(average_face, points) return average_face
def morph(src_img, src_points, dest_img, dest_points, video, width=500, height=600, num_frames=20, fps=10, out_frames=None, out_video=None, alpha=False, plot=False, keep_bg=False): """ Create a morph sequence from source to destination image :param src_img: ndarray source image :param src_img: source image array of x,y face points :param dest_img: ndarray destination image :param dest_img: destination image array of x,y face points :param video: facemorpher.videoer.Video object """ size = (height, width) stall_frames = np.clip(int(fps * 0.15), 1, fps) # Show first & last longer plt = plotter.Plotter(plot, num_images=num_frames, out_folder=out_frames) num_frames -= (stall_frames * 2) # No need to process src and dest image plt.plot_one(src_img) video.write(src_img, 1) # Produce morph frames! for percent in np.linspace(1, 0, num=num_frames): points = locator.weighted_average_points(src_points, dest_points, percent) src_face = warper.warp_image(src_img, src_points, points, size) end_face = warper.warp_image(dest_img, dest_points, points, size) average_face = blender.weighted_average(src_face, end_face, percent) average_face = alpha_image(average_face, points) if alpha else average_face # Average background (find transparent pixel, remove alpha from face image, and than replace transparent with averaged bg) if (keep_bg): average_background = blender.weighted_average( src_img, dest_img, percent) average_face = alpha_image(average_face, points) transparent_pixel = average_face[..., 3] == 0 average_face = average_face[..., :3] average_face[transparent_pixel] = average_background[ transparent_pixel] plt.plot_one(average_face) plt.save(average_face) video.write(average_face) plt.plot_one(dest_img) video.write(dest_img, stall_frames) plt.show()
def morph(src_img, src_points, dest_img, dest_points, video, width=500, height=600, num_frames=20, fps=10, out_frames=None, out_video=None, plot=False, background='black'): """ Create a morph sequence from source to destination image :param src_img: ndarray source image :param src_points: source image array of x,y face points :param dest_img: ndarray destination image :param dest_points: destination image array of x,y face points :param video: facemorpher.videoer.Video object """ size = (height, width) stall_frames = np.clip(int(fps * 0.15), 1, fps) # Show first & last longer plt = plotter.Plotter(plot, num_images=num_frames, out_folder=out_frames) num_frames -= (stall_frames * 2) # No need to process src and dest image plt.plot_one(src_img) video.write(src_img, 1) # Produce morph frames! for percent in np.linspace(1, 0, num=num_frames): points = locator.weighted_average_points(src_points, dest_points, percent) src_face = warper.warp_image(src_img, src_points, points, size) end_face = warper.warp_image(dest_img, dest_points, points, size) average_face = blender.weighted_average(src_face, end_face, percent) if background in ('transparent', 'average'): mask = blender.mask_from_points(average_face.shape[:2], points) average_face = np.dstack((average_face, mask)) if background == 'average': average_background = blender.weighted_average( src_img, dest_img, percent) average_face = blender.overlay_image(average_face, mask, average_background) plt.plot_one(average_face) plt.save(average_face) video.write(average_face) plt.plot_one(dest_img) video.write(dest_img, stall_frames) plt.show()
def morph_one(src_img, src_points, dest_img, dest_points, percent, width=500, height=600): size = (height, width) points = locator.weighted_average_points(src_points, dest_points, percent) src_face = warper.warp_image(src_img, src_points, points, size) end_face = warper.warp_image(dest_img, dest_points, points, size) average_face = blender.weighted_average(src_face, end_face, percent) return average_face, points
def morph(src_img, src_points, dest_img, dest_points, video, width=500, height=600, num_frames=20, fps=10, out_frames=None, out_video=None, alpha=False, plot=False): """ Create a morph sequence from source to destination image :param out_video: :param src_img: ndarray source image :param src_img: source image array of x,y face points :param dest_img: ndarray destination image :param dest_img: destination image array of x,y face points :param video: facemorpher.videoer.Video object """ size = (height, width) stall_frames = np.clip(int(fps * 0.15), 1, fps) # Show first & last longer plt = plotter.Plotter(plot, num_images=num_frames, out_folder=out_frames) num_frames -= (stall_frames * 2) # No need to process src and dest image plt.plot_one(src_img) video.write(src_img, 1) # Produce morph frames! for percent in np.linspace(1, 0, num=num_frames): points = locator.weighted_average_points(src_points, dest_points, percent) src_face = warper.warp_image(src_img, src_points, points, size) end_face = warper.warp_image(dest_img, dest_points, points, size) average_face = blender.weighted_average(src_face, end_face, percent) average_face = alpha_image(average_face, points) if alpha else average_face average_bg = blender.weighted_average(src_img, dest_img, percent) img_over_bg(average_face, average_bg) plt.plot_one(average_bg, 'save') video.write(average_bg) plt.plot_one(dest_img) video.write(dest_img, stall_frames) plt.show()
def morph(src_img, src_points, dest_img, dest_points, video, width=500, height=600, num_frames=20, fps=10, out_frames=None, out_video=None, alpha=False, plot=False, obj=None, sessionid=None, result_type="zero"): """ Create a morph sequence from source to destination image :param src_img: ndarray source image :param src_img: source image array of x,y face points :param dest_img: ndarray destination image :param dest_img: destination image array of x,y face points :param video: facemorpher.videoer.Video object """ size = (height, width) stall_frames = np.clip(int(fps * 0.15), 1, fps) # Show first & last longer plt = plotter.Plotter(plot, num_images=num_frames, out_folder=out_frames) num_frames -= (stall_frames * 2) # No need to process src and dest image plt.plot_one(src_img) video.write(src_img, 1) # Produce morph frames! for percent in np.linspace(1, 0, num=num_frames): points = locator.weighted_average_points(src_points, dest_points, percent) src_face = warper.warp_image(src_img, src_points, points, size, result_type=result_type, bk_img=dest_img) end_face = warper.warp_image(dest_img, dest_points, points, size, result_type=result_type, bk_img=dest_img) # Check for a callback function if obj != None: debugMsg("morph calls mix_callback session={}".format(sessionid)) obj.mix_callback(sessionid, percent, points) else: debugMsg("morph has obj=None") average_face = blender.weighted_average(src_face, end_face, percent) average_face = alpha_image(average_face, points) if alpha else average_face plt.plot_one(average_face) plt.save(average_face) video.write(average_face) plt.plot_one(dest_img) video.write(dest_img, stall_frames) plt.show()
def morph(src_img, src_points, dest_img, dest_points, video, width=500, height=600, num_frames=20, fps=10, out_frames=None, out_video=None, alpha=False, plot=False): """ Create a morph sequence from source to destination image :param src_img: ndarray source image :param src_img: source image array of x,y face points :param dest_img: ndarray destination image :param dest_img: destination image array of x,y face points :param video: facemorpher.videoer.Video object """ size = (height, width) stall_frames = np.clip(int(fps * 0.15), 1, fps) # Show first & last longer plt = plotter.Plotter(plot, num_images=num_frames, out_folder=out_frames) num_frames -= (stall_frames * 2) # No need to process src and dest image label = plotter.Plotter(plot, num_images=2, out_folder=out_frames, label=True) label.plot_one(src_img, src_points) label.plot_one(dest_img, dest_points) label.show() plt.plot_one(src_img) video.write(src_img, 1) try: os.mkdir(os.path.join(os.getcwd(), 'result')) os.mkdir(os.path.join(os.getcwd(), 'result', 'src')) os.mkdir(os.path.join(os.getcwd(), 'result', 'src_corners')) os.mkdir(os.path.join(os.getcwd(), 'result', 'end')) os.mkdir(os.path.join(os.getcwd(), 'result', 'average')) except Exception as e: print(e) # Produce morph frames! for percent in np.linspace(1, 0, num=num_frames): points = locator.weighted_average_points(src_points, dest_points, percent) src_face = warper.warp_image(src_img, src_points, points, size) end_face = warper.warp_image(dest_img, dest_points, points, size) average_face = blender.weighted_average(src_face, end_face, percent) average_face = alpha_image(average_face, points) if alpha else average_face average_face[:, :, :3] = correct_colours(src_face, average_face, np.matrix(points)) corners = np.array([ np.array([0, 0]), np.array([0, height - 2]), np.array([width - 2, 0]), np.array([width - 2, height - 2]) ]) src_points_with_corners = np.concatenate((src_points, corners)) points_with_corners = np.concatenate((points, corners)) src_face_corners = warper.warp_image(src_img, src_points_with_corners, points_with_corners, size) average_face = process_edge(src_face_corners, average_face, width, height) plt.plot_one(average_face) filename = '%d.jpg' % int((1 - percent) * num_frames) cv2.imwrite(os.path.join(os.getcwd(), 'result', 'src', filename), src_face) cv2.imwrite( os.path.join(os.getcwd(), 'result', 'src_corners', filename), src_face_corners) cv2.imwrite(os.path.join(os.getcwd(), 'result', 'end', filename), end_face) cv2.imwrite(os.path.join(os.getcwd(), 'result', 'average', filename), average_face) plt.save(average_face) video.write(average_face) plt.plot_one(dest_img) video.write(dest_img, stall_frames) plt.show()