Example #1
0
def pre_proc_example(example_image):
    example_landmarks = facial_landmarks_detection_dlib.get_facial_landmarks(example_image)
    resolution = 512
    example_face_rect = face_process.select_face_rect(example_landmarks)
    new_example_face, new_example_face_rect, t_example = face_process.calc_image_transform(example_face_rect,
                                                                                           example_image,
                                                                                           resolution=resolution)

    new_example_landmarks = facial_landmarks_detection_dlib.get_facial_landmarks(new_example_face)
    new_example_landmarks = add_landmarks(new_example_landmarks,
                                              tuple([new_example_face.shape[0], new_example_face.shape[1]]))

    alpha_map_example, _ = face_process.get_new_face_mask(new_example_face, segment=True)

    return new_example_face, alpha_map_example, new_example_landmarks
 def test_get_facial_landmarks_dlib(self):
     image_path = '../assets/examples/example_6.jpg'
     image = cv2.imread(image_path)
     rect = face_process.get_face_rect(image)
     landmarks = facial_landmarks_detection_dlib.get_facial_landmarks(
         image, rect)
     landmark_image = face_segmentation.draw_landmark(image, landmarks)
     cv2.imwrite('test_facial_landmarks.jpg', landmark_image)
     pass
def makeup_by_facial_feature(target_image, example_image):
    target_landmarks = facial_landmarks_detection_dlib.get_facial_landmarks(
        target_image)
    example_landmarks = facial_landmarks_detection_dlib.get_facial_landmarks(
        example_image)
    target_triangle, target_triangle_mesh = get_triangle(target_landmarks)
    example_triangle_mesh = face_segmentation.get_triangle_mesh(
        example_landmarks, target_triangle)

    target_pts, example_pts = face_segmentation.get_pixels_warp(
        target_triangle_mesh, target_image.shape, example_triangle_mesh)
    alpha_map, _ = face_process.get_new_face_mask(example_image)
    fusion_image = \
        makeup_transfer_by_example.color_blending(target_image,
                                                  target_pts,
                                                  example_image,
                                                  example_pts, alpha_map=alpha_map)

    return fusion_image
Example #4
0
def get_new_face_mask(image, landmarks=None, segment=False):
    face = get_face_image(image)
    max_, threds1, threds2 = _whiteness_analysis(face)

    image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    mask = copy.copy(image_gray) / 255
    mask = 1.5 - mask
    mask = np.tanh(mask * 1.5)

    if segment is True:
        face_rect = {
            'left_top': tuple([0, 0]),
            'bottom_right': tuple([image.shape[1], image.shape[0]])
        }
        image_rescale, _, t = calc_image_transform(face_rect,
                                                   image,
                                                   ratio=1,
                                                   resolution=256)
        if landmarks is None:
            landmarks = facial_landmarks_detection_dlib.get_facial_landmarks(
                image_rescale)
        else:
            landmarks = rescale_landmarks(t, landmarks)
        contour = landmarks[0:17]

        left_eye_brow = np.asarray(list(reversed(landmarks[22:27])))
        left_eye_brow[:, 1] = left_eye_brow[:, 1] - 10
        left_eye_brow[np.where(left_eye_brow < 0)] = 0
        contour.extend(left_eye_brow.tolist())

        right_eye_brow = np.asarray(list(reversed(landmarks[17:22])))
        right_eye_brow[:, 1] = right_eye_brow[:, 1] - 10
        right_eye_brow[np.where(right_eye_brow < 0)] = 0
        contour.extend(right_eye_brow)
        trimap = knn_matte.get_face_trimap(image_rescale, contour,
                                           np.ones((8, 8)))

        # cv2.imshow('trimap', np.asarray(trimap, dtype=np.uint8))

        matting = knn_matte.knn_matte(image_rescale, trimap)

        matting = cv2.resize(matting,
                             dsize=(image.shape[1], image.shape[0]),
                             interpolation=cv2.INTER_LINEAR)
        # cv2.imshow('alpha', np.asarray(255 * matting, dtype=np.uint8))
        # cv2.imshow('fusion_weights', np.asarray(255 * mask, dtype=np.uint8))
        # cv2.waitKey(500)
        mask = np.multiply(matting, mask)
    scalar = np.repeat(mask, 3, axis=1)
    scalar = np.reshape(scalar, image.shape)
    res = np.multiply(scalar, image)
    return mask, res
