Swapping faces
Here comes the last step of the deepfake pipeline, but let's first recap the pipeline. The deepfake production pipeline involves three main stages:
- Extract a face from an image using
dlib
and OpenCV. - Translate the face using the trained encoder and decoders.
- Swap the new face back into the original image.
The new face generated by the autoencoder is an aligned face of size 64×64, so we will need to warp it to the position, size, and angle of the face in the original image. We'll use the affine matrix obtained from step 1 in the face extraction stage. We'll use cv2.warpAffine
like before, but this time, the cv2.WARP_INVERSE_MAP
flag is used to reverse the direction of image transformation as follows:
h, w, _ = image.shape size = 64 new_image = np.zeros_like(image, dtype=np.uint8) new_image = cv2.warpAffine(np.array(new_face, dtype=np.uint8) ...