Skip to content

Commit e310fc5

Browse files
committed
face: refactoring
- change face detector interface - avoid using of legacy C-API defines - simplify CV_Error() - avoid using of legacy license headers
1 parent 7e9c532 commit e310fc5

File tree

14 files changed

+275
-558
lines changed

14 files changed

+275
-558
lines changed

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

Lines changed: 43 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,9 @@
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+
15
/*
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
6+
This file contains results of GSoC Project: Facemark API for OpenCV
327
Final report: https://gist.github.com/kurnianggoro/74de9121e122ad0bd825176751d47ecc
338
Student: Laksono Kurnianggoro
349
Mentor: Delia Passalacqua
@@ -45,29 +20,36 @@ Mentor: Delia Passalacqua
4520

4621
#include "opencv2/face.hpp"
4722
#include "opencv2/objdetect.hpp"
48-
#include "opencv2/objdetect/objdetect_c.h"
49-
#include "opencv2/imgproc/types_c.h"
23+
#include <vector>
24+
#include <string>
25+
5026

5127
namespace cv {
5228
namespace face {
5329

5430
//! @addtogroup face
5531
//! @{
56-
struct CV_EXPORTS_W CParams{
32+
33+
typedef bool(*FN_FaceDetector)(InputArray, OutputArray, void* userData);
34+
35+
struct CParams{
5736
String cascade; //!< the face detector
5837
double scaleFactor; //!< Parameter specifying how much the image size is reduced at each image scale.
5938
int minNeighbors; //!< Parameter specifying how many neighbors each candidate rectangle should have to retain it.
6039
Size minSize; //!< Minimum possible object size.
6140
Size maxSize; //!< Maximum possible object size.
6241

63-
CParams(
42+
CV_EXPORTS CParams(
6443
String cascade_model,
6544
double sf = 1.1,
6645
int minN = 3,
6746
Size minSz = Size(30, 30),
6847
Size maxSz = Size()
6948
);
49+
50+
CascadeClassifier face_cascade;
7051
};
52+
7153
/** @brief Default face detector
7254
This function is mainly utilized by the implementation of a Facemark Algorithm.
7355
End users are advised to use function Facemark::getFaces which can be manually defined
@@ -76,7 +58,7 @@ and circumvented to the algorithm by Facemark::setFaceDetector.
7658
@param image The input image to be processed.
7759
@param faces Output of the function which represent region of interest of the detected faces.
7860
Each face is stored in cv::Rect container.
79-
@param extra_params extra parameters
61+
@param params detector parameters
8062
8163
<B>Example of usage</B>
8264
@code
@@ -89,11 +71,7 @@ for(int j=0;j<faces.size();j++){
8971
cv::imshow("detection", frame);
9072
@endcode
9173
*/
92-
/*other option: move this function inside Facemark as default face detector*/
93-
CV_EXPORTS bool getFaces( InputArray image,
94-
OutputArray faces,
95-
void * extra_params
96-
);
74+
CV_EXPORTS bool getFaces(InputArray image, OutputArray faces, CParams* params);
9775

9876
/** @brief A utility to load list of paths to training image and annotation file.
9977
@param imageList The specified file contains paths to the training images.
@@ -109,7 +87,6 @@ std::vector<String> images_train;
10987
std::vector<String> landmarks_train;
11088
loadDatasetList(imageFiles,ptsFiles,images_train,landmarks_train);
11189
@endcode
112-
11390
*/
11491
CV_EXPORTS_W bool loadDatasetList(String imageList,
11592
String annotationList,
@@ -138,13 +115,12 @@ cv::String imageFiles = "../data/images_train.txt";
138115
cv::String ptsFiles = "../data/points_train.txt";
139116
std::vector<String> images;
140117
std::vector<std::vector<Point2f> > facePoints;
141-
loadTrainingData(imageFiles, ptsFiles, images, facePoints, 0.0);
118+
loadTrainingData(imageFiles, ptsFiles, images, facePoints, 0.0f);
142119
@endcode
143120
*/
144-
145121
CV_EXPORTS_W bool loadTrainingData( String filename , std::vector<String> & images,
146122
OutputArray facePoints,
147-
char delim = ' ', float offset = 0.0);
123+
char delim = ' ', float offset = 0.0f);
148124