Example #5
0
    def test_get_facial_segments(self):
        image_path = '../assets/examples/example_8.jpeg'
        image = cv2.imread(image_path)

        new_image = np.zeros(image.shape)
        landmarks = facial_landmarks_detection_dlib.get_facial_landmarks(
            image, add_points=True)

        chin_landmarks = landmarks[0:17]
        coords = face_segmentation.find_face_region(chin_landmarks,
                                                    image.shape)
        new_image[coords[0], coords[1], :] = image[coords[0], coords[1], :]
        cv2.imwrite('test1.jpg', new_image)
        pass
Example #6
0
    def test_color_blending(self):
        example_path = '../assets/targets/target_12.jpg'
        example_image = cv2.imread(example_path)
        example_landmarks = facial_landmarks_detection_dlib.get_facial_landmarks(
            example_image, add_points=True)
        target_path = '../assets/targets/target_9.jpg'
        target = cv2.imread(target_path)
        target_landmarks = facial_landmarks_detection_dlib.get_facial_landmarks(
            target, add_points=True)

        _, target_triangle_indices, target_triangle_meshes = \
            face_segmentation.get_triangle_landmarks(target_landmarks)

        example_triangle_meshes = face_segmentation.get_triangle_mesh(
            example_landmarks, target_triangle_indices)
        target_pts, example_pts = face_segmentation.get_pixels_warp(
            target_triangle_meshes, target.shape, example_triangle_meshes)

        fusion_image = makeup_transfer_by_example.color_blending(
            target, target_pts, example_image, example_pts)
        cv2.imwrite('test_blending_result.jpg', fusion_image)

        pass
Example #7
0
    def test_face_seg(self):
        triangle_list_path = '../../resources/landmark_triangle_index.txt'
        triangle_list = np.loadtxt(triangle_list_path, dtype=int)
        image_path = '../assets/targets/target_0.jpg'
        image = cv2.imread(image_path)

        landmarks = facial_landmarks_detection_dlib.get_facial_landmarks(
            image, add_points=True)
        triangles = face_segmentation.get_triangle_mesh(
            landmarks, triangle_list)

        for triangle in triangles:
            cv2.polylines(image, [np.asarray(triangle)], True, (0, 0, 255))
        cv2.imwrite('test1.jpg', image)
        pass
    def test_facial_features(self):
        image_path = '../assets/targets/target_12.jpg'
        image = cv2.imread(image_path)
        landmarks = facial_landmarks_detection_dlib.get_facial_landmarks(image)
        _, triangle, triangle_mesh = face_segmentation.get_triangle_landmarks(
            landmarks)

        triangle, triangle_mesh = makeup_by_facial_features.get_triangle(
            landmarks)
        image = face_segmentation.draw_landmark(image, landmarks)

        for triangle in triangle_mesh:
            cv2.polylines(image, [np.asarray(triangle)], True, (0, 255, 255))
        cv2.imwrite('facial_features.jpg', image)
        pass
