Last active
December 1, 2016 03:34
-
-
Save EyalAr/5196581 to your computer and use it in GitHub Desktop.
Code snippets for my OpenCV face recognition tutorial, in which I show how to build an application which recognizes a specific person in a video.
Full code at: https://bitbucket.org/EyalAr/person-recognizer
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
#define PERSON_LABEL = 10 //some arbitrary label | |
//LBPH face recognizer parameters: | |
#define RADIUS 1 | |
#define NEIGHBORS 8 | |
#define GRID_X 8 | |
#define GRID_Y 8 | |
#define THRESHOLD 130.0 | |
//create a LBPH face recognizer model: | |
Ptr<FaceRecognizer> model = | |
createLBPHFaceRecognizer(RADIUS, NEIGHBORS, GRID_X, GRID_Y, THRESHOLD); | |
vector<Mat> imgs = /* Populate this vector with images of the | |
face of the person to be recognized */; | |
//Initialize a vector of labels. All labels have the same value | |
//since all faces belong to the same person. | |
vector<int> labels(imgs.size()); | |
for (vector<int>::iterator it = labels.begin(); | |
it != labels.end(); | |
*(it++) = PERSON_LABEL); | |
//Train the face recognizer to recognize the face of the perosn: | |
model->train(imgs, labels); | |
/* Now the model is ready to recognize faces. */ |
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
Mat face = /* The image of the face to be recognized */; | |
Mat gray; | |
//convert the face to grayscale: | |
cvtColor(face, gray, CV_BGR2GRAY); | |
int label = -1; | |
double confidence = 0; | |
//Make the prediction: | |
model->predict(gray, label, confidence); | |
if (label == PERSON_LABEL){ | |
/* Face recognized as our person. */ | |
cout << "Person recognized with confidence of " << confidence << endl; | |
} | |
else{ | |
cout << "Face not recognized." << endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment