forked from China-SPE-T-2013-Group1/Face-Verification-System
-
Notifications
You must be signed in to change notification settings - Fork 0
/
facerec.cpp
448 lines (406 loc) · 16.7 KB
/
facerec.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
#include "stdafx.h"
#include "facerec.hpp"
#include "helper.hpp"
#include "decomposition.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/objdetect/objdetect.hpp"
// A nettoyer !!!!
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/objdetect/objdetect.hpp"
using namespace cv;
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include <fstream>
static Mat test1(150, 150, CV_8UC3);
static Mat size(150, 150, CV_8UC3);
static string face_cascade_name = "haarcascade_frontalface_alt2.xml";
static string eyes_cascade_name = "haarcascade_eye_tree_eyeglasses.xml";
static CascadeClassifier face_cascade;
static CascadeClassifier eyes_cascade;
//------------------------------------------------------------------------------
// cv::FaceRecognizer
//------------------------------------------------------------------------------
void cv::FaceRecognizer::save(const string& filename) const {
FileStorage fs(filename, FileStorage::WRITE);
if (!fs.isOpened())
CV_Error(CV_StsError, "File can't be opened for writing!");
this->save(fs);
fs.release();
}
void cv::FaceRecognizer::load(const string& filename) {
FileStorage fs(filename, FileStorage::READ);
if (!fs.isOpened())
CV_Error(CV_StsError, "File can't be opened for writing!");
this->load(fs);
fs.release();
}
//------------------------------------------------------------------------------
// cv::Eigenfaces
//------------------------------------------------------------------------------
void cv::Eigenfaces::train(InputArray src, InputArray _lbls) {
if(src.total() == 0) {
string error_message = format("Empty training data was given. You'll need more than one sample to learn a model.");
CV_Error(CV_StsUnsupportedFormat, error_message);
} else if(_lbls.getMat().type() != CV_32SC1) {
string error_message = format("Labels must be given as integer (CV_32SC1). Expected %d, but was %d.", CV_32SC1, _lbls.type());
CV_Error(CV_StsUnsupportedFormat, error_message);
}
// make sure data has correct size
if(src.total() > 1) {
for(int i = 1; i < src.total(); i++) {
if(src.getMat(i-1).total() != src.getMat(i).total()) {
string error_message = format("In the Eigenfaces method all input samples (training images) must be of equal size! Expected %d pixels, but was %d pixels.", src.getMat(i-1).total(), src.getMat(i).total());
CV_Error(CV_StsUnsupportedFormat, error_message);
}
}
}
// get labels
vector<int> labels = _lbls.getMat();
// observations in row
Mat data = asRowMatrix(src, CV_64FC1);
// number of samples
int n = data.rows;
// dimensionality of data
int d = data.cols;
// assert there are as much samples as labels
if(n != labels.size()) {
string error_message = format("The number of samples (src) must equal the number of labels (labels). Was len(samples)=%d, len(labels)=%d.", n, labels.size());
CV_Error(CV_StsBadArg, error_message);
}
// clip number of components to be valid
if((_num_components <= 0) || (_num_components > n))
_num_components = n;
// perform the PCA
PCA pca(data, Mat(), CV_PCA_DATA_AS_ROW, _num_components);
// copy the PCA results
_mean = pca.mean.reshape(1,1); // store the mean vector
_eigenvalues = pca.eigenvalues.clone(); // eigenvalues by row
_eigenvectors = transpose(pca.eigenvectors); // eigenvectors by column
_labels = labels; // store labels for prediction
// save projections
for(int sampleIdx = 0; sampleIdx < data.rows; sampleIdx++) {
Mat p = subspace::project(_eigenvectors, _mean, data.row(sampleIdx).clone());
this->_projections.push_back(p);
}
}
void cv::Eigenfaces::predict(InputArray _src, int &minClass, double &minDist) const {
Mat src = _src.getMat();
if(_projections.empty()) {
// throw error if no data (or simply return -1?)
string error_message = "This cv::Eigenfaces model is not computed yet. Did you call cv::Eigenfaces::train?";
CV_Error(CV_StsError, error_message);
} else if(_eigenvectors.rows != src.total()) {
// check data alignment just for clearer exception messages
string error_message = format("Wrong input image size. Reason: Training and Test images must be of equal size! Expected an image with %d elements, but got %d.", _eigenvectors.rows, src.total());
CV_Error(CV_StsError, error_message);
}
// project into PCA subspace
Mat q = subspace::project(_eigenvectors, _mean, src.reshape(1,1));
// find 1-nearest neighbor
minDist = DBL_MAX;
minClass = -1;
for(int sampleIdx = 0; sampleIdx < _projections.size(); sampleIdx++) {
double dist = norm(_projections[sampleIdx], q, NORM_L2);
if((dist < minDist) && (dist < _threshold)) {
minDist = dist;
minClass = _labels[sampleIdx];
}
}
}
int cv::Eigenfaces::predict(Mat _src) const {
int label;
double dummy;
face_cascade.load(face_cascade_name);
eyes_cascade.load(eyes_cascade_name);
Mat faceFrame;
faceFrame = _src;
resize(faceFrame, faceFrame, size.size(), 0, 0, INTER_NEAREST);
predict(faceFrame, label, dummy);
if (dummy < 10000)
{
return label;
}
else
{
return (this->_labels[this->_labels.size()-1] + 1);
}
}
void cv::Eigenfaces::load(const FileStorage& fs) {
//read matrices
fs["num_components"] >> _num_components;
fs["mean"] >> _mean;
fs["eigenvalues"] >> _eigenvalues;
fs["eigenvectors"] >> _eigenvectors;
// read sequences
readFileNodeList(fs["projections"], _projections);
readFileNodeList(fs["labels"], _labels);
}
void cv::Eigenfaces::save(FileStorage& fs) const {
// write matrices
fs << "num_components" << _num_components;
fs << "mean" << _mean;
fs << "eigenvalues" << _eigenvalues;
fs << "eigenvectors" << _eigenvectors;
// write sequences
writeFileNodeList(fs, "projections", _projections);
writeFileNodeList(fs, "labels", _labels);
}
//------------------------------------------------------------------------------
// cv::Fisherfaces
//------------------------------------------------------------------------------
void cv::Fisherfaces::train(InputArray src, InputArray _lbls) {
if(src.total() == 0) {
string error_message = format("Empty training data was given. You'll need more than one sample to learn a model.");
CV_Error(CV_StsUnsupportedFormat, error_message);
} else if(_lbls.getMat().type() != CV_32SC1) {
string error_message = format("Labels must be given as integer (CV_32SC1). Expected %d, but was %d.", CV_32SC1, _lbls.type());
CV_Error(CV_StsUnsupportedFormat, error_message);
}
// make sure data has correct size
if(src.total() > 1) {
for(int i = 1; i < src.total(); i++) {
if(src.getMat(i-1).total() != src.getMat(i).total()) {
string error_message = format("In the Fisherfaces method all input samples (training images) must be of equal size! Expected %d pixels, but was %d pixels.", src.getMat(i-1).total(), src.getMat(i).total());
CV_Error(CV_StsUnsupportedFormat, error_message);
}
}
}
// get data
vector<int> labels = _lbls.getMat();
// wrap asRowMatrix in a try/catch, as people tend to pass wrong data here
Mat data = asRowMatrix(src, CV_64FC1);
// number of samples (N) and dimensions (D)
int N = data.rows;
int D = data.cols;
// assert data is correctly given
if(labels.size() != N) {
string error_message = format("The number of samples (src) must equal the number of labels (labels)! len(src)=%d, len(labels)=%d.", N, labels.size());
CV_Error(CV_StsBadArg, error_message);
}
// the following equals len(unique(C))
int C = remove_dups(labels).size();
// clip number of components to be a valid number
if((_num_components <= 0) || (_num_components > (C-1)))
_num_components = (C-1);
// perform a PCA and keep (N-C) components
PCA pca(data, Mat(), CV_PCA_DATA_AS_ROW, (N-C));
// project the data and perform a LDA on it
subspace::LDA lda(pca.project(data), labels, _num_components);
// store the total mean vector
_mean = pca.mean.reshape(1,1);
// store labels
_labels = labels;
// store the eigenvalues of the discriminants (and make sure they are doubles!)
lda.eigenvalues().convertTo(_eigenvalues, CV_64FC1);
// Now calculate the projection matrix as pca.eigenvectors * lda.eigenvectors.
// Note: OpenCV stores the eigenvectors by row, so we need to transpose it!
gemm(pca.eigenvectors, lda.eigenvectors(), 1.0, Mat(), 0.0, _eigenvectors, GEMM_1_T);
// store the projections of the original data
for(int sampleIdx = 0; sampleIdx < data.rows; sampleIdx++) {
Mat p = subspace::project(_eigenvectors, _mean, data.row(sampleIdx).clone());
_projections.push_back(p);
}
}
int cv::Fisherfaces::predict(Mat _src) const {
int label;
double dummy;
face_cascade.load(face_cascade_name);
eyes_cascade.load(eyes_cascade_name);
Mat faceFrame;
faceFrame = _src;
resize(faceFrame, faceFrame, size.size(), 0, 0, INTER_NEAREST);
//imshow("test", faceFrame);
predict(faceFrame, label, dummy);
if (dummy < 10000)
{
return label;
}
else
{
return (this->_labels[this->_labels.size()-1] + 1);
}
}
void cv::Fisherfaces::predict(InputArray _src, int &minClass, double &minDist) const {
Mat src = _src.getMat();
// check data alignment just for clearer exception messages
if(_projections.empty()) {
// throw error if no data (or simply return -1?)
string error_message = "This cv::Fisherfaces model is not computed yet. Did you call cv::Fisherfaces::train?";
CV_Error(CV_StsError, error_message);
} else if(_eigenvectors.rows != src.total()) {
string error_message = format("Wrong input image size. Reason: Training and Test images must be of equal size! Expected an image with %d elements, but got %d.", _eigenvectors.rows, src.total());
CV_Error(CV_StsError, error_message);
}
// project into LDA subspace
Mat q = subspace::project(_eigenvectors, _mean, src.reshape(1,1));
// find 1-nearest neighbor
minDist = DBL_MAX;
minClass = -1;
for(int sampleIdx = 0; sampleIdx < _projections.size(); sampleIdx++) {
double dist = norm(_projections[sampleIdx], q, NORM_L2);
if((dist < minDist) && (dist < _threshold)) {
minDist = dist;
minClass = _labels[sampleIdx];
}
}
}
// See cv::FaceRecognizer::load.
void cv::Fisherfaces::load(const FileStorage& fs) {
//read matrices
fs["num_components"] >> _num_components;
fs["mean"] >> _mean;
fs["eigenvalues"] >> _eigenvalues;
fs["eigenvectors"] >> _eigenvectors;
// read sequences
readFileNodeList(fs["projections"], _projections);
readFileNodeList(fs["labels"], _labels);
}
// See cv::FaceRecognizer::save.
void cv::Fisherfaces::save(FileStorage& fs) const {
// write matrices
fs << "num_components" << _num_components;
fs << "mean" << _mean;
fs << "eigenvalues" << _eigenvalues;
fs << "eigenvectors" << _eigenvectors;
// write sequences
writeFileNodeList(fs, "projections", _projections);
writeFileNodeList(fs, "labels", _labels);
}
//------------------------------------------------------------------------------
// cv::LBPH
//------------------------------------------------------------------------------
void cv::LBPH::load(const FileStorage& fs) {
fs["radius"] >> _radius;
fs["neighbors"] >> _neighbors;
fs["grid_x"] >> _grid_x;
fs["grid_y"] >> _grid_y;
//read matrices
readFileNodeList(fs["histograms"], _histograms);
readFileNodeList(fs["labels"], _labels);
}
// See cv::FaceRecognizer::save.
void cv::LBPH::save(FileStorage& fs) const {
fs << "radius" << _radius;
fs << "neighbors" << _neighbors;
fs << "grid_x" << _grid_x;
fs << "grid_y" << _grid_y;
// write matrices
writeFileNodeList(fs, "histograms", _histograms);
writeFileNodeList(fs, "labels", _labels);
}
void cv::LBPH::train(InputArray _src, InputArray _lbls) {
if(_src.kind() != _InputArray::STD_VECTOR_MAT && _src.kind() != _InputArray::STD_VECTOR_VECTOR) {
string error_message = "The images are expected as InputArray::STD_VECTOR_MAT (a std::vector<Mat>) or _InputArray::STD_VECTOR_VECTOR (a std::vector< vector<...> >).";
CV_Error(CV_StsBadArg, error_message);
}
if(_src.total() == 0) {
string error_message = format("Empty training data was given. You'll need more than one sample to learn a model.");
CV_Error(CV_StsUnsupportedFormat, error_message);
} else if(_lbls.getMat().type() != CV_32SC1) {
string error_message = format("Labels must be given as integer (CV_32SC1). Expected %d, but was %d.", CV_32SC1, _lbls.type());
CV_Error(CV_StsUnsupportedFormat, error_message);
}
// get the vector of matrices
vector<Mat> src;
_src.getMatVector(src);
// turn the label matrix into a vector
vector<int> labels = _lbls.getMat();
if(labels.size() != src.size()) {
string error_message = format("The number of samples (src) must equal the number of labels (labels). Was len(samples)=%d, len(labels)=%d.", src.size(), labels.size());
CV_Error(CV_StsBadArg, error_message);
}
// store given labels
_labels = labels;
// store the spatial histograms of the original data
for(int sampleIdx = 0; sampleIdx < src.size(); sampleIdx++) {
// calculate lbp image
Mat lbp_image = elbp(src[sampleIdx], _radius, _neighbors);
// get spatial histogram from this lbp image
Mat p = spatial_histogram(
lbp_image, /* lbp_image */
static_cast<int>(std::pow(2.0, static_cast<double>(_neighbors))), /* number of possible patterns */
_grid_x, /* grid size x */
_grid_y, /* grid size y */
true);
// add to templates
_histograms.push_back(p);
}
}
int cv::LBPH::predict(InputArray _src) const {
int label;
double dummy;
predict(_src, label, dummy);
return label;
}
void cv::LBPH::predict(InputArray _src, int &minClass, double &minDist) const {
Mat src = _src.getMat();
// get the spatial histogram from input image
Mat lbp_image = elbp(src, _radius, _neighbors);
Mat query = spatial_histogram(
lbp_image, /* lbp_image */
static_cast<int>(std::pow(2.0, static_cast<double>(_neighbors))), /* number of possible patterns */
_grid_x, /* grid size x */
_grid_y, /* grid size y */
true /* normed histograms */);
// find 1-nearest neighbor
minDist = DBL_MAX;
minClass = -1;
for(int sampleIdx = 0; sampleIdx < _histograms.size(); sampleIdx++) {
double dist = compareHist(_histograms[sampleIdx], query, CV_COMP_CHISQR);
if((dist < minDist) && (dist < _threshold)) {
minDist = dist;
minClass = _labels[sampleIdx];
}
}
}
std::vector<Rect> detectionAndDisplay(Mat frame, CascadeClassifier face_cascade, CascadeClassifier eyes_cascade)
{
std::vector<Rect> faces;
Mat frame_gray;
Mat b = frame;
std::vector<Rect> retour;
cvtColor(frame, frame_gray, CV_BGR2GRAY);
equalizeHist(frame_gray, frame_gray);
//-- Detect faces
face_cascade.detectMultiScale(frame_gray, faces, 1.1, 2, 0, Size(80, 80));
retour = faces;
for (int i = 0; i < faces.size(); i++)
{
Mat faceROI = frame_gray( faces[i] );
std::vector<Rect> eyes;
//-- In each face, detect eyes
eyes_cascade.detectMultiScale( faceROI, eyes, 1.1, 2, 0 |CV_HAAR_SCALE_IMAGE, Size(30, 30) );
/*if( eyes.size() == 2)
{
//-- Draw the face
Point center( faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5 );
ellipse( frame, center, Size( faces[i].width*0.5, faces[i].height*0.5), 0, 0, 360, Scalar( 255, 0, 0 ), 2, 8, 0 );
for( int j = 0; j < eyes.size(); j++ )
{ //-- Draw the eyes
Point center( faces[i].x + eyes[j].x + eyes[j].width*0.5, faces[i].y + eyes[j].y + eyes[j].height*0.5 );
int radius = cvRound( (eyes[j].width + eyes[j].height)*0.25 );
circle( frame, center, radius, Scalar( 255, 0, 255 ), 3, 8, 0 );
}
}*/
}
return retour;
}
Mat showNormalizeFace(vector<Rect> detectedfaces, Mat frame)
{
Mat faceROI;
Mat frame1;
if (detectedfaces.size() > 0)
{
Mat frame_gray;
cvtColor(frame, frame_gray, CV_BGR2GRAY);
equalizeHist(frame_gray, frame_gray);
faceROI = frame_gray(detectedfaces[0]);
cv::resize(faceROI, faceROI, frame.size());
//imshow("Normalized face", faceROI);
return faceROI;
}
cvtColor(frame, frame1, CV_BGR2GRAY);
//imshow("Normalized face", frame1);
return frame1;
}