Example #9
0
 def test_gmm_color_model(self):
     image_path = '../assets/examples/example_8.jpeg'
     image = cv2.imread(image_path)
     image_size = tuple([image.shape[0], image.shape[1]])
     landmarks = facial_landmarks_detection_dlib.get_facial_landmarks(image)
     facial_region_index = face_segmentation.get_facial_indices(
         landmarks, image_size)
     gmm_color = face_segmentation.gmm_color_model(image,
                                                   facial_region_index)
     color = np.mean(image[facial_region_index[0],
                           facial_region_index[1], :],
                     axis=0) / 255
     image[facial_region_index[0],
           facial_region_index[1], :] = (255, 255, 255)
     cv2.imwrite('test_facial_region.jpg', image)
     scores = gmm_color.score(np.asarray([color, [0, 0, 0]]))
     self.assertGreater(scores[0], scores[1])
    def test_transfer_image(self):
        image_path = '../assets/examples/example_8.jpeg'
        image = cv2.imread(image_path)
        landmarks = facial_landmarks_detection_dlib.get_facial_landmarks(image)
        face_rect = face_process.select_face_rect(landmarks)
        face_image = image[face_rect['left_top'][1]:face_rect['bottom_right'][1],
                           face_rect['left_top'][0]:face_rect['bottom_right'][0]]
        cv2.imwrite('test.jpg', face_image)
        new_image, t = face_process.calc_image_transform(face_rect, image, resolution=512)
        cv2.imwrite('test1.jpg', new_image)
        new_landmarks = []
        for landmark in landmarks:
            new_landmarks.append(face_process.calc_transform_pts(landmark, t))

        new_image = face_segmentation.draw_landmark(new_image, new_landmarks)

        cv2.imwrite('test2.jpg', new_image)
        self.assertEquals(True,True)
 def test_landmarks_rectification(self):
     image_path = '../assets/targets/target_10.jpeg'
     image = cv2.imread(image_path)
     landmarks = facial_landmarks_detection_dlib.get_facial_landmarks(image)
     face_contour_landmarks = landmarks[0:17]
     image_size = tuple([image.shape[0], image.shape[1]])
     facial_region_index = face_segmentation.get_facial_indices(
         landmarks, image_size)
     gmm_color = face_segmentation.gmm_color_model(image,
                                                   facial_region_index)
     rectified_landmarks = landmarks_rectification.landmarks_rectify(
         image,
         face_contour_landmarks,
         gmm_color,
         resolution_list=tuple([512]))
     image = face_segmentation.draw_landmark(image, face_contour_landmarks)
     image = face_segmentation.draw_landmark(image, rectified_landmarks,
                                             (0, 0, 255))
     cv2.imwrite('test_rectify.jpg', image)
    def test_calc_transform_mat(self):
        image_path = '../assets/examples/after-makeup1.jpeg'
        image = cv2.imread(image_path)
        landmarks = facial_landmarks_detection_dlib.get_facial_landmarks(image)
        height, width = image.shape[0], image.shape[1]
        origin_resolution = min(height, width)
        origin_center = tuple([origin_resolution /2, origin_resolution/2])
        image = image[0: origin_resolution, 0:origin_resolution]
        new_resolution = 256
        image = cv2.resize(image, dsize=(int(new_resolution),int(new_resolution)))
        new_center = tuple([new_resolution / 2, new_resolution/ 2])

        transform = face_process._calc_transfer_mat(origin_center, new_center, origin_resolution, new_resolution)

        new_landmarks = []
        for landmark in landmarks:
            new_landmarks.append(face_process.calc_transform_pts(landmark, transform))

        new_image = face_segmentation.draw_landmark(image, new_landmarks)
        cv2.imwrite('test1.jpg', new_image)
    def test__landmark_rectify(self):
        image_path = '../assets/examples/example_8.jpeg'
        image = cv2.imread(image_path)
        landmarks = facial_landmarks_detection_dlib.get_facial_landmarks(image)
        face_contour_landmarks = landmarks[0:17]
        image_size = tuple([image.shape[0], image.shape[1]])
        facial_region_index = face_segmentation.get_facial_indices(
            landmarks, image_size)
        gmm_color = face_segmentation.gmm_color_model(image,
                                                      facial_region_index)
        rectified_landmark = landmarks_rectification._landmark_rectify(
            image,
            face_contour_landmarks,
            gmm_color,
            search_dir='horizontal',
            window_size=25)

        image = face_segmentation.draw_landmark(image, landmarks)
        image = face_segmentation.draw_landmark(image,
                                                rectified_landmark,
                                                color=(0, 0, 255))
        cv2.imwrite('test_rectify.jpg', image)
