Exemplo n.º 1
0
def faceswap(face_image, face_data, target_image, target_info,
             feather_amount=17, color_correct_blur_frac=0.8):
    """
    Perform a face swap where face_image and face_landmarks are the faces and
    face landmark points for the desired.  Target image and target landmarks
    point to the face to be changed. Feather amount is the percentage of the
    face to feather the face-mask for a nicer transition and
    color_correct_blur_frac is the amount of blur to use during color
    correction, as a fraction of the pupillary distance.

    'aligned_face face_box landmarks imghash'.split()
    """
    M = transformation_from_points(
        target_info.landmarks[ALIGN_POINTS],
        face_data['landmarks'][ALIGN_POINTS]
    )

    feather_target = feather_amount
    feather_face = feather_amount

    mask = get_face_mask(
        face_image,
        face_data['landmarks'],
        feather_face
    )
    warped_mask = warp_im(mask, M, target_image.shape)
    combined_mask = np.max([
        get_face_mask(target_image, target_info.landmarks, feather_target),
        warped_mask
    ], axis=0)

    warped_target_image = warp_im(face_image, M, target_image.shape)

    warped_corrected_target_image = correct_colours(
        target_image,
        warped_target_image,
        face_data['landmarks'],
        color_correct_blur_frac
    )

    output_im = target_image * (1.0 - combined_mask) + \
        warped_corrected_target_image * combined_mask
    return output_im
def _normalize(image, image_size, face_box, landmarks):
    H = transformation_from_points(landmarks, ALIGNED_LANDMARKS)
    return cv2.warpAffine(image, H[:2], (image_size, image_size), flags=cv2.INTER_AREA)