OpenCV
3.4.19
Open Source Computer Vision
|
This application lets you swap a face in one image with another face in other image. The application first detects faces in both images and finds its landmarks. Then it swaps the face in first image with in another image. You just have to give paths to the images run the application to swap the two faces.
- image1 i1 (REQUIRED) Path to the first image file in which you want to apply swapping.
- image2 i2 (REQUIRED) Path to the second image file in which you want to apply face swapping.
- model m (REQUIRED) Path to the file containing model to be loaded for face landmark detection.
- face_cascade f (REQUIRED) Path to the face cascade xml file which you want to use as a face detector.
This tutorial will explain the sample code for face swapping using OpenCV. Jumping directly to the code :
The facemark API provides the functionality to the user to use their own face detector to be used in face landmark detection.The above code creartes a sample face detector. The above function would be passed to a function pointer in the facemark API.
The above code creates a pointer of the face landmark detection class. The face detector created above has to be passed as function pointer to the facemark pointer created for detecting faces while training the model.
The above code creates vectors to store the detected faces and a vector of vector to store shapes for each face detected in both the images.It then detects landmarks of each face detected in both the images.the images are resized as it is easier to process small images. The images are resized according their actual ratio.
The above code then finds convex hull to find the boundary points of the face in the image which has to be swapped.
Now as we need to warp one face over the other and we need to find affine transform. Now as the function in OpenCV to find affine transform requires three set of points to calculate the affine matrix. Also we just need to warp the face instead of the surrounding regions. Hence we divide the face into triangles so that each triiangle can be easily warped onto the other image.
The function divideIntoTriangles divides the detected faces into triangles. The function warpTriangle then warps each triangle of one image to other image to swap the faces.
Even after warping the results somehow look unnatural. Hence to improve the results we apply seamless cloning to get the desired results as required.
Consider two images to be used for face swapping as follows :