149125
/** @brief A utility to load facial landmark information from the dataset.
150126
@@ -163,7 +139,7 @@ cv::String imageFiles = "../data/images_train.txt";
163139
cv::String ptsFiles = "../data/points_train.txt";
164140
std::vector<String> images;
165141
std::vector<std::vector<Point2f> > facePoints;
166-
loadTrainingData(imageFiles, ptsFiles, images, facePoints, 0.0);
142+
loadTrainingData(imageFiles, ptsFiles, images, facePoints, 0.0f);
167143
@endcode
168144
169145
example of content in the images_train.txt
@@ -182,11 +158,10 @@ example of content in the points_train.txt
182158
/home/user/ibug/image_006.pts
183159
@endcode
184160
*/
185-
186161
CV_EXPORTS_W bool loadTrainingData( String imageList, String groundTruth,
187162
std::vector<String> & images,
188163
OutputArray facePoints,
189-
float offset = 0.0);
164+
float offset = 0.0f);
190165

191166
/** @brief A utility to load facial landmark information from a given file.
192167
@@ -197,7 +172,7 @@ CV_EXPORTS_W bool loadTrainingData( String imageList, String groundTruth,
197172
<B>Example of usage</B>
198173
@code
199174
std::vector<Point2f> points;
200-
face::loadFacePoints("filename.txt", points, 0.0);
175+
face::loadFacePoints("filename.txt", points, 0.0f);
201176
@endcode
202177
203178
The annotation file should follow the default format which is
@@ -213,9 +188,8 @@ n_points: 68
213188
where n_points is the number of points considered
214189
and each point is represented as its position in x and y.
215190
*/
216-
217191
CV_EXPORTS_W bool loadFacePoints( String filename, OutputArray points,
218-
float offset = 0.0);
192+
float offset = 0.0f);
219193

220194
/** @brief Utility to draw the detected facial landmark points
221195
@@ -365,40 +339,42 @@ class CV_EXPORTS_W Facemark : public virtual Algorithm
365339
std::vector<std::vector<Point2f> > landmarks;
366340
facemark->fit(image, faces, landmarks);
367341
@endcode
342+
343+
TODO remove "config" from here
368344
*/
369-
virtual bool fit( InputArray image,\
370-
InputArray faces,\
371-
InputOutputArray landmarks,\
345+
virtual bool fit( InputArray image,
346+
InputArray faces,
347+
InputOutputArray landmarks,
372348
void * config = 0)=0;
373349

374350
/** @brief Set a user defined face detector for the Facemark algorithm.
375-
@param f The user defined face detector function
351+
@param detector The user defined face detector function
352+
@param userData Detector parameters
353+
376354
<B>Example of usage</B>
377355
@code
378-
facemark->setFaceDetector(myDetector);
356+
MyDetectorParameters detectorParameters(...);
357+
facemark->setFaceDetector(myDetector, &detectorParameters);
379358
@endcode
380359
381360
Example of a user defined face detector
382361
@code
383-
bool myDetector( InputArray image, OutputArray ROIs ){
384-
std::vector<Rect> & faces = *(std::vector<Rect>*) ROIs.getObj();
385-
faces.clear();
386-
387-
Mat img = image.getMat();
388-
362+
bool myDetector( InputArray image, OutputArray faces, void* userData)
363+
{
364+
MyDetectorParameters* params = (MyDetectorParameters*)userData;
389365
// -------- do something --------
390366
}
391367
@endcode
368+
369+
TODO Lifetime of detector parameters is uncontrolled. Rework interface design to "Ptr<FaceDetector>".
392370
*/
393-
virtual bool setFaceDetector(bool(*f)(InputArray , OutputArray, void * ))=0;
371+
virtual bool setFaceDetector(FN_FaceDetector detector, void* userData = 0)=0;
394372

395373
/** @brief Detect faces from a given image using default or user defined face detector.
396374
Some Algorithm might not provide a default face detector.
397375
398376
@param image Input image.
399-
@param faces Output of the function which represent region of interest of the detected faces.
400-
Each face is stored in cv::Rect container.
401-
@param extra_params Optional extra-parameters for the face detector function.
377+
@param faces Output of the function which represent region of interest of the detected faces. Each face is stored in cv::Rect container.
402378
403379
<B>Example of usage</B>
404380
@code
@@ -409,7 +385,7 @@ class CV_EXPORTS_W Facemark : public virtual Algorithm
409385
}
410386
@endcode
411387
*/
412-
virtual bool getFaces( InputArray image , OutputArray faces, void * extra_params=0)=0;
388+
virtual bool getFaces(InputArray image, OutputArray faces)=0;
413389

414390
/** @brief Get data from an algorithm
415391
@@ -427,13 +403,10 @@ class CV_EXPORTS_W Facemark : public virtual Algorithm
427403
cout<<s0<<endl;
428404
@endcode
429405
*/
430-
virtual bool getData(void * items=0)=0;
406+
virtual bool getData(void * items=0)=0; // FIXIT
431407
}; /* Facemark*/
432408

