Goals
In this tutorial will helps you to
- Create a Facemark object.
- Set a user defined face detector for the facemark algorithm
- Train the algorithm.
- Use the trained model to detect the facial landmarks from a given image.
Preparation
Before you continue with this tutorial, you should download the dataset of facial landmarks detection. We suggest you to download the helen dataset which can be retrieved at http://www.ifp.illinois.edu/~vuongle2/helen/ (Caution! The algorithm requires around 9GB of RAM to train on this dataset).
Make sure that the annotation format is supported by the API, the contents in annotation file should look like the following snippet:
The next thing to do is to make 2 text files containing the list of image files and annotation files respectively. Make sure that the order or image and annotation in both files are matched. Furthermore, it is advised to use absolute path instead of relative path. Example to make the file list in Linux machine
example of content in the images_train.txt
example of content in the annotation_train.txt
Creating the facemark object
Set a custom face detector function
Firstly, you need to create your own face detector function, you might also need to create a struct
to save the custom parameter. Alternatively, you can just make these parameter hard coded within the myDetector
function.
The following snippet demonstrates how to set the custom detector to the facemark object and use it to detect the faces. Keep in mind that some facemark object might use the face detector during the training process.
Here is the snippet for detecting face using the user defined face detector function.
Training a facemark object
- First of all, you need to set the training parameters params.n_landmarks = 68; // number of landmark pointsparams.initShape_n = 10; // number of multiplier for make data augmentationparams.stages_n=5; // amount of refinement stagesparams.tree_n=6; // number of tree in the model for each landmark pointparams.tree_depth=5; //he depth of decision treefacemark = FacemarkLBF::create(params);
- And then, you need to load the file list from the dataset that you have prepared. std::vector<String> images_train;std::vector<String> landmarks_train;loadDatasetList("images_train.txt","annotation_train.txt",images_train,landmarks_train);
- The next step is to add training samples into the facemark object. Mat image;std::vector<Point2f> facial_points;for(size_t i=0;i<images_train.size();i++){image = imread(images_train[i].c_str());loadFacePoints(landmarks_train[i],facial_points);facemark->addTrainingSample(image, facial_points);}
- execute the training process /*train the Algorithm*/facemark->training();
Use the trained model to detect the facial landmarks from a given image.
- First of all, load the trained model. You can also download the pre-trained model in this link https://raw.githubusercontent.com/kurnianggoro/GSOC2017/master/data/lbfmodel.yaml facemark->loadModel(params.model_filename);
- Detect the faces facemark->getFaces(img, faces, config);
- Perform the fitting process std::vector<std::vector<Point2f> > landmarks;facemark->fit(img, faces, landmarks);
- Display the result for(int j=0;j<faces.size();j++){face::drawFacemarks(img, landmarks[j], Scalar(0,0,255));}imshow("result", img);waitKey(0);