-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecognition.cpp
More file actions
104 lines (86 loc) · 3.05 KB
/
recognition.cpp
File metadata and controls
104 lines (86 loc) · 3.05 KB
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
#include "recognition.h"
#include "opencv2/opencv.hpp"
#include "opencv2/contrib/contrib.hpp"
#include "opencv2/core/core.hpp"
#include "opencv/cv.h"
#include "opencv/cxcore.h"
using namespace cv;
using namespace std;
recognition::recognition()
{
}
recognition::~recognition()
{
}
/*
Trains the Face recogniser with processed images
@params - preProcessedfaces(array); faceLabes(array); faceAlgorthm(string)
@returns - trained faceRecogniser
*/
Ptr<FaceRecognizer> recognition::learnCollectedFaces(const vector<Mat> preprocessedFaces, const vector<int> faceLabels, const string facerecAlgorithm)
{
Ptr<FaceRecognizer> model;
cout << "Learning faces using: " << facerecAlgorithm << " algorith" << endl;
//ensure contrib is loaded at runtime
bool haveContribModule = initModule_contrib();
if(!haveContribModule){
cerr << "contrib load failed!" << endl;
exit(1);
}
model = Algorithm::create<FaceRecognizer>(facerecAlgorithm);
if(model.empty()){
cerr << "algorithm not available" << endl;
exit(1);
}
//init done, now train from collected faces
model->train(preprocessedFaces, faceLabels);
return model;
}
/*
genereate a reconstructed face by backprojecting eigenvectors and eigenvalues of given preprocessed face
@params - FaceRecogniser ; processedFace
@returns - reconstruced Face
*/
Mat recognition::reconstructFace(const Ptr<FaceRecognizer> model, const Mat preprocessedFace)
{
try{
//get required data
Mat eigenvectors = model->get<Mat>("eigenvectors");
Mat averageFaceRow = model->get<Mat>("mean");
int faceHeight = preprocessedFace.rows;
//project input into PCA subspace
Mat projection = subspaceProject(eigenvectors, averageFaceRow, preprocessedFace.reshape(1,1));
//imshow("projection", averageFaceRow);
//generate reconstructed face back form pca
Mat reconstructionRow = subspaceReconstruct(eigenvectors, averageFaceRow, projection);
//convert float row matrix to a regular 8-bit image
//make rectangular
Mat reconstructionMat = reconstructionRow.reshape(1, faceHeight);
//convert to floating point pixels
Mat reconstructedFace = Mat(reconstructionMat.size(), CV_8U);
reconstructionMat.convertTo(reconstructedFace, CV_8U, 1, 0);
return reconstructedFace;
} catch(cv::Exception e){
cout << "error: " << endl;
return Mat();
}
}
/*
compare 2 images by getting the L2 error; (sqrt of sum of squared error)
@params - reconstructed face; capturedface
@returns - similarity
*/
double recognition::getSimilarity(const Mat A, const Mat B)
{
if (A.rows > 0 && A.rows == B.rows && A.cols > 0 && A.cols == B.cols){
//Calculate the L2 relative error
double errorL2 = norm(A,B,CV_L2);
//convert to reasonable scale
double similarity = errorL2/ (double)(A.rows * A.cols);
return similarity;
}
else{
cout << "images have diff size" << endl;
return 100000000.0;
}
}