facerec_demo.cpp 6.33 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * 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>
 */

19 20 21
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/contrib/contrib.hpp"
22 23 24 25 26 27 28 29

#include <iostream>
#include <fstream>
#include <sstream>

using namespace cv;
using namespace std;

30
static Mat toGrayscale(InputArray _src) {
Philipp Wagner's avatar
Philipp Wagner committed
31 32
    Mat src = _src.getMat();
    // only allow one channel
33
    if(src.channels() != 1) {
Philipp Wagner's avatar
Philipp Wagner committed
34
        CV_Error(CV_StsBadArg, "Only Matrices with one channel are supported");
35
    }
Philipp Wagner's avatar
Philipp Wagner committed
36 37 38 39 40 41
    // create and return normalized image
    Mat dst;
    cv::normalize(_src, dst, 0, 255, NORM_MINMAX, CV_8UC1);
    return dst;
}

42
static void read_csv(const string& filename, vector<Mat>& images, vector<int>& labels, char separator = ';') {
43
    std::ifstream file(filename.c_str(), ifstream::in);
44 45 46 47
    if (!file) {
        string error_message = "No valid input file was given, please check the given filename.";
        CV_Error(CV_StsBadArg, error_message);
    }
48 49 50 51 52
    string line, path, classlabel;
    while (getline(file, line)) {
        stringstream liness(line);
        getline(liness, path, separator);
        getline(liness, classlabel);
53 54 55 56
        if(!path.empty() && !classlabel.empty()) {
            images.push_back(imread(path, 0));
            labels.push_back(atoi(classlabel.c_str()));
        }
57 58 59 60
    }
}

int main(int argc, const char *argv[]) {
61 62
    // Check for valid command line arguments, print usage
    // if no arguments were given.
63 64 65 66
    if (argc != 2) {
        cout << "usage: " << argv[0] << " <csv.ext>" << endl;
        exit(1);
    }
67
    // Get the path to your CSV.
68
    string fn_csv = string(argv[1]);
69
    // These vectors hold the images and corresponding labels.
70 71
    vector<Mat> images;
    vector<int> labels;
72 73
    // Read in the data. This can fail if no valid
    // input filename is given.
74 75
    try {
        read_csv(fn_csv, images, labels);
76 77 78
    } catch (cv::Exception& e) {
        cerr << "Error opening file \"" << fn_csv << "\". Reason: " << e.msg << endl;
        // nothing more we can do
79 80
        exit(1);
    }
81 82 83 84 85 86 87 88
    // 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:
89
    int height = images[0].rows;
90 91 92 93 94
    // 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.
95 96 97 98
    Mat testSample = images[images.size() - 1];
    int testLabel = labels[labels.size() - 1];
    images.pop_back();
    labels.pop_back();
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
    // 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();
114
    model->train(images, labels);
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
    // 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:
147
    for (int i = 0; i < min(10, W.cols); i++) {
148 149
        string msg = format("Eigenvalue #%d = %.5f", i, eigenvalues.at<double>(i));
        cout << msg << endl;
150 151
        // get eigenvector #i
        Mat ev = W.col(i).clone();
152
        // Reshape to original size & normalize to [0...255] for imshow.
Philipp Wagner's avatar
Philipp Wagner committed
153
        Mat grayscale = toGrayscale(ev.reshape(1, height));
154
        // Show the image & apply a Jet colormap for better sensing.
Philipp Wagner's avatar
Philipp Wagner committed
155
        Mat cgrayscale;
156 157 158 159
        applyColorMap(grayscale, cgrayscale, COLORMAP_JET);
        imshow(format("%d", i), cgrayscale);
    }
    waitKey(0);
160

161 162
    return 0;
}