2012-03-24 23:03:47 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2011. Philipp Wagner <bytefish[at]gmx[dot]de>.
|
|
|
|
* Released to public domain under terms of the BSD Simplified license.
|
|
|
|
*
|
|
|
|
* 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 name of the organization nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* See <http://www.opensource.org/licenses/bsd-license>
|
|
|
|
*/
|
|
|
|
|
2012-06-10 13:57:33 +02:00
|
|
|
#include "opencv2/core/core.hpp"
|
|
|
|
#include "opencv2/highgui/highgui.hpp"
|
|
|
|
#include "opencv2/contrib/contrib.hpp"
|
2012-03-24 23:03:47 +01:00
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
using namespace cv;
|
|
|
|
using namespace std;
|
|
|
|
|
2012-06-07 19:21:29 +02:00
|
|
|
static Mat toGrayscale(InputArray _src) {
|
2012-05-11 13:48:26 +02:00
|
|
|
Mat src = _src.getMat();
|
|
|
|
// only allow one channel
|
2012-06-11 00:42:20 +02:00
|
|
|
if(src.channels() != 1) {
|
2012-05-11 13:48:26 +02:00
|
|
|
CV_Error(CV_StsBadArg, "Only Matrices with one channel are supported");
|
2012-06-11 00:42:20 +02:00
|
|
|
}
|
2012-05-11 13:48:26 +02:00
|
|
|
// create and return normalized image
|
|
|
|
Mat dst;
|
|
|
|
cv::normalize(_src, dst, 0, 255, NORM_MINMAX, CV_8UC1);
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
2012-06-07 19:21:29 +02:00
|
|
|
static void read_csv(const string& filename, vector<Mat>& images, vector<int>& labels, char separator = ';') {
|
2012-03-24 23:03:47 +01:00
|
|
|
std::ifstream file(filename.c_str(), ifstream::in);
|
2012-06-10 13:57:33 +02:00
|
|
|
if (!file) {
|
2012-06-11 00:23:18 +02:00
|
|
|
string error_message = "No valid input file was given, please check the given filename.";
|
2012-06-10 13:57:33 +02:00
|
|
|
CV_Error(CV_StsBadArg, error_message);
|
|
|
|
}
|
2012-03-24 23:03:47 +01:00
|
|
|
string line, path, classlabel;
|
|
|
|
while (getline(file, line)) {
|
|
|
|
stringstream liness(line);
|
|
|
|
getline(liness, path, separator);
|
|
|
|
getline(liness, classlabel);
|
2012-06-10 13:57:33 +02:00
|
|
|
if(!path.empty() && !classlabel.empty()) {
|
|
|
|
images.push_back(imread(path, 0));
|
|
|
|
labels.push_back(atoi(classlabel.c_str()));
|
|
|
|
}
|
2012-03-24 23:03:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, const char *argv[]) {
|
2012-06-10 13:57:33 +02:00
|
|
|
// Check for valid command line arguments, print usage
|
2012-06-11 00:23:18 +02:00
|
|
|
// if no arguments were given.
|
2012-03-24 23:03:47 +01:00
|
|
|
if (argc != 2) {
|
|
|
|
cout << "usage: " << argv[0] << " <csv.ext>" << endl;
|
|
|
|
exit(1);
|
|
|
|
}
|
2012-06-10 13:57:33 +02:00
|
|
|
// Get the path to your CSV.
|
2012-03-24 23:03:47 +01:00
|
|
|
string fn_csv = string(argv[1]);
|
2012-06-10 13:57:33 +02:00
|
|
|
// These vectors hold the images and corresponding labels.
|
2012-03-24 23:03:47 +01:00
|
|
|
vector<Mat> images;
|
|
|
|
vector<int> labels;
|
2012-06-10 13:57:33 +02:00
|
|
|
// Read in the data. This can fail if no valid
|
|
|
|
// input filename is given.
|
2012-03-24 23:03:47 +01:00
|
|
|
try {
|
|
|
|
read_csv(fn_csv, images, labels);
|
2012-06-10 13:57:33 +02:00
|
|
|
} catch (cv::Exception& e) {
|
|
|
|
cerr << "Error opening file \"" << fn_csv << "\". Reason: " << e.msg << endl;
|
|
|
|
// nothing more we can do
|
2012-03-24 23:03:47 +01:00
|
|
|
exit(1);
|
|
|
|
}
|
2012-06-10 13:57:33 +02:00
|
|
|
// Quit if there are not enough images for this demo.
|
|
|
|
if(images.size() <= 1) {
|
2012-06-11 00:23:18 +02:00
|
|
|
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);
|
2012-06-10 13:57:33 +02:00
|
|
|
}
|
|
|
|
// Get the height from the first image. We'll need this
|
|
|
|
// later in code to reshape the images to their original
|
|
|
|
// size:
|
2012-03-24 23:03:47 +01:00
|
|
|
int height = images[0].rows;
|
2012-06-10 13:57:33 +02:00
|
|
|
// 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.
|
2012-03-24 23:03:47 +01:00
|
|
|
Mat testSample = images[images.size() - 1];
|
|
|
|
int testLabel = labels[labels.size() - 1];
|
|
|
|
images.pop_back();
|
|
|
|
labels.pop_back();
|
2012-06-10 13:57:33 +02:00
|
|
|
// 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:
|
|
|
|
//
|
2012-06-11 00:23:18 +02:00
|
|
|
// cv::createEigenFaceRecognizer(10);
|
|
|
|
//
|
|
|
|
// If you want to create a FaceRecognizer with a
|
|
|
|
// confidennce threshold, call it with:
|
|
|
|
//
|
|
|
|
// cv::createEigenFaceRecognizer(10, 123.0);
|
|
|
|
//
|
2012-06-10 13:57:33 +02:00
|
|
|
Ptr<FaceRecognizer> model = createEigenFaceRecognizer();
|
2012-03-24 23:03:47 +01:00
|
|
|
model->train(images, labels);
|
2012-06-10 13:57:33 +02:00
|
|
|
// The following line predicts the label of a given
|
2012-06-11 00:23:18 +02:00
|
|
|
// test image:
|
|
|
|
int predictedLabel = model->predict(testSample);
|
|
|
|
//
|
2012-06-13 00:49:14 +02:00
|
|
|
// To get the confidence of a prediction call the model with:
|
2012-06-11 00:23:18 +02:00
|
|
|
//
|
|
|
|
// int predictedLabel = -1;
|
|
|
|
// double confidence = 0.0;
|
|
|
|
// model->predict(testSample, predictedLabel, confidence);
|
|
|
|
//
|
|
|
|
string result_message = format("Predicted class = %d / Actual class = %d.", predictedLabel, testLabel);
|
2012-06-10 13:57:33 +02:00
|
|
|
cout << result_message << endl;
|
2012-06-11 00:23:18 +02:00
|
|
|
// 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
|
2012-06-11 00:42:20 +02:00
|
|
|
// to 0.0 without retraining the model. This can be useful if
|
|
|
|
// you are evaluating the model:
|
2012-06-11 00:23:18 +02:00
|
|
|
//
|
|
|
|
model->set("threshold", 0.0);
|
2012-06-11 00:42:20 +02:00
|
|
|
// 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
|
2012-06-11 00:23:18 +02:00
|
|
|
predictedLabel = model->predict(testSample);
|
|
|
|
cout << "Predicted class = " << predictedLabel << endl;
|
2012-06-11 00:42:20 +02:00
|
|
|
// Here is how to get the eigenvalues of this Eigenfaces model:
|
2012-06-10 13:57:33 +02:00
|
|
|
Mat eigenvalues = model->getMat("eigenvalues");
|
2012-06-11 00:23:18 +02:00
|
|
|
// And we can do the same to display the Eigenvectors (read Eigenfaces):
|
2012-06-10 13:57:33 +02:00
|
|
|
Mat W = model->getMat("eigenvectors");
|
|
|
|
// From this we will display the (at most) first 10 Eigenfaces:
|
2012-03-24 23:03:47 +01:00
|
|
|
for (int i = 0; i < min(10, W.cols); i++) {
|
2012-06-11 00:23:18 +02:00
|
|
|
string msg = format("Eigenvalue #%d = %.5f", i, eigenvalues.at<double>(i));
|
|
|
|
cout << msg << endl;
|
2012-03-24 23:03:47 +01:00
|
|
|
// get eigenvector #i
|
|
|
|
Mat ev = W.col(i).clone();
|
2012-06-10 13:57:33 +02:00
|
|
|
// Reshape to original size & normalize to [0...255] for imshow.
|
2012-05-11 13:48:26 +02:00
|
|
|
Mat grayscale = toGrayscale(ev.reshape(1, height));
|
2012-06-10 13:57:33 +02:00
|
|
|
// Show the image & apply a Jet colormap for better sensing.
|
2012-05-11 13:48:26 +02:00
|
|
|
Mat cgrayscale;
|
2012-03-24 23:03:47 +01:00
|
|
|
applyColorMap(grayscale, cgrayscale, COLORMAP_JET);
|
|
|
|
imshow(format("%d", i), cgrayscale);
|
|
|
|
}
|
|
|
|
waitKey(0);
|
2012-06-10 13:57:33 +02:00
|
|
|
|
2012-03-24 23:03:47 +01:00
|
|
|
return 0;
|
|
|
|
}
|