2 * Copyright (c) 2011. Philipp Wagner <bytefish[at]gmx[dot]de>.
3 * Released to public domain under terms of the BSD Simplified license.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the organization nor the names of its contributors
13 * may be used to endorse or promote products derived from this software
14 * without specific prior written permission.
16 * See <http://www.opensource.org/licenses/bsd-license>
19 #include "opencv2/core.hpp"
20 #include "opencv2/contrib.hpp"
21 #include "opencv2/highgui.hpp"
30 static void read_csv(const string& filename, vector<Mat>& images, vector<int>& labels, char separator = ';') {
31 std::ifstream file(filename.c_str(), ifstream::in);
33 string error_message = "No valid input file was given, please check the given filename.";
34 CV_Error(CV_StsBadArg, error_message);
36 string line, path, classlabel;
37 while (getline(file, line)) {
38 stringstream liness(line);
39 getline(liness, path, separator);
40 getline(liness, classlabel);
41 if(!path.empty() && !classlabel.empty()) {
42 images.push_back(imread(path, 0));
43 labels.push_back(atoi(classlabel.c_str()));
48 int main(int argc, const char *argv[]) {
49 // Check for valid command line arguments, print usage
50 // if no arguments were given.
52 cout << "usage: " << argv[0] << " <csv.ext>" << endl;
55 // Get the path to your CSV.
56 string fn_csv = string(argv[1]);
57 // These vectors hold the images and corresponding labels.
60 // Read in the data. This can fail if no valid
61 // input filename is given.
63 read_csv(fn_csv, images, labels);
64 } catch (cv::Exception& e) {
65 cerr << "Error opening file \"" << fn_csv << "\". Reason: " << e.msg << endl;
66 // nothing more we can do
69 // Quit if there are not enough images for this demo.
70 if(images.size() <= 1) {
71 string error_message = "This demo needs at least 2 images to work. Please add more images to your data set!";
72 CV_Error(CV_StsError, error_message);
74 // Get the height from the first image. We'll need this
75 // later in code to reshape the images to their original
77 int height = images[0].rows;
78 // The following lines simply get the last images from
79 // your dataset and remove it from the vector. This is
80 // done, so that the training data (which we learn the
81 // cv::FaceRecognizer on) and the test data we test
82 // the model with, do not overlap.
83 Mat testSample = images[images.size() - 1];
84 int testLabel = labels[labels.size() - 1];
87 // The following lines create an LBPH model for
88 // face recognition and train it with the images and
89 // labels read from the given CSV file.
91 // The LBPHFaceRecognizer uses Extended Local Binary Patterns
92 // (it's probably configurable with other operators at a later
93 // point), and has the following default values
100 // So if you want a LBPH FaceRecognizer using a radius of
101 // 2 and 16 neighbors, call the factory method with:
103 // cv::createLBPHFaceRecognizer(2, 16);
105 // And if you want a threshold (e.g. 123.0) call it with its default values:
107 // cv::createLBPHFaceRecognizer(1,8,8,8,123.0)
109 Ptr<FaceRecognizer> model = createLBPHFaceRecognizer();
110 model->train(images, labels);
111 // The following line predicts the label of a given
113 int predictedLabel = model->predict(testSample);
115 // To get the confidence of a prediction call the model with:
117 // int predictedLabel = -1;
118 // double confidence = 0.0;
119 // model->predict(testSample, predictedLabel, confidence);
121 string result_message = format("Predicted class = %d / Actual class = %d.", predictedLabel, testLabel);
122 cout << result_message << endl;
123 // Sometimes you'll need to get/set internal model data,
124 // which isn't exposed by the public cv::FaceRecognizer.
125 // Since each cv::FaceRecognizer is derived from a
126 // cv::Algorithm, you can query the data.
128 // First we'll use it to set the threshold of the FaceRecognizer
129 // to 0.0 without retraining the model. This can be useful if
130 // you are evaluating the model:
132 model->set("threshold", 0.0);
133 // Now the threshold of this model is set to 0.0. A prediction
134 // now returns -1, as it's impossible to have a distance below
136 predictedLabel = model->predict(testSample);
137 cout << "Predicted class = " << predictedLabel << endl;
138 // Show some informations about the model, as there's no cool
139 // Model data to display as in Eigenfaces/Fisherfaces.
140 // Due to efficiency reasons the LBP images are not stored
142 cout << "Model Information:" << endl;
143 string model_info = format("\tLBPH(radius=%i, neighbors=%i, grid_x=%i, grid_y=%i, threshold=%.2f)",
144 model->getInt("radius"),
145 model->getInt("neighbors"),
146 model->getInt("grid_x"),
147 model->getInt("grid_y"),
148 model->getDouble("threshold"));
149 cout << model_info << endl;
150 // We could get the histograms for example:
151 vector<Mat> histograms = model->getMatVector("histograms");
152 // But should I really visualize it? Probably the length is interesting:
153 cout << "Size of the histograms: " << histograms[0].total() << endl;