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, 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, stall_frames) # 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 plt.plot_one(average_face, 'save') 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, 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, stall_frames) # 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) # Comment to remove background #average_face = alpha_image(average_face, points) if alpha else average_face plt.plot_one(average_face, 'save') video.write(average_face) plt.plot_one(dest_img) video.write(dest_img, stall_frames) plt.show()
def morph(name, src_img, src_points, dest_img, dest_points, width=500, height=600, num_frames=20, fps=10, out_frames=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) # Produce morph frames! percent = np.linspace(1, 0, num=num_frames) points = locator.weighted_average_points(src_points, dest_points, percent[8]) 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[8]) 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[8]) average_face = blender.overlay_image(average_face, mask, average_background) #plt.plot_one(average_face) #name = src_img[-11:-4]+"_"+dest_img[-11:-4]+".png" plt.save(average_face,name)
def averager(imgpaths, dest_filename=None, width=500, height=600, background='black', blur_edges=False, out_filename='result.png', plot=False): size = (height, width) images = [] point_set = [] for path in imgpaths: img, points = load_image_points(path, size) if img is not None: images.append(img) point_set.append(points) if len(images) == 0: raise FileNotFoundError('Could not find any valid work.' + ' Supported formats are .jpg, .png, .jpeg') if dest_filename is not None: dest_img, dest_points = load_image_points(dest_filename, size) if dest_img is None or dest_points is None: raise Exception('No face or detected face points in dest img: ' + dest_filename) else: dest_img = np.zeros(images[0].shape, np.uint8) dest_points = locator.average_points(point_set) num_images = len(images) result_images = np.zeros(images[0].shape, np.float32) for i in range(num_images): result_images += warper.warp_image(images[i], point_set[i], dest_points, size, np.float32) result_image = np.uint8(result_images / num_images) face_indexes = np.nonzero(result_image) dest_img[face_indexes] = result_image[face_indexes] mask = blender.mask_from_points(size, dest_points) if blur_edges: blur_radius = 10 mask = cv2.blur(mask, (blur_radius, blur_radius)) if background in ('transparent', 'average'): dest_img = np.dstack((dest_img, mask)) if background == 'average': average_background = locator.average_points(images) dest_img = blender.overlay_image(dest_img, mask, average_background) print('Averaged {} work'.format(num_images)) plt = plotter.Plotter(plot, num_images=1, out_filename=out_filename) plt.save(dest_img) plt.plot_one(dest_img) plt.show()
def averager(imgpaths, width=500, height=600, alpha=False, blur_edges=False, out_filename='result.png', plot=False): size = (height, width) images = [] point_set = [] for path in imgpaths: img, points = load_image_points(path, size) if img is not None: images.append(img) point_set.append(points) ave_points = locator.average_points(point_set) num_images = len(images) result_images = np.zeros(images[0].shape, np.float32) for i in xrange(num_images): result_images += warper.warp_image(images[i], point_set[i], ave_points, size, np.float32) result_image = np.uint8(result_images / num_images) mask = blender.mask_from_points(size, ave_points) if blur_edges: blur_radius = 10 mask = cv2.blur(mask, (blur_radius, blur_radius)) if alpha: result_image = np.dstack((result_image, mask)) mpimg.imsave(out_filename, result_image) if plot: plt.axis('off') plt.imshow(result_image) plt.show()
def averager(imgpaths, width=500, height=600, alpha=False, blur_edges=False, out_filename='result.png', plot=False): size = (height, width) images = [] point_set = [] for path in imgpaths: img, points = load_image_points(path, size) if img is not None: images.append(img) point_set.append(points) ave_points = locator.average_points(point_set) num_images = len(images) result_images = np.zeros(images[0].shape, np.float32) for i in range(num_images): result_images += warper.warp_image(images[i], point_set[i], ave_points, size, np.float32) result_image = np.uint8(result_images / num_images) mask = blender.mask_from_points(size, ave_points) if blur_edges: blur_radius = 10 mask = cv2.blur(mask, (blur_radius, blur_radius)) if alpha: result_image = np.dstack((result_image, mask)) mpimg.imsave(out_filename, result_image) if plot: plt.axis('off') plt.imshow(result_image) plt.show()
def averager(imgpaths, dest_filename=None, width=500, height=600, alpha=False, blur_edges=False, out_filename='result.png', plot=False): size = (height, width) images = [] point_set = [] for path in imgpaths: img, points = load_image_points(path, size) if img is not None: images.append(img) point_set.append(points) if len(images) == 0: raise FileNotFoundError('Could not find any valid images.' + ' Supported formats are .jpg, .png, .jpeg') if dest_filename is not None: dest_img, dest_points = load_image_points(dest_filename, size) if dest_img is None or dest_points is None: raise Exception('No face or detected face points in dest img: ' + dest_filename) else: dest_img = np.zeros(images[0].shape, np.uint8) dest_points = locator.average_points(point_set) num_images = len(images) result_images = np.zeros(images[0].shape, np.float32) for i in range(num_images): result_images += warper.warp_image(images[i], point_set[i], dest_points, size, np.float32) result_image = np.uint8(result_images / num_images) face_indexes = np.nonzero(result_image) dest_img[face_indexes] = result_image[face_indexes] mask = blender.mask_from_points(size, dest_points) if blur_edges: blur_radius = 10 mask = cv2.blur(mask, (blur_radius, blur_radius)) if alpha: dest_img = np.dstack((dest_img, mask)) mpimg.imsave(out_filename, dest_img) if plot: plt.axis('off') plt.imshow(dest_img) plt.show()
def averager(imgpaths, resultFolder, width=500, height=600, alpha=False, blur_edges=False, useDb=False): #startTime = time.time() size = (height, width) images = [] point_set = [] if useDb: #con = sqlite3.connect(os.path.join(alignedFolder, 'points.sqlite'), detect_types=sqlite3.PARSE_DECLTYPES) with con: cur = con.cursor() for path in imgpaths: img = scipy.ndimage.imread(path)[..., :3] if img is not None: filename = os.path.basename(path) data = cur.execute('SELECT points FROM entries WHERE name=?', ([filename])).fetchone() if data: images.append(img) point_set.append(convert_array(data[0])) else: for path in imgpaths: img, points = load_image_points(path, size) if img is not None: images.append(img) point_set.append(points) ave_points = locator.average_points(point_set) num_images = len(images) result_images = np.zeros(images[0].shape, np.float32) for i in xrange(num_images): #print '{0} of {1}'.format(i+1, num_images) result_images += warper.warp_image(images[i], point_set[i], ave_points, size, np.float32) # Store all images result_image = np.uint8(result_images / (i+1)) # mpimg.imsave(os.path.join(resultFolder, filenames[i]), result_image) mpimg.imsave(os.path.join(resultFolder, "{:05d}.jpg".format(i)), result_image) # result_image = np.uint8(result_images / num_images) #result_image = np.uint8(result_images) #mpimg.imsave(os.path.join(resultFolder, "result_raw.jpg"), result_image) result_image = np.uint8(result_images / num_images) mpimg.imsave(os.path.join(resultFolder, "result.jpg"), result_image)
def extract_face(imgPath, savePath, width=500, height=600, useDb=False): size = (height, width) filename = os.path.basename(imgPath) #print filename # path = "{}/captures/capture_{}.jpg".format(rootpath, timestamp) img, points = load_image_points(imgPath, size, fromDb=False) # Save points to database # Save image result_image = np.zeros(img.shape, np.float32) result_image += warper.warp_image(img, points, points, size, np.float32) result_image = np.uint8(result_image) mpimg.imsave(os.path.join(savePath, filename), result_image) if useDb: with con: cur = con.cursor() #print "insert using filename: ", filename cur.execute("INSERT INTO entries ('name', 'points') VALUES(?, ?)", (filename, 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, 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()