433409
//! @}
434-
435410
} /* namespace face */
436411
} /* namespace cv */
437-
438-
439412
#endif //__OPENCV_FACELANDMARK_HPP__

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

Lines changed: 7 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,9 @@
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+
15
/*
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
6+
This file contains results of GSoC Project: Facemark API for OpenCV
327
Final report: https://gist.github.com/kurnianggoro/74de9121e122ad0bd825176751d47ecc
338
Student: Laksono Kurnianggoro
349
Mentor: Delia Passalacqua
@@ -80,8 +55,8 @@ class CV_EXPORTS_W FacemarkAAM : public Facemark
8055
struct CV_EXPORTS Config
8156
{
8257
Config( Mat rot = Mat::eye(2,2,CV_32F),
83-
Point2f trans = Point2f(0.0,0.0),
84-
float scaling = 1.0,
58+
Point2f trans = Point2f(0.0f,0.0f),
59+
float scaling = 1.0f,
8560
int scale_id=0
8661
);
8762

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

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,9 @@
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+
15
/*
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
6+
This file contains results of GSoC Project: Facemark API for OpenCV
327
Final report: https://gist.github.com/kurnianggoro/74de9121e122ad0bd825176751d47ecc
338
Student: Laksono Kurnianggoro
349
Mentor: Delia Passalacqua

modules/face/samples/facemark_demo_aam.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ using namespace std;
5050
using namespace cv;
5151
using namespace cv::face;
5252

53-
bool myDetector( InputArray image, OutputArray ROIs, CascadeClassifier face_cascade);
53+
bool myDetector( InputArray image, OutputArray ROIs, CascadeClassifier *face_cascade);
5454
bool getInitialFitting(Mat image, Rect face, std::vector<Point2f> s0,
5555
CascadeClassifier eyes_cascade, Mat & R, Point2f & Trans, float & scale);
5656
bool parseArguments(int argc, char** argv, CommandLineParser & , String & cascade,
@@ -130,7 +130,7 @@ int main(int argc, char** argv )
130130
printf("image #%i ", i);
131131
//! [detect_face]
132132
image = imread(images[i]);
133-
myDetector(image, faces, face_cascade);
133+
myDetector(image, faces, &face_cascade);
134134
//! [detect_face]
135135
if(faces.size()>0){
136136
//! [get_initialization]
@@ -167,19 +167,20 @@ int main(int argc, char** argv )
167167
//! [fitting]
168168
}
169169

170-
bool myDetector( InputArray image, OutputArray ROIs, CascadeClassifier face_cascade){
170+
bool myDetector(InputArray image, OutputArray faces, CascadeClassifier *face_cascade)
171+
{
171172
Mat gray;
172-
std::vector<Rect> & faces = *(std::vector<Rect>*) ROIs.getObj();
173-
faces.clear();
174173

175-
if(image.channels()>1){
176-
cvtColor(image.getMat(),gray,CV_BGR2GRAY);
177-
}else{
174+
if (image.channels() > 1)
175+
cvtColor(image, gray, COLOR_BGR2GRAY);
176+
else
178177
gray = image.getMat().clone();
179-
}
180-
equalizeHist( gray, gray );
181178

182-
face_cascade.detectMultiScale( gray, faces, 1.2, 2, CV_HAAR_SCALE_IMAGE, Size(30, 30) );
179+
equalizeHist(gray, gray);
180+
181+
std::vector<Rect> faces_;
182+
face_cascade->detectMultiScale(gray, faces_, 1.4, 2, CASCADE_SCALE_IMAGE, Size(30, 30));
183+
Mat(faces_).copyTo(faces);
183184
return true;
184185
}
185186

@@ -201,7 +202,7 @@ bool getInitialFitting(Mat image, Rect face, std::vector<Point2f> s0 ,CascadeCla
201202
std::vector<Rect> eyes;
202203

203204
//-- In each face, detect eyes
204-
eyes_cascade.detectMultiScale( faceROI, eyes, 1.1, 2, 0 |CV_HAAR_SCALE_IMAGE, Size(20, 20) );
205+
eyes_cascade.detectMultiScale( faceROI, eyes, 1.1, 2, CASCADE_SCALE_IMAGE, Size(20, 20) );
205206
if(eyes.size()==2){
206207
found = true;
207208
int j=0;

0 commit comments

Comments
 (0)