/*
By downloading, copying, installing or using the software you agree to this
license. If you do not agree to this license, do not download, install,
copy or use the software.
License Agreement
For Open Source Computer Vision Library
(3-clause BSD License)
Copyright (C) 2013, OpenCV Foundation, all rights reserved.
Third party copyrights are property of their respective owners.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the names of the copyright holders nor the names of the contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
This software is provided by the copyright holders and contributors "as is" and
any express or implied warranties, including, but not limited to, the implied
warranties of merchantability and fitness for a particular purpose are
disclaimed. In no event shall copyright holders or contributors be liable for
any direct, indirect, incidental, special, exemplary, or consequential damages
(including, but not limited to, procurement of substitute goods or services;
loss of use, data, or profits; or business interruption) however caused
and on any theory of liability, whether in contract, strict liability,
or tort (including negligence or otherwise) arising in any way out of
the use of this software, even if advised of the possibility of such damage.
This file was part of GSoC Project: Facemark API for OpenCV
Final report: https://gist.github.com/kurnianggoro/74de9121e122ad0bd825176751d47ecc
Student: Laksono Kurnianggoro
Mentor: Delia Passalacqua
*/
#include "precomp.hpp"
#include "../face.hpp"
#include <fstream>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <cstdarg>
namespace cv {
namespace face {
#define TIMER_BEGIN { double __time__ = (double)getTickCount();
#define TIMER_NOW ((getTickCount() - __time__) / getTickFrequency())
#define TIMER_END }
#define SIMILARITY_TRANSFORM(x, y, scale, rotate) do { \
double x_tmp = scale * (rotate(0, 0)*x + rotate(0, 1)*y); \
double y_tmp = scale * (rotate(1, 0)*x + rotate(1, 1)*y); \
x = x_tmp; y = y_tmp; \
} while(0)
FacemarkLBF::Params::Params(){
cascade_face = "";
shape_offset = 0.0;
n_landmarks = 68;
initShape_n = 10;
stages_n=5;
tree_n=6;
tree_depth=5;
bagging_overlap = 0.4;
model_filename = "";
save_model = true;
verbose = true;
seed = 0;
int _pupils[][6] = { { 36, 37, 38, 39, 40, 41 }, { 42, 43, 44, 45, 46, 47 } };
for (int i = 0; i < 6; i++) {
pupils[0].push_back(_pupils[0][i]);
pupils[1].push_back(_pupils[1][i]);
}
int _feats_m[] = { 500, 500, 500, 300, 300, 300, 200, 200, 200, 100 };
double _radius_m[] = { 0.3, 0.2, 0.15, 0.12, 0.10, 0.10, 0.08, 0.06, 0.06, 0.05 };
for (int i = 0; i < 10; i++) {
feats_m.push_back(_feats_m[i]);
radius_m.push_back(_radius_m[i]);
}
detectROI = Rect(-1,-1,-1,-1);
}
void FacemarkLBF::Params::read( const cv::FileNode& fn ){
*this = FacemarkLBF::Params();
if (!fn["verbose"].empty())
fn["verbose"] >> verbose;
}
void FacemarkLBF::Params::write( cv::FileStorage& fs ) const{
fs << "verbose" << verbose;
}
class FacemarkLBFImpl : public FacemarkLBF {
public:
FacemarkLBFImpl( const FacemarkLBF::Params ¶meters = FacemarkLBF::Params() );
void read( const FileNode& /*fn*/ ) CV_OVERRIDE;
void write( FileStorage& /*fs*/ ) const CV_OVERRIDE;
void loadModel(String fs) CV_OVERRIDE;
bool setFaceDetector(bool(*f)(InputArray , OutputArray, void * extra_params ), void* userData) CV_OVERRIDE;
bool getFaces(InputArray image, OutputArray faces) CV_OVERRIDE;
bool getData(void * items) CV_OVERRIDE;
Params params;
protected:
bool fit( InputArray image, InputArray faces, OutputArrayOfArrays landmarks ) CV_OVERRIDE;//!< from many ROIs
bool fitImpl( const Mat image, std::vector<Point2f> & landmarks );//!< from a face
bool addTrainingSample(InputArray image, InputArray landmarks) CV_OVERRIDE;
void training(void* parameters) CV_OVERRIDE;
Rect getBBox(Mat &img, const Mat_<double> shape);
void prepareTrainingData(Mat img, std::vector<Point2f> facePoints,
std::vector<Mat> & cropped, std::vector<Mat> & shapes, std::vector<BBox> &boxes);
void data_augmentation(std::vector<Mat> &imgs, std::vector<Mat> >_shapes, std::vector<BBox> &bboxes);
Mat getMeanShape(std::vector<Mat> >_shapes, std::vector<BBox> &bboxes);
bool defaultFaceDetector(const Mat& image, std::vector<Rect>& faces);
CascadeClassifier face_cascade;
FN_FaceDetector faceDetector;
void* faceDetectorData;
/*training data*/
std::vector<std::vector<Point2f> > data_facemarks; //original position
std::vector<Mat> data_faces; //face ROI
std::vector<BBox> data_boxes;
std::vector<Mat> data_shapes; //position in the face ROI
private:
bool isModelTrained;
/*---------------LBF Class---------------------*/
class LBF {
public:
void calcSimilarityTransform(const Mat &shape1, const Mat &shape2, double &scale, Mat &rotate);
std::vector<Mat> getDeltaShapes(std::vector<Mat> >_shapes, std::vector<Mat> ¤t_shapes,
std::vector<BBox> &bboxes, Mat &mean_shape);
double calcVariance(const Mat &vec);
double calcVariance(const std::vector<double> &vec);
double calcMeanError(std::vector<Mat> >_shapes, std::vector<Mat> ¤t_shapes, int landmark_n , std::vector<int> &left, std::vector<int> &right );
};
/*---------------RandomTree Class---------------------*/
class RandomTree : public LBF {
public:
RandomTree(){};
~RandomTree(){};
void initTree(int landmark_id, int depth, std::vector<int>, std::vector<double>);
void train(std::vector<Mat> &imgs, std::vector<Mat> ¤t_shapes, std::vector<BBox> &bboxes,
std::vector<Mat> &delta_shapes, Mat &mean_shape, std::vector<int> &index, int stage);
void splitNode(std::vector<cv::Mat> &imgs, std::vector<cv::Mat> ¤t_shapes, std::vector<BBox> &bboxes,
cv::Mat &delta_shapes, cv::Mat &mean_shape, std::vector<int> &root, int idx, int stage);
void write(FileStorage fs, int forestId, int i, int j);
void read(FileStorage fs, int forestId, int i, int j);
int depth;
int nodes_n;
int landmark_id;
cv::Mat_<double> feats;
std::vector<int> thresholds;
std::vector<int> params_feats_m;
std::vector<double> params_radius_m;
};
/*---------------RandomForest Class---------------------*/
class RandomForest : public LBF {
public:
RandomForest(){};
~RandomForest(){};
void initForest(int landmark_n, int trees_n, int tree_depth, double , std::vector<int>, std::vector<double>, bool);
void train(std::vector<cv::Mat> &imgs, std::vector<cv::Mat> ¤t_shapes, \
std::vector<BBox> &bboxes, std::vector<cv::Mat> &delta_shapes, cv::Mat &mean_shape, int stage);
Mat generateLBF(Mat &img, Mat ¤t_shape, BBox &bbox, Mat &mean_shape);
void write(FileStorage fs, int forestId);
void read(FileStorage fs, int forestId);
bool verbose;
int landmark_n;
int trees_n, tree_depth;
double overlap_ratio;
std::vector<