Beispiel #1
0
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()
Beispiel #2
0
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()
Beispiel #3
0
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()
Beispiel #4
0
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()
Beispiel #5
0
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)