Example #14
0
def makeup_by_whole_face_transfer(target_image, example_face, example_alpha, example_landmarks, rectify_landmarks=False):
    target_landmarks = facial_landmarks_detection_dlib.get_facial_landmarks(target_image)
    resolution = 512
    if rectify_landmarks is True:
        target_gmm_color = _get_facial_gmm_model(target_image, target_landmarks)
        target_contour_landmarks = target_landmarks[0:17]
        target_contour_landmarks = landmarks_rectification.landmarks_rectify(target_image,
                                                                             target_contour_landmarks,
                                                                             target_gmm_color,
                                                                             resolution_list=tuple([516])
                                                                             )
        target_landmarks[0:17] = target_contour_landmarks
        example_gmm_color = _get_facial_gmm_model(example_face, example_landmarks)
        example_contour_landmarks = example_landmarks[0:17]
        example_contour_landmarks = landmarks_rectification.landmarks_rectify(example_face,
                                                                              example_contour_landmarks,
                                                                              example_gmm_color,
                                                                              resolution_list=tuple([516])
                                                                              )
        example_landmarks[0:17] = example_contour_landmarks

    target_face_rect = face_process.select_face_rect(target_landmarks)
    new_target_face, new_target_face_rect, t_target = face_process.calc_image_transform(target_face_rect,
                                                                                        target_image,
                                                                                        resolution=resolution)
    target_landmarks_rescale = utils.face_process.rescale_landmarks(t_target, target_landmarks)
    origin_new_target_face_shape = tuple([new_target_face.shape[1], new_target_face.shape[0]])

    t_head_pose_rectify, rectify_head_pose_image_shape = \
        head_pose_estimation.landmarks_rectify_by_head_pose(origin_new_target_face_shape, target_landmarks_rescale)

    if np.array_equal(t_head_pose_rectify, np.eye(3)) is False:
        new_target_face = cv2.warpAffine(new_target_face, t_head_pose_rectify[0:2], rectify_head_pose_image_shape)
        target_landmarks_rescale = utils.face_process.rescale_landmarks(t_head_pose_rectify, target_landmarks_rescale)

    # cv2.imshow('rectify_face', new_target_face)
    # cv2.waitKey(500)

    target_landmarks_rescale = add_landmarks(target_landmarks_rescale,
                                             tuple([new_target_face.shape[0], new_target_face.shape[1]]))

    alpha_map_target, _ = face_process.get_new_face_mask(new_target_face, segment=True)
    alpha_map_example = cv2.cvtColor(example_alpha, cv2.COLOR_BGR2GRAY) / 255

    # cv2.imshow('alpha_map',
    #            np.concatenate((np.asarray(alpha_map_example * 255, dtype=np.uint8),
    #                            np.asarray(alpha_map_target * 255, dtype=np.uint8)), axis=1))
    # cv2.imshow('origin_image',
    #            np.concatenate((new_example_face,
    #                            new_target_face), axis=1))
    # cv2.waitKey(500)
    triangle_index_path = '../resources/landmark_triangle_index.txt'
    triangle_index = np.loadtxt(triangle_index_path, dtype=int)
    # _, target_triangle, target_triangle_mesh = face_segmentation.get_triangle_landmarks(target_landmarks_rescale)

    target_triangle_mesh = face_segmentation.get_triangle_mesh(target_landmarks_rescale, triangle_index)
    example_triangle_mesh = face_segmentation.get_triangle_mesh(example_landmarks, triangle_index)

    target_pts, ref_pts = face_segmentation.get_pixels_warp(target_triangle_mesh,
                                                            new_target_face.shape,
                                                            example_triangle_mesh)

    fusion_face = color_blending(new_target_face,
                                 target_pts,
                                 example_face,
                                 ref_pts,
                                 alpha_map=alpha_map_example,
                                 alpha=0.7)
    alpha_map_target = alpha_map_target.reshape((alpha_map_target.shape[0], alpha_map_target.shape[1], 1))

    # cv2.imshow('face', np.asarray(np.multiply(np.repeat(alpha_map_target,3,axis=2), fusion_face),dtype=np.uint8))
    # cv2.imshow('back', np.asarray(np.multiply(1 - np.repeat(alpha_map_target, 3, axis=2), new_target_face), dtype=np.uint8))
    # cv2.waitKey(300)
    fusion_face = np.multiply(np.repeat(alpha_map_target, 3, axis=2), fusion_face) + \
                  np.multiply(np.ones((alpha_map_target.shape[0], alpha_map_target.shape[1], 3)) -
                              np.repeat(alpha_map_target, 3, axis=2), new_target_face)
    # cv2.imshow('fusion_face', np.asarray(fusion_face, dtype=np.uint8))
    # cv2.waitKey(500)

    origin_coords = new_target_face_rect['left_top']
    origin_height = new_target_face_rect['bottom_right'][1] - new_target_face_rect['left_top'][1]
    origin_width = new_target_face_rect['bottom_right'][0] - new_target_face_rect['left_top'][0]

    if np.array_equal(t_head_pose_rectify, np.eye(3)) is False:
        fusion_face = cv2.warpAffine(fusion_face, inv(t_head_pose_rectify)[0:2], origin_new_target_face_shape)

    fusion_face_rescale = cv2.resize(fusion_face,
                                     dsize=(origin_width, origin_height),
                                     interpolation=cv2.INTER_LINEAR)

    target_image[int(origin_coords[1]):int(origin_coords[1]) + origin_height,
    int(origin_coords[0]):int(origin_coords[0]) + origin_width, :] = fusion_face_rescale

    return target_image
Example #15
0
    def test_get_triangle_mesh(self):
        image_path = '../assets/examples/example_8.jpeg'
        image = cv2.imread(image_path)

        landmarks = facial_landmarks_detection_dlib.get_facial_landmarks(
            image, add_points=True)
        lip = np.arange(48, 68).tolist()
        landmarks_coords, triangle_indices, triangles = face_segmentation.get_triangle_landmarks(
            landmarks)
        tmp_triangle_indices = []
        for triangle_index in triangle_indices:
            if (triangle_index[0] in lip) & \
               (triangle_index[1] in lip) & \
               (triangle_index[2] in lip):
                continue
            tmp_triangle_indices.append(triangle_index)

        up_lip_index = np.arange(48, 55)
        up_lip_index = np.hstack((up_lip_index, np.arange(60, 65))).tolist()
        up_lip_landmarks = np.asarray(landmarks)[up_lip_index]
        _, triangle_indices, _ = face_segmentation.get_triangle_landmarks(
            up_lip_landmarks)

        bottom_contour = [0]
        bottom_contour.extend(np.arange(6, 12).tolist())
        selected_triangle_index = list()
        for i, triangle_index in enumerate(triangle_indices):
            if (triangle_index[0] in bottom_contour) & \
                    (triangle_index[1] in bottom_contour) & \
                    (triangle_index[2] in bottom_contour):
                continue
            selected_triangle_index.append(i)
        triangle_indices = np.asarray(
            triangle_indices)[selected_triangle_index]
        up_lip_triangle = []
        for triangle_index in triangle_indices:
            tmp = [
                up_lip_index[triangle_index[0]],
                up_lip_index[triangle_index[1]],
                up_lip_index[triangle_index[2]]
            ]
            up_lip_triangle.append(tmp)

        tmp_triangle_indices.extend(up_lip_triangle)

        bottom_lip_index = [48]
        bottom_lip_index.extend(np.arange(54, 61).tolist())
        bottom_lip_index.extend(np.arange(64, 68).tolist())
        bottom_lip_landmarks = np.asarray(landmarks)[bottom_lip_index]
        _, triangle_indices, _ = face_segmentation.get_triangle_landmarks(
            bottom_lip_landmarks)

        up_contour = np.arange(7, 12).tolist()
        selected_triangle_index = list()
        for i, triangle_index in enumerate(triangle_indices):
            if (triangle_index[0] in up_contour) & \
                    (triangle_index[1] in up_contour) & \
                    (triangle_index[2] in up_contour):
                continue
            selected_triangle_index.append(i)
        triangle_indices = np.asarray(
            triangle_indices)[selected_triangle_index]
        bottom_lip_triangle = []
        for triangle_index in triangle_indices:
            tmp = [
                bottom_lip_index[triangle_index[0]],
                bottom_lip_index[triangle_index[1]],
                bottom_lip_index[triangle_index[2]]
            ]
            bottom_lip_triangle.append(tmp)

        tmp_triangle_indices.extend(bottom_lip_triangle)

        # remove eyes
        left_eyes = np.arange(36, 42).tolist()
        selected_triangle_index = []
        for i, tmp_triangle in enumerate(tmp_triangle_indices):
            if (tmp_triangle[0] in left_eyes) & \
                    (tmp_triangle[1] in left_eyes) & \
                    (tmp_triangle[2] in left_eyes):
                continue
            else:
                selected_triangle_index.append(i)
        tmp_triangle_indices = np.asarray(
            tmp_triangle_indices)[selected_triangle_index]

        right_eyes = np.arange(42, 48).tolist()
        selected_triangle_index = []
        for i, tmp_triangle in enumerate(tmp_triangle_indices):
            if (tmp_triangle[0] in right_eyes) & \
                    (tmp_triangle[1] in right_eyes) & \
                    (tmp_triangle[2] in right_eyes):
                continue
            else:
                selected_triangle_index.append(i)
        tmp_triangle_indices = np.asarray(
            tmp_triangle_indices)[selected_triangle_index]

        np.savetxt('landmark_triangle_index.txt',
                   tmp_triangle_indices,
                   fmt='%i')
        triangles = face_segmentation.get_triangle_mesh(
            landmarks, tmp_triangle_indices)

        for triangle in triangles:
            cv2.polylines(image, [np.asarray(triangle)], True, (0, 255, 255))
        cv2.imwrite('test1.jpg', image)
        pass