Last active
December 23, 2015 04:19
-
-
Save jimregan/6579970 to your computer and use it in GitHub Desktop.
Adaptation of the OpenCV LBP face recogniser demo to handle more than one image.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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> | |
*/ | |
#include "opencv2/core/core.hpp" | |
#include "opencv2/highgui/highgui.hpp" | |
#include "facerec.hpp" | |
#include <iostream> | |
#include <fstream> | |
#include <sstream> | |
using namespace cv; | |
using namespace std; | |
static void read_csv(const string& filename, vector<Mat>& images, vector<string>& labels, vector<int>& ids, vector<string>& todo, char separator = ';') { | |
int lastid = 0; | |
map<string, int> reverse; | |
std::ifstream file(filename.c_str(), ifstream::in); | |
if (!file) { | |
string error_message = "No valid input file was given, please check the given filename."; | |
CV_Error(CV_StsBadArg, error_message); | |
} | |
int lblcount = 0; | |
string line, path, classlabel; | |
while (getline(file, line)) { | |
stringstream liness(line); | |
getline(liness, path, separator); | |
getline(liness, classlabel); | |
if(classlabel.empty()) { | |
todo.push_back(path); | |
} | |
if(!path.empty() && !classlabel.empty()) { | |
images.push_back(imread(path, 0)); | |
if(std::find(labels.begin(), labels.end(), classlabel) == labels.end()) { | |
labels.push_back(classlabel); | |
reverse[classlabel] = lastid; | |
lastid++; | |
} | |
ids.push_back(reverse[classlabel]); | |
} | |
} | |
} | |
int main(int argc, const char *argv[]) { | |
// 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); | |
} | |
// Get the path to your CSV. | |
string fn_csv = string(argv[1]); | |
// These vectors hold the images and corresponding labels. | |
vector<Mat> images; | |
vector<string> labels; | |
vector<int> ids; | |
vector<string> todo; | |
// Read in the data. This can fail if no valid | |
// input filename is given. | |
try { | |
read_csv(fn_csv, images, labels, ids, todo); | |
} catch (cv::Exception& e) { | |
cerr << "Error opening file \"" << fn_csv << "\". Reason: " << e.msg << endl; | |
// nothing more we can do | |
exit(1); | |
} | |
// 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); | |
} | |
Ptr<FaceRecognizer> model = createLBPHFaceRecognizer(); | |
model->train(images, ids); | |
// The following line predicts the label of a given | |
// test image: | |
for(vector<string>::iterator it = todo.begin(); it != todo.end(); it++) { | |
Mat current = imread(*it, 0); | |
int predictedLabel = model->predict(current); | |
cout << *it << ";" << labels[predictedLabel] << endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment