Skip to content

Commit bccbec7

Browse files
IAmUnboundedalalek
authored andcommitted
Merge pull request #1199 from sukhad-app:face_alignment
Face alignment (#1199) * This commit will add a new functionality of one millisecond face_alignment to OpenCV. Face alignment is a computer vision technology for identifying the geometric structure of human faces in digital images. Given the location and size of a face, it automatically determines the shape of the face components such as eyes and nose. Added following functions : 1) Application to train a face landmark detector. 2) Application to detect face landmarks using a trained model. 3) Application to swap faces using face landmark detection 4) Application to detect landmarks in a video. Merged the code with a global facemark API. Added Doxygen Documentation for the Class created. Added tutorials for the samples added. Added visualisations depicting error rate and training time. Made desired changes fix fix fix fix fix fix fix fix fix * face: drop duplicated file -face_alignmentImpl.hpp +face_alignmentimpl.hpp * face: minor refactoring - replace license headers - fix usage of "precomp.hpp"
1 parent 004ac55 commit bccbec7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+3805
-752
lines changed

modules/face/CMakeLists.txt

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
11
set(the_description "Face recognition etc")
2-
ocv_define_module(face opencv_core opencv_imgproc opencv_objdetect WRAP python)
2+
ocv_define_module(face opencv_core
3+
opencv_imgproc
4+
opencv_objdetect
5+
opencv_tracking # estimateRigidTransform()
6+
opencv_photo # samples
7+
WRAP python
8+
)
39
# NOTE: objdetect module is needed for one of the samples
10+
11+
set(__commit_hash "8afa57abc8229d611c4937165d20e2a2d9fc5a12")
12+
set(__file_hash "7505c44ca4eb54b4ab1e4777cb96ac05")
13+
ocv_download(
14+
FILENAME face_landmark_model.dat
15+
HASH ${__file_hash}
16+
URL
17+
"${OPENCV_FACE_ALIGNMENT_URL}"
18+
"$ENV{OPENCV_FACE_ALIGNMENT_URL}"
19+
"https://raw.githubusercontent.com/opencv/opencv_3rdparty/${__commit_hash}/"
20+
DESTINATION_DIR "${CMAKE_BINARY_DIR}/${OPENCV_TEST_DATA_INSTALL_PATH}/cv/face/"
21+
ID "data"
22+
RELATIVE_URL
23+
STATUS res
24+
)
25+
if(NOT res)
26+
message(WARNING "Face: Can't get model file for face alignment.")
27+
endif()

modules/face/include/opencv2/face.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,4 +377,6 @@ class CV_EXPORTS_W FaceRecognizer : public Algorithm
377377
#include "opencv2/face/facemark.hpp"
378378
#include "opencv2/face/facemarkLBF.hpp"
379379
#include "opencv2/face/facemarkAAM.hpp"
380-
#endif
380+
#include "opencv2/face/face_alignment.hpp"
381+
382+
#endif // __OPENCV_FACE_HPP__
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// This file is part of OpenCV project.
2+
// It is subject to the license terms in the LICENSE file found in the top-level directory
3+
// of this distribution and at http://opencv.org/license.html.
4+
#ifndef __OPENCV_FACE_ALIGNMENT_HPP__
5+
#define __OPENCV_FACE_ALIGNMENT_HPP__
6+
7+
#include "facemark.hpp"
8+
9+
namespace cv{
10+
namespace face{
11+
class CV_EXPORTS_W FacemarkKazemi : public Algorithm
12+
{
13+
public:
14+
struct CV_EXPORTS Params
15+
{
16+
/**
17+
* \brief Constructor
18+
*/
19+
Params();
20+
/// cascade_depth This stores the deapth of cascade used for training.
21+
unsigned long cascade_depth;
22+
/// tree_depth This stores the max height of the regression tree built.
23+
unsigned long tree_depth;
24+
/// num_trees_per_cascade_level This stores number of trees fit per cascade level.
25+
unsigned long num_trees_per_cascade_level;
26+
/// learning_rate stores the learning rate in gradient boosting, also reffered as shrinkage.
27+
float learning_rate;
28+
/// oversampling_amount stores number of initialisations used to create training samples.
29+
unsigned long oversampling_amount;
30+
/// num_test_coordinates stores number of test coordinates.
31+
unsigned long num_test_coordinates;
32+
/// lambda stores a value to calculate probability of closeness of two coordinates.
33+
float lambda;
34+
/// num_test_splits stores number of random test splits generated.
35+
unsigned long num_test_splits;
36+
/// configfile stores the name of the file containing the values of training parameters
37+
String configfile;
38+
};
39+
static Ptr<FacemarkKazemi> create(const FacemarkKazemi::Params &parameters = FacemarkKazemi::Params());
40+
virtual ~FacemarkKazemi();
41+
42+
/// @brief training the facemark model, input are the file names of image list and landmark annotation
43+
virtual void training(String imageList, String groundTruth)=0;
44+
/** @brief This function is used to train the model using gradient boosting to get a cascade of regressors
45+
*which can then be used to predict shape.
46+
*@param images A vector of type cv::Mat which stores the images which are used in training samples.
47+
*@param landmarks A vector of vectors of type cv::Point2f which stores the landmarks detected in a particular image.
48+
*@param scale A size of type cv::Size to which all images and landmarks have to be scaled to.
49+
*@param configfile A variable of type std::string which stores the name of the file storing parameters for training the model.
50+
*@param modelFilename A variable of type std::string which stores the name of the trained model file that has to be saved.
51+
*@returns A boolean value. The function returns true if the model is trained properly or false if it is not trained.
52+
*/
53+
virtual bool training(std::vector<Mat>& images, std::vector< std::vector<Point2f> >& landmarks,std::string configfile,Size scale,std::string modelFilename = "face_landmarks.dat")=0;
54+
/** @brief This function is used to load the trained model..
55+
*@param filename A variable of type cv::String which stores the name of the file in which trained model is stored.
56+
*/
57+
virtual void loadModel(String filename)=0;
58+
/** @brief This functions retrieves a centered and scaled face shape, according to the bounding rectangle.
59+
*@param image A variable of type cv::InputArray which stores the image whose landmarks have to be found
60+
*@param faces A variable of type cv::InputArray which stores the bounding boxes of faces found in a given image.
61+
*@param landmarks A variable of type cv::InputOutputArray which stores the landmarks of all the faces found in the image
62+
*/
63+
virtual bool fit( InputArray image, InputArray faces, InputOutputArray landmarks )=0;//!< from many ROIs
64+
/// set the custom face detector
65+
virtual bool setFaceDetector(bool(*f)(InputArray , OutputArray, void*), void* userData)=0;
66+
/// get faces using the custom detector
67+
virtual bool getFaces(InputArray image, OutputArray faces)=0;
68+
};
69+
70+
}} // namespace
71+
#endif

modules/face/include/opencv2/face/facemark.hpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// of this distribution and at http://opencv.org/license.html.
44

55
/*
6-
This file contains results of GSoC Project: Facemark API for OpenCV
6+
This file was part of GSoC Project: Facemark API for OpenCV
77
Final report: https://gist.github.com/kurnianggoro/74de9121e122ad0bd825176751d47ecc
88
Student: Laksono Kurnianggoro
99
Mentor: Delia Passalacqua
@@ -73,6 +73,8 @@ cv::imshow("detection", frame);
7373
*/
7474
CV_EXPORTS bool getFaces(InputArray image, OutputArray faces, CParams* params);
7575

76+
CV_EXPORTS_W bool getFacesHAAR(InputArray image, OutputArray faces, const String& face_cascade_name);
77+
7678
/** @brief A utility to load list of paths to training image and annotation file.
7779
@param imageList The specified file contains paths to the training images.
7880
@param annotationList The specified file contains paths to the training annotations.
@@ -163,6 +165,25 @@ CV_EXPORTS_W bool loadTrainingData( String imageList, String groundTruth,
163165
OutputArray facePoints,
164166
float offset = 0.0f);
165167

168+
/** @brief This function extracts the data for training from .txt files which contains the corresponding image name and landmarks.
169+
*The first file in each file should give the path of the image whose
170+
*landmarks are being described in the file. Then in the subsequent
171+
*lines there should be coordinates of the landmarks in the image
172+
*i.e each line should be of the form x,y
173+
*where x represents the x coordinate of the landmark and y represents
174+
*the y coordinate of the landmark.
175+
*
176+
*For reference you can see the files as provided in the
177+
*<a href="http://www.ifp.illinois.edu/~vuongle2/helen/">HELEN dataset</a>
178+
*
179+
* @param filename A vector of type cv::String containing name of the .txt files.
180+
* @param trainlandmarks A vector of type cv::Point2f that would store shape or landmarks of all images.
181+
* @param trainimages A vector of type cv::String which stores the name of images whose landmarks are tracked
182+
* @returns A boolean value. It returns true when it reads the data successfully and false otherwise
183+
*/
184+
CV_EXPORTS_W bool loadTrainingData(std::vector<String> filename,std::vector< std::vector<Point2f> >
185+
&trainlandmarks,std::vector<String> & trainimages);
186+
166187
/** @brief A utility to load facial landmark information from a given file.
167188
168189
@param filename The filename of file contains the facial landmarks data.

modules/face/include/opencv2/face/facemarkAAM.hpp

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,34 @@
1-
// This file is part of OpenCV project.
2-
// It is subject to the license terms in the LICENSE file found in the top-level directory
3-
// of this distribution and at http://opencv.org/license.html.
4-
51
/*
6-
This file contains results of GSoC Project: Facemark API for OpenCV
2+
By downloading, copying, installing or using the software you agree to this
3+
license. If you do not agree to this license, do not download, install,
4+
copy or use the software.
5+
License Agreement
6+
For Open Source Computer Vision Library
7+
(3-clause BSD License)
8+
Copyright (C) 2013, OpenCV Foundation, all rights reserved.
9+
Third party copyrights are property of their respective owners.
10+
Redistribution and use in source and binary forms, with or without modification,
11+
are permitted provided that the following conditions are met:
12+
* Redistributions of source code must retain the above copyright notice,
13+
this list of conditions and the following disclaimer.
14+
* Redistributions in binary form must reproduce the above copyright notice,
15+
this list of conditions and the following disclaimer in the documentation
16+
and/or other materials provided with the distribution.
17+
* Neither the names of the copyright holders nor the names of the contributors
18+
may be used to endorse or promote products derived from this software
19+
without specific prior written permission.
20+
This software is provided by the copyright holders and contributors "as is" and
21+
any express or implied warranties, including, but not limited to, the implied
22+
warranties of merchantability and fitness for a particular purpose are
23+
disclaimed. In no event shall copyright holders or contributors be liable for
24+
any direct, indirect, incidental, special, exemplary, or consequential damages
25+
(including, but not limited to, procurement of substitute goods or services;
26+
loss of use, data, or profits; or business interruption) however caused
27+
and on any theory of liability, whether in contract, strict liability,
28+
or tort (including negligence or otherwise) arising in any way out of
29+
the use of this software, even if advised of the possibility of such damage.
30+
31+
This file was part of GSoC Project: Facemark API for OpenCV
732
Final report: https://gist.github.com/kurnianggoro/74de9121e122ad0bd825176751d47ecc
833
Student: Laksono Kurnianggoro
934
Mentor: Delia Passalacqua
@@ -55,7 +80,7 @@ class CV_EXPORTS_W FacemarkAAM : public Facemark
5580
struct CV_EXPORTS Config
5681
{
5782
Config( Mat rot = Mat::eye(2,2,CV_32F),
58-
Point2f trans = Point2f(0.0f,0.0f),
83+
Point2f trans = Point2f(0.0f, 0.0f),
5984
float scaling = 1.0f,
6085
int scale_id=0
6186
);

modules/face/include/opencv2/face/facemarkLBF.hpp

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,34 @@
1-
// This file is part of OpenCV project.
2-
// It is subject to the license terms in the LICENSE file found in the top-level directory
3-
// of this distribution and at http://opencv.org/license.html.
4-
51
/*
6-
This file contains results of GSoC Project: Facemark API for OpenCV
2+
By downloading, copying, installing or using the software you agree to this
3+
license. If you do not agree to this license, do not download, install,
4+
copy or use the software.
5+
License Agreement
6+
For Open Source Computer Vision Library
7+
(3-clause BSD License)
8+
Copyright (C) 2013, OpenCV Foundation, all rights reserved.
9+
Third party copyrights are property of their respective owners.
10+
Redistribution and use in source and binary forms, with or without modification,
11+
are permitted provided that the following conditions are met:
12+
* Redistributions of source code must retain the above copyright notice,
13+
this list of conditions and the following disclaimer.
14+
* Redistributions in binary form must reproduce the above copyright notice,
15+
this list of conditions and the following disclaimer in the documentation
16+
and/or other materials provided with the distribution.
17+
* Neither the names of the copyright holders nor the names of the contributors
18+
may be used to endorse or promote products derived from this software
19+
without specific prior written permission.
20+
This software is provided by the copyright holders and contributors "as is" and
21+
any express or implied warranties, including, but not limited to, the implied
22+
warranties of merchantability and fitness for a particular purpose are
23+
disclaimed. In no event shall copyright holders or contributors be liable for
24+
any direct, indirect, incidental, special, exemplary, or consequential damages
25+
(including, but not limited to, procurement of substitute goods or services;
26+
loss of use, data, or profits; or business interruption) however caused
27+
and on any theory of liability, whether in contract, strict liability,
28+
or tort (including negligence or otherwise) arising in any way out of
29+
the use of this software, even if advised of the possibility of such damage.
30+
31+
This file was part of GSoC Project: Facemark API for OpenCV
732
Final report: https://gist.github.com/kurnianggoro/74de9121e122ad0bd825176751d47ecc
833
Student: Laksono Kurnianggoro
934
Mentor: Delia Passalacqua

modules/face/samples/facemark_lbf_fitting.cpp

Lines changed: 61 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,34 @@
11
/*
2-
This file contains results of GSoC Project: Facemark API for OpenCV
2+
By downloading, copying, installing or using the software you agree to this
3+
license. If you do not agree to this license, do not download, install,
4+
copy or use the software.
5+
License Agreement
6+
For Open Source Computer Vision Library
7+
(3-clause BSD License)
8+
Copyright (C) 2013, OpenCV Foundation, all rights reserved.
9+
Third party copyrights are property of their respective owners.
10+
Redistribution and use in source and binary forms, with or without modification,
11+
are permitted provided that the following conditions are met:
12+
* Redistributions of source code must retain the above copyright notice,
13+
this list of conditions and the following disclaimer.
14+
* Redistributions in binary form must reproduce the above copyright notice,
15+
this list of conditions and the following disclaimer in the documentation
16+
and/or other materials provided with the distribution.
17+
* Neither the names of the copyright holders nor the names of the contributors
18+
may be used to endorse or promote products derived from this software
19+
without specific prior written permission.
20+
This software is provided by the copyright holders and contributors "as is" and
21+
any express or implied warranties, including, but not limited to, the implied
22+
warranties of merchantability and fitness for a particular purpose are
23+
disclaimed. In no event shall copyright holders or contributors be liable for
24+
any direct, indirect, incidental, special, exemplary, or consequential damages
25+
(including, but not limited to, procurement of substitute goods or services;
26+
loss of use, data, or profits; or business interruption) however caused
27+
and on any theory of liability, whether in contract, strict liability,
28+
or tort (including negligence or otherwise) arising in any way out of
29+
the use of this software, even if advised of the possibility of such damage.
30+
31+
This file was part of GSoC Project: Facemark API for OpenCV
332
Final report: https://gist.github.com/kurnianggoro/74de9121e122ad0bd825176751d47ecc
433
Student: Laksono Kurnianggoro
534
Mentor: Delia Passalacqua
@@ -18,7 +47,7 @@ Mentor: Delia Passalacqua
1847

1948
#include <stdio.h>
2049
#include <ctime>
21-
#include <iostream>
50+
#include <iostream>
2251
#include "opencv2/core.hpp"
2352
#include "opencv2/highgui.hpp"
2453
#include "opencv2/imgproc.hpp"
@@ -137,34 +166,34 @@ bool parseArguments(int argc, char** argv, CommandLineParser & parser,
137166
String & model,
138167
String & video
139168
){
140-
const String keys =
141-
"{ @c cascade | | (required) path to the cascade model file for the face detector }"
142-
"{ @m model | | (required) path to the trained model }"
143-
"{ @v video | | (required) path input video}"
144-
"{ help h usage ? | | facemark_lbf_fitting -cascade -model -video [-t]\n"
145-
" example: facemark_lbf_fitting ../face_cascade.xml ../LBF.model ../video.mp4}"
146-
;
147-
parser = CommandLineParser(argc, argv,keys);
148-
parser.about("hello");
149-
150-
if (parser.has("help")){
151-
parser.printMessage();
152-
return false;
153-
}
154-
155-
cascade = String(parser.get<String>("cascade"));
156-
model = String(parser.get<string>("model"));
157-
video = String(parser.get<string>("video"));
158-
159-
160-
if(cascade.empty() || model.empty() || video.empty() ){
161-
std::cerr << "one or more required arguments are not found" << '\n';
162-
cout<<"cascade : "<<cascade.c_str()<<endl;
163-
cout<<"model : "<<model.c_str()<<endl;
164-
cout<<"video : "<<video.c_str()<<endl;
165-
parser.printMessage();
166-
return false;
167-
}
168-
169-
return true;
169+
const String keys =
170+
"{ @c cascade | | (required) path to the cascade model file for the face detector }"
171+
"{ @m model | | (required) path to the trained model }"
172+
"{ @v video | | (required) path input video}"
173+
"{ help h usage ? | | facemark_lbf_fitting -cascade -model -video [-t]\n"
174+
" example: facemark_lbf_fitting ../face_cascade.xml ../LBF.model ../video.mp4}"
175+
;
176+
parser = CommandLineParser(argc, argv,keys);
177+
parser.about("hello");
178+
179+
if (parser.has("help")){
180+
parser.printMessage();
181+
return false;
182+
}
183+
184+
cascade = String(parser.get<String>("cascade"));
185+
model = String(parser.get<string>("model"));
186+
video = String(parser.get<string>("video"));
187+
188+
189+
if(cascade.empty() || model.empty() || video.empty() ){
190+
std::cerr << "one or more required arguments are not found" << '\n';
191+
cout<<"cascade : "<<cascade.c_str()<<endl;
192+
cout<<"model : "<<model.c_str()<<endl;
193+
cout<<"video : "<<video.c_str()<<endl;
194+
parser.printMessage();
195+
return false;
196+
}
197+
198+
return true;
170199
}

0 commit comments

Comments
 (0)