Пример #1
0
def validate(n_img, o_img):
    # Return value 1: No image found in given path
    if not os.path.exists(n_img) or not os.path.exists(o_img):
        log_error('No image found in given path')
        return False, 1, None, None

    # Return value 2: Image is unreadable (size: 0 bytes)
    if not os.path.getsize(n_img) > 0 or not os.path.getsize(o_img) > 0:
        log_error('Image is unreadable (size: 0 bytes)')
        return False, 2, None, None

    # Read input images
    new_im = Image.open(new_img_path)
    old_im = Image.open(old_img_path)

    # Return value 3: Image is below minimum dimension (500x500)
    new_file_width, new_file_height = new_im.size
    old_file_width, old_file_height = old_im.size

    if new_file_width + new_file_height < MIN_HEIGHT + MIN_WIDTH or \
            old_file_width + old_file_height < MIN_HEIGHT + MIN_WIDTH:
        log_error('Image is below minimum dimension')
        return False, 3, new_im, old_im

    # Return value 4: Face not detected
    # Return value 5: Multiple face detected
    new_im_cv2 = cv2.imread(n_img)
    old_im_cv2 = cv2.imread(o_img)
    new_img_points = face_points(new_im_cv2)
    old_img_points = face_points(old_im_cv2)
    if len(new_img_points) == 0:
        log_error('Face not detected')
        return False, 4, new_im, old_im
    if len(old_img_points) == 0:
        log_error('Face not detected')
        return False, 4, new_im, old_im
    # if len(new_img_points) > 1:
    #     log_error('Multiple face detected')
    #     return False, 5, new_im, old_im

    # Return value 6: Face too small
    face_detector = dlib.get_frontal_face_detector()
    gray_image = cv2.cvtColor(new_im_cv2, cv2.COLOR_BGR2GRAY)
    detected_faces = face_detector(gray_image, 0)
    x = detected_faces[0]
    x1, y1, x2, y2 = x.left(), x.top(), x.right(), x.bottom()
    ratio_of_height = (x2 - x1) / OUTPUT_HEIGHT
    ratio_of_weight = (y2 - y1) / OUTPUT_WIDTH
    if (ratio_of_height + ratio_of_weight) / 2.0 < 0.30:
        log_error('Face too small')
        return False, 6, new_im, old_im

    return True, 0, new_im, old_im
Пример #2
0
def load_image_points(path, size):
  print 'in load image points'
  img = scipy.ndimage.imread(path)[..., :3]
  points = locator.face_points(path)
  if len(points) == 0:
    print 'No face in %s' % path
    return None, None
  else:
    # print 'Face landmark points : ',points
    # for i in range (0,71):
    #   cv2.line(img, (points[i][0],points[i][1]), (points[i][0],points[i][1]), (0,255,0),5)
    # cv2.imshow('original points',img)
    # cv2.waitKey(0)

    cv2.line(img, (points[18][0],points[18][1]), (points[18][0],points[18][1]), (255,224,0),5)
    cv2.line(img, (points[21][0],points[21][1]), (points[21][0],points[21][1]), (255,224,0),5)
    cv2.line(img, (points[22][0],points[22][1]), (points[22][0],points[22][1]), (255,224,0),5)
    cv2.line(img, (points[25][0],points[25][1]), (points[25][0],points[25][1]), (255,224,0),5)

    cv2.line(img, (points[30][0],points[30][1]), (points[30][0],points[30][1]), (255,0,0),5)
    cv2.line(img, (points[40][0],points[40][1]), (points[40][0],points[40][1]), (255,0,0),5)

    cv2.line(img, (points[54][0],points[54][1]), (points[54][0],points[54][1]), (0,255,0),5)
    cv2.line(img, (points[56][0],points[56][1]), (points[56][0],points[56][1]), (0,255,0),5)
    cv2.line(img, (points[58][0],points[58][1]), (points[58][0],points[58][1]), (0,255,0),5)

    cv2.line(img, (points[59][0],points[59][1]), (points[59][0],points[59][1]), (0,0,255),5)
    cv2.line(img, (points[65][0],points[65][1]), (points[65][0],points[65][1]), (0,0,255),5)
    cv2.imshow("img",img)
    cv2.waitKey(0)
    return aligner.resize_align(img, points, size)
