Example #1
0
def generator_demo_example_lips(img_path):
    name = img_path.split('/')[-1]
    landmark_path = os.path.join('../image/', name.replace('jpg', 'npy'))
    region_path = os.path.join('../image/',
                               name.replace('.jpg', '_region.jpg'))
    roi, landmark = crop_image(img_path)
    if np.sum(landmark[37:39, 1] - landmark[40:42, 1]) < -9:

        # pts2 = np.float32(np.array([template[36],template[45],template[30]]))
        template = np.load('../basics/base_68.npy')
    else:
        template = np.load('../basics/base_68_close.npy')
    # pts2 = np.float32(np.vstack((template[27:36,:], template[39,:],template[42,:],template[45,:])))
    pts2 = np.float32(template[27:45, :])
    # pts2 = np.float32(template[17:35,:])
    # pts1 = np.vstack((landmark[27:36,:], landmark[39,:],landmark[42,:],landmark[45,:]))
    pts1 = np.float32(landmark[27:45, :])
    # pts1 = np.float32(landmark[17:35,:])
    tform = tf.SimilarityTransform()
    tform.estimate(pts2, pts1)
    dst = tf.warp(roi, tform, output_shape=(163, 163))

    dst = np.array(dst * 255, dtype=np.uint8)
    dst = dst[1:129, 1:129, :]
    cv2.imwrite(region_path, dst)

    gray = cv2.cvtColor(dst, cv2.COLOR_BGR2GRAY)

    # detect faces in the grayscale image
    rects = detector(gray, 1)
    for (i, rect) in enumerate(rects):

        shape = predictor(gray, rect)
        shape = utils.shape_to_np(shape)
        shape, _, _ = normLmarks(shape)
        np.save(landmark_path, shape)
        lmark = shape.reshape(68, 2)
        name = img_path[:-4] + 'lmark.png'

        utils.plot_flmarks(lmark,
                           name, (-0.2, 0.2), (-0.2, 0.2),
                           'x',
                           'y',
                           figsize=(10, 10))
    return dst, lmark
Example #2
0
def crop_image(image_path):
    image = cv2.imread(image_path)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    rects = detector(gray, 1)
    for (i, rect) in enumerate(rects):
        shape = predictor(gray, rect)
        shape = utils.shape_to_np(shape)
        (x, y, w, h) = utils.rect_to_bb(rect)
        center_x = x + int(0.5 * w)
        center_y = y + int(0.5 * h)
        r = int(0.64 * h)
        new_x = center_x - r
        new_y = center_y - r
        roi = image[new_y:new_y + 2 * r, new_x:new_x + 2 * r]

        roi = cv2.resize(roi, (163, 163), interpolation=cv2.INTER_AREA)
        scale = 163. / (2 * r)

        shape = ((shape - np.array([new_x, new_y])) * scale)

        return roi, shape