Skip to content

Instantly share code, notes, and snippets.

@UnaNancyOwen
Last active July 27, 2017 07:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save UnaNancyOwen/08ab052bb0420049aad25b149cd3c7b2 to your computer and use it in GitHub Desktop.
Save UnaNancyOwen/08ab052bb0420049aad25b149cd3c7b2 to your computer and use it in GitHub Desktop.
Face Recognition using Face Module
#include <iostream>
#include <vector>
#include <opencv2/opencv.hpp>
#include <opencv2/face.hpp>
int main( int argc, char* argv[] )
{
// Read Image
cv::Mat image = cv::imread( "image.jpg", cv::IMREAD_GRAYSCALE );
if( image.empty() ){
return -1;
}
// Create Recognizer
//cv::Ptr<cv::face::FaceRecognizer> model = cv::face::createFisherFaceRecognizer();
//cv::Ptr<cv::face::FaceRecognizer> model = cv::face::createEigenFaceRecognizer();
cv::Ptr<cv::face::FaceRecognizer> model = cv::face::createLBPHFaceRecognizer();
// Load Recognizer
model->load( "../model.xml" );
if( model.empty() ){
return -1;
}
// Load Cascade Classifier for Face Detect
cv::CascadeClassifier cascade( "../haarcascade_frontalface_default.xml" );
if( cascade.empty() ){
return -1;
}
// Detect Face
std::vector<cv::Rect> rects;
cascade.detectMultiScale( image, rects );
for( const cv::Rect rect : rects ){
// Retrieve Face Image
cv::Mat face = image( rect );
// FisherFaceRecognizer and EigenFaceRecognizer require resize image to same size as training image.
// LBPHFaceRecognizer doesn't requere resize image.
//cv::resize( face, face, cv::Size( 200, 200 ) );
// Face Recognition with Retrieve Most Match Result
int label = -1;
double confidence = 0.0;
model->predict( face, label, confidence );
std::cout << label << " (" << confidence << ")" << std::endl;
/*
// Face Recognition with Retrieve All Match Results
cv::Ptr<cv::face::StandardCollector> collector = cv::face::StandardCollector::create( model->getThreshold() );
model->predict( face, collector );
std::vector<std::pair<int, double>> results = collector->getResults( true );
for( const std::pair<int, double> result : results ){
int label = result.first;
double confidence = result.second;
std::cout << label << " (" << confidence << ")" << std::endl;
}
*/
}
return 0;
}
#include <iostream>
#include <vector>
#include <filesystem>
#include <string>
#include <regex>
#include <opencv2/opencv.hpp>
#include <opencv2/face.hpp>
int main( int argc, char* argv[] )
{
/* Training */
// Load Cascade Classifier for Face Detect
cv::CascadeClassifier cascade( "../haarcascade_frontalface_default.xml" );
if( cascade.empty() ){
return -1;
}
// Create DataSet
std::cout << "Create DataSet" << std::endl;
std::vector<cv::Mat> faces;
std::vector<int> labels;
std::tr2::sys::path dataset( "../trainingdata" );
std::for_each( std::tr2::sys::directory_iterator( dataset ), std::tr2::sys::directory_iterator(),
[&]( const std::tr2::sys::path& path ){
if( std::tr2::sys::is_regular_file( path ) ){
// Read Image
cv::Mat image = cv::imread( path.string(), cv::IMREAD_GRAYSCALE );
if( image.empty() ){
return;
}
// Detect Face (First Face)
std::vector<cv::Rect> rects;
cascade.detectMultiScale( image, rects );
cv::Mat face = image( rects[0] );
// Resize Face
cv::resize( face, face, cv::Size( 200, 200 ) );
// Store Face
faces.push_back( face.clone() );
// Retrieve Label Number from File Name ( e.g. image01.png )
const std::string filename = path.filename().string();
std::smatch match;
std::regex_search( filename, match, std::regex( "\\d+" ) );
int label = std::stoi( match.str() );
// Store Label Number
labels.push_back( label );
}
}
);
// Create Recognizer
//cv::Ptr<cv::face::FaceRecognizer> model = cv::face::createFisherFaceRecognizer();
//cv::Ptr<cv::face::FaceRecognizer> model = cv::face::createEigenFaceRecognizer();
cv::Ptr<cv::face::FaceRecognizer> model = cv::face::createLBPHFaceRecognizer();
// Training Recognizer
std::cout << "Training Recognizer" << std::endl;
model->train( faces, labels );
// Save Recognizer
std::cout << "Save Recognizer" << std::endl;
model->save( "../model.xml" );
return 0;
}
@lethanhthai
Copy link

please tell me what is model.xml?
and I have bug:
OpenCV Error: Unspecified error (File can't be opened for writing!) in load, file /home/kien/Bluerical/opencv_contrib/modules/face/src/facerec.cpp, line 61
terminate called after throwing an instance of 'cv::Exception'
what(): /home/kien/Bluerical/opencv_contrib/modules/face/src/facerec.cpp:61: error: (-2) File can't be opened for writing! in function load

Aborted

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment