コード例 #1
0
def get_triangle(landmarks, landmark_mode=68):
    feature_index_dict = _get_facial_feature_landmarks(
        landmark_mode=landmark_mode)
    triangle_indices = list()
    triangle_mesh = list()
    for key in feature_index_dict.keys():
        index = feature_index_dict[key]
        tmp = np.array(landmarks)[index]
        _, triangles_tmp, triangle_mesh_tmp = face_segmentation.get_triangle_landmarks(
            tmp)

        if key == 'up_lip':
            bottom_contour = [0]
            bottom_contour.extend(np.arange(6, 12).tolist())
            selected_triangle_index = list()
            for i, triangle_tmp in enumerate(triangles_tmp):
                if (triangle_tmp[0] in bottom_contour) & \
                    (triangle_tmp[1] in bottom_contour) & \
                        (triangle_tmp[2] in bottom_contour):
                    continue
                selected_triangle_index.append(i)

            triangle_mesh_tmp = np.array(
                triangle_mesh_tmp)[selected_triangle_index]
            triangle_mesh_tmp = triangle_mesh_tmp.tolist()
        if key == 'bottom_lip':
            up_contour = np.arange(7, 12).tolist()
            selected_triangle_index = list()
            for i, triangle_tmp in enumerate(triangles_tmp):
                if (triangle_tmp[0] in up_contour) & \
                        (triangle_tmp[1] in up_contour) & \
                        (triangle_tmp[2] in up_contour):
                    continue
                selected_triangle_index.append(i)

            triangle_mesh_tmp = np.array(
                triangle_mesh_tmp)[selected_triangle_index]
            triangle_mesh_tmp = triangle_mesh_tmp.tolist()

            triangles_tmp = np.array(triangles_tmp)[selected_triangle_index]
            triangles_tmp = triangles_tmp.tolist()

        origin_triangles = list()
        for triangle_tmp in triangles_tmp:
            origin_triangles.append([
                index[triangle_tmp[0]], index[triangle_tmp[1]],
                index[triangle_tmp[2]]
            ])

        triangle_indices.extend(origin_triangles)
        triangle_mesh.extend(triangle_mesh_tmp)
    return np.array(triangle_indices), np.array(triangle_mesh)
コード例 #2
0
    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
コード例 #3
0
    def test_face_recognition(self):
        image_path = '../assets/examples/after-makeup2.jpeg'
        image = cv2.imread(image_path)
        landmarks = face_landmarks(image)[0]
        uplip = landmarks['top_lip']

        landmarks_coords, triangle_indices, triangles = face_segmentation.get_triangle_landmarks(
            uplip)
        tmp_triangle_indices = []
        # for triangle_index in triangle_indices:
        #     if (triangle_index[0] in inner_mouse) & \
        #        (triangle_index[1] in inner_mouse) & \
        #        (triangle_index[2] in inner_mouse):
        #         continue
        #     tmp_triangle_indices.append(triangle_index)

        # 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)
コード例 #4
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
コード例 #5
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