Merged facerec from the trunk

This commit is contained in:
Andrey Kamaev
2012-07-02 11:48:16 +00:00
parent b368f99d03
commit 3a40f27655
54 changed files with 3888 additions and 237 deletions

View File

@@ -0,0 +1,400 @@
/path/to/at/s13/2.pgm;12
/path/to/at/s13/7.pgm;12
/path/to/at/s13/6.pgm;12
/path/to/at/s13/9.pgm;12
/path/to/at/s13/5.pgm;12
/path/to/at/s13/3.pgm;12
/path/to/at/s13/4.pgm;12
/path/to/at/s13/10.pgm;12
/path/to/at/s13/8.pgm;12
/path/to/at/s13/1.pgm;12
/path/to/at/s17/2.pgm;16
/path/to/at/s17/7.pgm;16
/path/to/at/s17/6.pgm;16
/path/to/at/s17/9.pgm;16
/path/to/at/s17/5.pgm;16
/path/to/at/s17/3.pgm;16
/path/to/at/s17/4.pgm;16
/path/to/at/s17/10.pgm;16
/path/to/at/s17/8.pgm;16
/path/to/at/s17/1.pgm;16
/path/to/at/s32/2.pgm;31
/path/to/at/s32/7.pgm;31
/path/to/at/s32/6.pgm;31
/path/to/at/s32/9.pgm;31
/path/to/at/s32/5.pgm;31
/path/to/at/s32/3.pgm;31
/path/to/at/s32/4.pgm;31
/path/to/at/s32/10.pgm;31
/path/to/at/s32/8.pgm;31
/path/to/at/s32/1.pgm;31
/path/to/at/s10/2.pgm;9
/path/to/at/s10/7.pgm;9
/path/to/at/s10/6.pgm;9
/path/to/at/s10/9.pgm;9
/path/to/at/s10/5.pgm;9
/path/to/at/s10/3.pgm;9
/path/to/at/s10/4.pgm;9
/path/to/at/s10/10.pgm;9
/path/to/at/s10/8.pgm;9
/path/to/at/s10/1.pgm;9
/path/to/at/s27/2.pgm;26
/path/to/at/s27/7.pgm;26
/path/to/at/s27/6.pgm;26
/path/to/at/s27/9.pgm;26
/path/to/at/s27/5.pgm;26
/path/to/at/s27/3.pgm;26
/path/to/at/s27/4.pgm;26
/path/to/at/s27/10.pgm;26
/path/to/at/s27/8.pgm;26
/path/to/at/s27/1.pgm;26
/path/to/at/s5/2.pgm;4
/path/to/at/s5/7.pgm;4
/path/to/at/s5/6.pgm;4
/path/to/at/s5/9.pgm;4
/path/to/at/s5/5.pgm;4
/path/to/at/s5/3.pgm;4
/path/to/at/s5/4.pgm;4
/path/to/at/s5/10.pgm;4
/path/to/at/s5/8.pgm;4
/path/to/at/s5/1.pgm;4
/path/to/at/s20/2.pgm;19
/path/to/at/s20/7.pgm;19
/path/to/at/s20/6.pgm;19
/path/to/at/s20/9.pgm;19
/path/to/at/s20/5.pgm;19
/path/to/at/s20/3.pgm;19
/path/to/at/s20/4.pgm;19
/path/to/at/s20/10.pgm;19
/path/to/at/s20/8.pgm;19
/path/to/at/s20/1.pgm;19
/path/to/at/s30/2.pgm;29
/path/to/at/s30/7.pgm;29
/path/to/at/s30/6.pgm;29
/path/to/at/s30/9.pgm;29
/path/to/at/s30/5.pgm;29
/path/to/at/s30/3.pgm;29
/path/to/at/s30/4.pgm;29
/path/to/at/s30/10.pgm;29
/path/to/at/s30/8.pgm;29
/path/to/at/s30/1.pgm;29
/path/to/at/s39/2.pgm;38
/path/to/at/s39/7.pgm;38
/path/to/at/s39/6.pgm;38
/path/to/at/s39/9.pgm;38
/path/to/at/s39/5.pgm;38
/path/to/at/s39/3.pgm;38
/path/to/at/s39/4.pgm;38
/path/to/at/s39/10.pgm;38
/path/to/at/s39/8.pgm;38
/path/to/at/s39/1.pgm;38
/path/to/at/s35/2.pgm;34
/path/to/at/s35/7.pgm;34
/path/to/at/s35/6.pgm;34
/path/to/at/s35/9.pgm;34
/path/to/at/s35/5.pgm;34
/path/to/at/s35/3.pgm;34
/path/to/at/s35/4.pgm;34
/path/to/at/s35/10.pgm;34
/path/to/at/s35/8.pgm;34
/path/to/at/s35/1.pgm;34
/path/to/at/s23/2.pgm;22
/path/to/at/s23/7.pgm;22
/path/to/at/s23/6.pgm;22
/path/to/at/s23/9.pgm;22
/path/to/at/s23/5.pgm;22
/path/to/at/s23/3.pgm;22
/path/to/at/s23/4.pgm;22
/path/to/at/s23/10.pgm;22
/path/to/at/s23/8.pgm;22
/path/to/at/s23/1.pgm;22
/path/to/at/s4/2.pgm;3
/path/to/at/s4/7.pgm;3
/path/to/at/s4/6.pgm;3
/path/to/at/s4/9.pgm;3
/path/to/at/s4/5.pgm;3
/path/to/at/s4/3.pgm;3
/path/to/at/s4/4.pgm;3
/path/to/at/s4/10.pgm;3
/path/to/at/s4/8.pgm;3
/path/to/at/s4/1.pgm;3
/path/to/at/s9/2.pgm;8
/path/to/at/s9/7.pgm;8
/path/to/at/s9/6.pgm;8
/path/to/at/s9/9.pgm;8
/path/to/at/s9/5.pgm;8
/path/to/at/s9/3.pgm;8
/path/to/at/s9/4.pgm;8
/path/to/at/s9/10.pgm;8
/path/to/at/s9/8.pgm;8
/path/to/at/s9/1.pgm;8
/path/to/at/s37/2.pgm;36
/path/to/at/s37/7.pgm;36
/path/to/at/s37/6.pgm;36
/path/to/at/s37/9.pgm;36
/path/to/at/s37/5.pgm;36
/path/to/at/s37/3.pgm;36
/path/to/at/s37/4.pgm;36
/path/to/at/s37/10.pgm;36
/path/to/at/s37/8.pgm;36
/path/to/at/s37/1.pgm;36
/path/to/at/s24/2.pgm;23
/path/to/at/s24/7.pgm;23
/path/to/at/s24/6.pgm;23
/path/to/at/s24/9.pgm;23
/path/to/at/s24/5.pgm;23
/path/to/at/s24/3.pgm;23
/path/to/at/s24/4.pgm;23
/path/to/at/s24/10.pgm;23
/path/to/at/s24/8.pgm;23
/path/to/at/s24/1.pgm;23
/path/to/at/s19/2.pgm;18
/path/to/at/s19/7.pgm;18
/path/to/at/s19/6.pgm;18
/path/to/at/s19/9.pgm;18
/path/to/at/s19/5.pgm;18
/path/to/at/s19/3.pgm;18
/path/to/at/s19/4.pgm;18
/path/to/at/s19/10.pgm;18
/path/to/at/s19/8.pgm;18
/path/to/at/s19/1.pgm;18
/path/to/at/s8/2.pgm;7
/path/to/at/s8/7.pgm;7
/path/to/at/s8/6.pgm;7
/path/to/at/s8/9.pgm;7
/path/to/at/s8/5.pgm;7
/path/to/at/s8/3.pgm;7
/path/to/at/s8/4.pgm;7
/path/to/at/s8/10.pgm;7
/path/to/at/s8/8.pgm;7
/path/to/at/s8/1.pgm;7
/path/to/at/s21/2.pgm;20
/path/to/at/s21/7.pgm;20
/path/to/at/s21/6.pgm;20
/path/to/at/s21/9.pgm;20
/path/to/at/s21/5.pgm;20
/path/to/at/s21/3.pgm;20
/path/to/at/s21/4.pgm;20
/path/to/at/s21/10.pgm;20
/path/to/at/s21/8.pgm;20
/path/to/at/s21/1.pgm;20
/path/to/at/s1/2.pgm;0
/path/to/at/s1/7.pgm;0
/path/to/at/s1/6.pgm;0
/path/to/at/s1/9.pgm;0
/path/to/at/s1/5.pgm;0
/path/to/at/s1/3.pgm;0
/path/to/at/s1/4.pgm;0
/path/to/at/s1/10.pgm;0
/path/to/at/s1/8.pgm;0
/path/to/at/s1/1.pgm;0
/path/to/at/s7/2.pgm;6
/path/to/at/s7/7.pgm;6
/path/to/at/s7/6.pgm;6
/path/to/at/s7/9.pgm;6
/path/to/at/s7/5.pgm;6
/path/to/at/s7/3.pgm;6
/path/to/at/s7/4.pgm;6
/path/to/at/s7/10.pgm;6
/path/to/at/s7/8.pgm;6
/path/to/at/s7/1.pgm;6
/path/to/at/s16/2.pgm;15
/path/to/at/s16/7.pgm;15
/path/to/at/s16/6.pgm;15
/path/to/at/s16/9.pgm;15
/path/to/at/s16/5.pgm;15
/path/to/at/s16/3.pgm;15
/path/to/at/s16/4.pgm;15
/path/to/at/s16/10.pgm;15
/path/to/at/s16/8.pgm;15
/path/to/at/s16/1.pgm;15
/path/to/at/s36/2.pgm;35
/path/to/at/s36/7.pgm;35
/path/to/at/s36/6.pgm;35
/path/to/at/s36/9.pgm;35
/path/to/at/s36/5.pgm;35
/path/to/at/s36/3.pgm;35
/path/to/at/s36/4.pgm;35
/path/to/at/s36/10.pgm;35
/path/to/at/s36/8.pgm;35
/path/to/at/s36/1.pgm;35
/path/to/at/s25/2.pgm;24
/path/to/at/s25/7.pgm;24
/path/to/at/s25/6.pgm;24
/path/to/at/s25/9.pgm;24
/path/to/at/s25/5.pgm;24
/path/to/at/s25/3.pgm;24
/path/to/at/s25/4.pgm;24
/path/to/at/s25/10.pgm;24
/path/to/at/s25/8.pgm;24
/path/to/at/s25/1.pgm;24
/path/to/at/s14/2.pgm;13
/path/to/at/s14/7.pgm;13
/path/to/at/s14/6.pgm;13
/path/to/at/s14/9.pgm;13
/path/to/at/s14/5.pgm;13
/path/to/at/s14/3.pgm;13
/path/to/at/s14/4.pgm;13
/path/to/at/s14/10.pgm;13
/path/to/at/s14/8.pgm;13
/path/to/at/s14/1.pgm;13
/path/to/at/s34/2.pgm;33
/path/to/at/s34/7.pgm;33
/path/to/at/s34/6.pgm;33
/path/to/at/s34/9.pgm;33
/path/to/at/s34/5.pgm;33
/path/to/at/s34/3.pgm;33
/path/to/at/s34/4.pgm;33
/path/to/at/s34/10.pgm;33
/path/to/at/s34/8.pgm;33
/path/to/at/s34/1.pgm;33
/path/to/at/s11/2.pgm;10
/path/to/at/s11/7.pgm;10
/path/to/at/s11/6.pgm;10
/path/to/at/s11/9.pgm;10
/path/to/at/s11/5.pgm;10
/path/to/at/s11/3.pgm;10
/path/to/at/s11/4.pgm;10
/path/to/at/s11/10.pgm;10
/path/to/at/s11/8.pgm;10
/path/to/at/s11/1.pgm;10
/path/to/at/s26/2.pgm;25
/path/to/at/s26/7.pgm;25
/path/to/at/s26/6.pgm;25
/path/to/at/s26/9.pgm;25
/path/to/at/s26/5.pgm;25
/path/to/at/s26/3.pgm;25
/path/to/at/s26/4.pgm;25
/path/to/at/s26/10.pgm;25
/path/to/at/s26/8.pgm;25
/path/to/at/s26/1.pgm;25
/path/to/at/s18/2.pgm;17
/path/to/at/s18/7.pgm;17
/path/to/at/s18/6.pgm;17
/path/to/at/s18/9.pgm;17
/path/to/at/s18/5.pgm;17
/path/to/at/s18/3.pgm;17
/path/to/at/s18/4.pgm;17
/path/to/at/s18/10.pgm;17
/path/to/at/s18/8.pgm;17
/path/to/at/s18/1.pgm;17
/path/to/at/s29/2.pgm;28
/path/to/at/s29/7.pgm;28
/path/to/at/s29/6.pgm;28
/path/to/at/s29/9.pgm;28
/path/to/at/s29/5.pgm;28
/path/to/at/s29/3.pgm;28
/path/to/at/s29/4.pgm;28
/path/to/at/s29/10.pgm;28
/path/to/at/s29/8.pgm;28
/path/to/at/s29/1.pgm;28
/path/to/at/s33/2.pgm;32
/path/to/at/s33/7.pgm;32
/path/to/at/s33/6.pgm;32
/path/to/at/s33/9.pgm;32
/path/to/at/s33/5.pgm;32
/path/to/at/s33/3.pgm;32
/path/to/at/s33/4.pgm;32
/path/to/at/s33/10.pgm;32
/path/to/at/s33/8.pgm;32
/path/to/at/s33/1.pgm;32
/path/to/at/s12/2.pgm;11
/path/to/at/s12/7.pgm;11
/path/to/at/s12/6.pgm;11
/path/to/at/s12/9.pgm;11
/path/to/at/s12/5.pgm;11
/path/to/at/s12/3.pgm;11
/path/to/at/s12/4.pgm;11
/path/to/at/s12/10.pgm;11
/path/to/at/s12/8.pgm;11
/path/to/at/s12/1.pgm;11
/path/to/at/s6/2.pgm;5
/path/to/at/s6/7.pgm;5
/path/to/at/s6/6.pgm;5
/path/to/at/s6/9.pgm;5
/path/to/at/s6/5.pgm;5
/path/to/at/s6/3.pgm;5
/path/to/at/s6/4.pgm;5
/path/to/at/s6/10.pgm;5
/path/to/at/s6/8.pgm;5
/path/to/at/s6/1.pgm;5
/path/to/at/s22/2.pgm;21
/path/to/at/s22/7.pgm;21
/path/to/at/s22/6.pgm;21
/path/to/at/s22/9.pgm;21
/path/to/at/s22/5.pgm;21
/path/to/at/s22/3.pgm;21
/path/to/at/s22/4.pgm;21
/path/to/at/s22/10.pgm;21
/path/to/at/s22/8.pgm;21
/path/to/at/s22/1.pgm;21
/path/to/at/s15/2.pgm;14
/path/to/at/s15/7.pgm;14
/path/to/at/s15/6.pgm;14
/path/to/at/s15/9.pgm;14
/path/to/at/s15/5.pgm;14
/path/to/at/s15/3.pgm;14
/path/to/at/s15/4.pgm;14
/path/to/at/s15/10.pgm;14
/path/to/at/s15/8.pgm;14
/path/to/at/s15/1.pgm;14
/path/to/at/s2/2.pgm;1
/path/to/at/s2/7.pgm;1
/path/to/at/s2/6.pgm;1
/path/to/at/s2/9.pgm;1
/path/to/at/s2/5.pgm;1
/path/to/at/s2/3.pgm;1
/path/to/at/s2/4.pgm;1
/path/to/at/s2/10.pgm;1
/path/to/at/s2/8.pgm;1
/path/to/at/s2/1.pgm;1
/path/to/at/s31/2.pgm;30
/path/to/at/s31/7.pgm;30
/path/to/at/s31/6.pgm;30
/path/to/at/s31/9.pgm;30
/path/to/at/s31/5.pgm;30
/path/to/at/s31/3.pgm;30
/path/to/at/s31/4.pgm;30
/path/to/at/s31/10.pgm;30
/path/to/at/s31/8.pgm;30
/path/to/at/s31/1.pgm;30
/path/to/at/s28/2.pgm;27
/path/to/at/s28/7.pgm;27
/path/to/at/s28/6.pgm;27
/path/to/at/s28/9.pgm;27
/path/to/at/s28/5.pgm;27
/path/to/at/s28/3.pgm;27
/path/to/at/s28/4.pgm;27
/path/to/at/s28/10.pgm;27
/path/to/at/s28/8.pgm;27
/path/to/at/s28/1.pgm;27
/path/to/at/s40/2.pgm;39
/path/to/at/s40/7.pgm;39
/path/to/at/s40/6.pgm;39
/path/to/at/s40/9.pgm;39
/path/to/at/s40/5.pgm;39
/path/to/at/s40/3.pgm;39
/path/to/at/s40/4.pgm;39
/path/to/at/s40/10.pgm;39
/path/to/at/s40/8.pgm;39
/path/to/at/s40/1.pgm;39
/path/to/at/s3/2.pgm;2
/path/to/at/s3/7.pgm;2
/path/to/at/s3/6.pgm;2
/path/to/at/s3/9.pgm;2
/path/to/at/s3/5.pgm;2
/path/to/at/s3/3.pgm;2
/path/to/at/s3/4.pgm;2
/path/to/at/s3/10.pgm;2
/path/to/at/s3/8.pgm;2
/path/to/at/s3/1.pgm;2
/path/to/at/s38/2.pgm;37
/path/to/at/s38/7.pgm;37
/path/to/at/s38/6.pgm;37
/path/to/at/s38/9.pgm;37
/path/to/at/s38/5.pgm;37
/path/to/at/s38/3.pgm;37
/path/to/at/s38/4.pgm;37
/path/to/at/s38/10.pgm;37
/path/to/at/s38/8.pgm;37
/path/to/at/s38/1.pgm;37

View File

@@ -16,7 +16,9 @@
* See <http://www.opensource.org/licenses/bsd-license>
*/
#include "opencv2/opencv.hpp"
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/contrib/contrib.hpp"
#include <iostream>
#include <fstream>
@@ -28,8 +30,9 @@ using namespace std;
static Mat toGrayscale(InputArray _src) {
Mat src = _src.getMat();
// only allow one channel
if(src.channels() != 1)
if(src.channels() != 1) {
CV_Error(CV_StsBadArg, "Only Matrices with one channel are supported");
}
// create and return normalized image
Mat dst;
cv::normalize(_src, dst, 0, 255, NORM_MINMAX, CV_8UC1);
@@ -38,65 +41,122 @@ static Mat toGrayscale(InputArray _src) {
static void read_csv(const string& filename, vector<Mat>& images, vector<int>& labels, char separator = ';') {
std::ifstream file(filename.c_str(), ifstream::in);
if (!file)
throw std::exception();
if (!file) {
string error_message = "No valid input file was given, please check the given filename.";
CV_Error(CV_StsBadArg, error_message);
}
string line, path, classlabel;
while (getline(file, line)) {
stringstream liness(line);
getline(liness, path, separator);
getline(liness, classlabel);
images.push_back(imread(path, 0));
labels.push_back(atoi(classlabel.c_str()));
if(!path.empty() && !classlabel.empty()) {
images.push_back(imread(path, 0));
labels.push_back(atoi(classlabel.c_str()));
}
}
}
int main(int argc, const char *argv[]) {
// check for command line arguments
// Check for valid command line arguments, print usage
// if no arguments were given.
if (argc != 2) {
cout << "usage: " << argv[0] << " <csv.ext>" << endl;
exit(1);
}
// path to your CSV
// Get the path to your CSV.
string fn_csv = string(argv[1]);
// images and corresponding labels
// These vectors hold the images and corresponding labels.
vector<Mat> images;
vector<int> labels;
// read in the data
// Read in the data. This can fail if no valid
// input filename is given.
try {
read_csv(fn_csv, images, labels);
} catch (exception&) {
cerr << "Error opening file \"" << fn_csv << "\"." << endl;
} catch (cv::Exception& e) {
cerr << "Error opening file \"" << fn_csv << "\". Reason: " << e.msg << endl;
// nothing more we can do
exit(1);
}
// get width and height
//int width = images[0].cols;
// Quit if there are not enough images for this demo.
if(images.size() <= 1) {
string error_message = "This demo needs at least 2 images to work. Please add more images to your data set!";
CV_Error(CV_StsError, error_message);
}
// Get the height from the first image. We'll need this
// later in code to reshape the images to their original
// size:
int height = images[0].rows;
// get test instances
// The following lines simply get the last images from
// your dataset and remove it from the vector. This is
// done, so that the training data (which we learn the
// cv::FaceRecognizer on) and the test data we test
// the model with, do not overlap.
Mat testSample = images[images.size() - 1];
int testLabel = labels[labels.size() - 1];
// ... and delete last element
images.pop_back();
labels.pop_back();
// build the Fisherfaces model
Ptr<FaceRecognizer> model = createFisherFaceRecognizer();
// The following lines create an Eigenfaces model for
// face recognition and train it with the images and
// labels read from the given CSV file.
// This here is a full PCA, if you just want to keep
// 10 principal components (read Eigenfaces), then call
// the factory method like this:
//
// cv::createEigenFaceRecognizer(10);
//
// If you want to create a FaceRecognizer with a
// confidennce threshold, call it with:
//
// cv::createEigenFaceRecognizer(10, 123.0);
//
Ptr<FaceRecognizer> model = createEigenFaceRecognizer();
model->train(images, labels);
// test model
int predicted = model->predict(testSample);
cout << "predicted class = " << predicted << endl;
cout << "actual class = " << testLabel << endl;
// get the eigenvectors
Mat W = model->eigenvectors();
// show first 10 fisherfaces
// The following line predicts the label of a given
// test image:
int predictedLabel = model->predict(testSample);
//
// To get the confidence of a prediction call the model with:
//
// int predictedLabel = -1;
// double confidence = 0.0;
// model->predict(testSample, predictedLabel, confidence);
//
string result_message = format("Predicted class = %d / Actual class = %d.", predictedLabel, testLabel);
cout << result_message << endl;
// Sometimes you'll need to get/set internal model data,
// which isn't exposed by the public cv::FaceRecognizer.
// Since each cv::FaceRecognizer is derived from a
// cv::Algorithm, you can query the data.
//
// First we'll use it to set the threshold of the FaceRecognizer
// to 0.0 without retraining the model. This can be useful if
// you are evaluating the model:
//
model->set("threshold", 0.0);
// Now the threshold of this model is set to 0.0. A prediction
// now returns -1, as it's impossible to have a distance below
// it
predictedLabel = model->predict(testSample);
cout << "Predicted class = " << predictedLabel << endl;
// Here is how to get the eigenvalues of this Eigenfaces model:
Mat eigenvalues = model->getMat("eigenvalues");
// And we can do the same to display the Eigenvectors (read Eigenfaces):
Mat W = model->getMat("eigenvectors");
// From this we will display the (at most) first 10 Eigenfaces:
for (int i = 0; i < min(10, W.cols); i++) {
string msg = format("Eigenvalue #%d = %.5f", i, eigenvalues.at<double>(i));
cout << msg << endl;
// get eigenvector #i
Mat ev = W.col(i).clone();
// reshape to original size AND normalize between [0...255]
// Reshape to original size & normalize to [0...255] for imshow.
Mat grayscale = toGrayscale(ev.reshape(1, height));
// show image (with Jet colormap)
// Show the image & apply a Jet colormap for better sensing.
Mat cgrayscale;
applyColorMap(grayscale, cgrayscale, COLORMAP_JET);
imshow(format("%d", i), cgrayscale);
}
waitKey(0);
return 0;
}