Swap faces – Face swapping Opencv with Python (part 5)

We’re going to see in this video the “core” of face swapping, where we finally swap both faces and we can see the first interesting results.

We will focuso on this tutorial on the explanation of only the swapping part, if you want to understand all the process from the beginning, go and check the entire tutorial.

We added on line 20 a black image with the same size and channels as image 2. This is the image where we’re going to put the swapped face later.

img2_new_face = np.zeros_like(img2)

After we warp the triangles from the first image, we reconstruct the first face warped with same dimensions and position as the second face.

	# Reconstructing destination face
	img2_new_face_rect_area = img2_new_face[y: y + h, x: x + w]
	img2_new_face_rect_area = cv2.add(img2_new_face_rect_area, warped_triangle)
	img2_new_face[y: y + h, x: x + w] = img2_new_face_rect_area
swap faces

Once the face is reconstructed combining the triangles we create a mask to select the face and the black are, so that we can remove the original face from img2.

# Face swapped (putting 1st face into 2nd face)
img2_face_mask = np.zeros_like(img2_gray)
img2_head_mask = cv2.fillConvexPoly(img2_face_mask, convexhull2, 255)
img2_face_mask = cv2.bitwise_not(img2_head_mask)

Once we have the mask we extract everything from image 2 except the face.

img2_head_noface = cv2.bitwise_and(img2, img2, mask=img2_face_mask)

We finally add together img2_head_no_face and img2_new_face.

result = cv2.add(img2_head_noface, img2_new_face)

Learn to build Computer Vision Software easily and efficiently.

This is a FREE Workshop where I'm going to break down the 4 steps that are necessary to build software to detect and track any object.

Sign UP for FREE