Пример #3
0
def load_image_points(path, size):
    # print 'in load image points'
    # img = scipy.ndimage.imread(path)[..., :3]
    img = cv2.imread(path)[..., :3]
    # print 'in load image points'
    # img = scipy.ndimage.imread(path)[..., :3]
    points = locator.face_points(path)
    if len(points) == 0:
        print 'No face in %s' % path
        return None, None
    else:
        # print 'Face landmark points : ',points
        # for i in range (0,71):
        #   cv2.line(img, (points[i][0],points[i][1]), (points[i][0],points[i][1]), (0,255,0),5)
        # cv2.imshow('original points',img)
        # cv2.waitKey(0)

        # cv2.line(img, (points[18][0],points[18][1]), (points[18][0],points[18][1]), (255,224,0),5)
        # cv2.line(img, (points[21][0],points[21][1]), (points[21][0],points[21][1]), (255,224,0),5)
        # cv2.line(img, (points[22][0],points[22][1]), (points[22][0],points[22][1]), (255,224,0),5)
        # cv2.line(img, (points[25][0],points[25][1]), (points[25][0],points[25][1]), (255,224,0),5)
        #
        # cv2.line(img, (points[30][0],points[30][1]), (points[30][0],points[30][1]), (255,0,0),5)
        # cv2.line(img, (points[40][0],points[40][1]), (points[40][0],points[40][1]), (255,0,0),5)
        #
        # cv2.line(img, (points[54][0],points[54][1]), (points[54][0],points[54][1]), (0,255,0),5)
        # cv2.line(img, (points[56][0],points[56][1]), (points[56][0],points[56][1]), (0,255,0),5)
        # cv2.line(img, (points[58][0],points[58][1]), (points[58][0],points[58][1]), (0,255,0),5)
        #
        # cv2.line(img, (points[59][0],points[59][1]), (points[59][0],points[59][1]), (0,0,255),5)
        # cv2.line(img, (points[65][0],points[65][1]), (points[65][0],points[65][1]), (0,0,255),5)
        # cv2.imshow("img",img)
        # cv2.waitKey(0)
        # return aligner.resize_align(img, points, size)
        return aligner.resize_align(img, points, size)
Пример #4
0
def load_image_points(path, size):
  img = scipy.ndimage.imread(path)[..., :3]
  points = locator.face_points(path)

  if len(points) == 0:
    return None, None
  else:
    return aligner.resize_align(img, points, size)
Пример #5
0
def load_image_points(path, size):
    img = scipy.ndimage.imread(path)[..., :3]
    points = locator.face_points(path)

    if len(points) == 0:
        return None, None
    else:
        return aligner.resize_align(img, points, size)
Пример #6
0
def load_image_points(img):
    # img = cv2.imread(path)
    points = locator.face_points(img)
    if len(points) == 0:
        # print('No face in the image')
        return (None, None)
    else:
        return (img, points)
Пример #7
0
def load_image_points(path, size):
    img = cv2.imread(path)
    points = locator.face_points(img)

    if len(points) == 0:
        print('No face in %s' % path)
        return None, None
    else:
        return aligner.resize_align(img, points, size)
Пример #8
0
def load_image_points(path):
    img = cv2.imread(path)
    points = locator.face_points(path)

    if len(points) == 0:
        print('No face in %s' % path)
        return (None, None)
    else:
        return (img, points)
Пример #9
0
def load_image_points(path, size):
    img = scipy.ndimage.imread(path)[..., :3]
    points = locator.face_points(path)
    ## Add image boundary points
    #x,y = np.meshgrid([0,size[0]/2,size[0]],[0,size[1]/2,size[1]])
    #im_edge_points = np.vstack((y.flatten(),x.flatten())).T
    #points = np.vstack((points,im_edge_points))

    if len(points) == 0:
        print('No face in %s' % path)
        return None, None
    else:
        return aligner.resize_align(img, points, size)
Пример #10
0
def load_image_points(path, size, fromDb=False):
  img = scipy.ndimage.imread(path)[..., :3]
  if fromDb:
    with con:
      cur = con.cursor()
      data = cur.execute("SELECT points FROM entries WHERE name=?", ([os.path.basename(path)]))
      points = convert_array(cur.fetchone()[0])
  else:
    points = locator.face_points(path)

  if len(points) == 0:
    return None, None
  else:
    return aligner.resize_align(img, points, size)