-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
281 lines (245 loc) · 8.83 KB
/
main.cpp
File metadata and controls
281 lines (245 loc) · 8.83 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
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
#include "objectDetection.h"
#include "recognition.h"
#include "captureimages.h"
#include "opencv2/opencv.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/video/video.hpp"
#include "opencv2/objdetect/objdetect.hpp"
#include<iostream>
#include<vector>
#include<string>
#include<stdio.h>
#include <stdexcept>
#include <QTimer>
using namespace cv;
using namespace std;
//function to print useful data
template <typename T> string toString(T t)
{
ostringstream out;
out << t;
return out.str();
}
//define variables to be used in program
const int CAMERA_WIDTH = 640;
const int CAMERA_HEIGHT = 480;
#ifdef IMX6
const string DATABASE_DIR = "/nvdata/config/faces/";
#else
const string DATABASE_DIR = "/home/standby/Projects/faceRecognition/faces/";
#endif
const string EXT = ".png";
string Name;
const float DETECTION_THRESHOLD = 0.7f;
const int TIMEOUT = 75;
const int matchThreshold = 5;
const int ESC_KEY = 27;
const int ENTER_KEY = 13;
const int SPACE_KEY = 32;
//function prototypes
void initCamera(VideoCapture &capture);
void detectAndRecognise(VideoCapture &capture, CascadeClassifier &faceCascade, CascadeClassifier &eyeCascade, CascadeClassifier &eyeGlassCascade);
void storeFaces(Mat &processedFace, vector<Mat>& preProcessedFaces, vector<int>& faceLabels);
void writeImage(Mat &image, string name);
detectObject detection;
recognition faceRecognition;
captureImages captureImage;
/*
Program entry point - initialises cascades and camera
then enters program loop
*/
int main (int argc, char* argv[])
{
if (argc > 0){
Name = argv[1];
}
CascadeClassifier faceCascade;
CascadeClassifier eyeCascade;
CascadeClassifier eyeGlassCascade;
VideoCapture capture;
//initialise the three cascade classifiers to be used
//initCascades(faceCascade, eyeCascade, eyeGlassCascade);
detection.initCascades(faceCascade, eyeCascade, eyeGlassCascade);
//initialise the camera
initCamera(capture);
//check if user exists
//enter program loop
detectAndRecognise(capture, faceCascade, eyeCascade, eyeGlassCascade);
return 0;
}
/*
Initialises and opens camera stream
@params VideoCapture
*/
void initCamera(VideoCapture& capture)
{
try{
capture.open(0);
capture.set(CV_CAP_PROP_FRAME_WIDTH,CAMERA_WIDTH);
capture.set(CV_CAP_PROP_FRAME_HEIGHT, CAMERA_HEIGHT);
if(capture.isOpened()){
cout << "Stream opened sucessfully" << endl;
}else{
cout << "Error opening stream" << endl;
}
}
catch(cv::Exception &e){
cout << "Error: " << endl;
}
}
/*
Main program loop
Loads database image, processess it and then trains the FaceRecogniser
Streams camera image, on button press captures frame, processess and compares
@params VideoCapture; FaceCascade; eyeCascade; eyeGlassCascade
*/
void detectAndRecognise(VideoCapture &capture, CascadeClassifier& faceCascade, CascadeClassifier& eyeCascade, CascadeClassifier& eyeGlassCascade)
{
cout << "Face Recognition with open cv" << endl;
cout << "press 'spacebar' to capture image and compare with database" << endl;
cout << "press 'enter' to add user" << endl;
cout << "press 'esc' to exit" << endl;
vector<Mat> userFaces;
Ptr<FaceRecognizer> model;
vector<Mat> preProcessedFaces;
vector<Mat>capturedAndProcessedFaces;
vector<int> faceLabels;
Mat referenceFace;
Mat processedImage;
Mat frame;
Mat userFace;
string databaseImage;
int matches = 0;
double similarity;
int identity = -1;
databaseImage = DATABASE_DIR + Name + EXT;
//Try to load image specified by Name
//if fail enter loop waiting for enter keypress
try{
referenceFace = imread(databaseImage,1);
}catch(cv::Exception&e){};
if (referenceFace.empty()){
cout << "User : " << Name << " not found, press Enter to add photo" << endl;
while (true){
capture >> frame;
imshow("stream", frame);
char c = waitKey(20);
if (c == 13){
writeImage(frame, Name);
referenceFace = imread(databaseImage, 1);
break;
}
}
}
//put image through preProcessing - returns a Mat of the face ROI
processedImage = detection.processImage(referenceFace, faceCascade, eyeCascade, eyeGlassCascade);
if(!processedImage.empty()){
imshow("processed", processedImage);
//Add the processed face to the array
//Train the recogniser
storeFaces(processedImage, preProcessedFaces, faceLabels);
model = faceRecognition.learnCollectedFaces(preProcessedFaces, faceLabels);
}
//free up resources
processedImage.release();
bool keyPressed =false;
while(true)
{
//stream camera image to gui window
capture >> frame;
imshow("stream", frame);
if (!captureImage.timer->isActive() && keyPressed ){ //if timer has elapsed and key has been pressed
captureImage.startTimer(TIMEOUT, capture, userFaces);
keyPressed = false;
int size = userFaces.size();
if(size != 0){ //confirm that array has been filled
imshow("userface", userFaces.at(9));
int faceCount = userFaces.size();
//process each captured image
for (int i = 0; i < faceCount; i++){
Mat face = userFaces.at(i);
//imshow(toString(i), face);
userFace = detection.processImage(face, faceCascade, eyeCascade, eyeGlassCascade);
if(!userFace.empty()){ //if processing successful
//imshow("userFace", userFace);
capturedAndProcessedFaces.push_back(userFace);
}
}
//use successfully processed images for face recogniser
for (int i = 0; i < (int)capturedAndProcessedFaces.size(); i++){
Mat reconstructedFace = faceRecognition.reconstructFace(model, userFace); //project to pca space
similarity = faceRecognition.getSimilarity(userFace, reconstructedFace); //compare with stored images
string output;
if (similarity < DETECTION_THRESHOLD){
identity = model->predict(userFace);
output = toString(identity);
storeFaces(userFace, preProcessedFaces, faceLabels);
//model = faceRecognition.learnCollectedFaces(preProcessedFaces, faceLabels); //re-train face rec with more matches
matches++;
}else{
output = "Unknown";
}
}
if(matches >= matchThreshold){
cout << "Identity: " << Name << ". Similarity: " << similarity << " matches: " << matches << endl;
}else{
cout << "insufficient match count: " << matches << endl;
}
}else{
cout << "image processing failed" << endl;
}
}
char c = waitKey(20);
if (c == ESC_KEY){ //if esc key leave program
break;
}else if(c == ENTER_KEY){ //if enter
writeImage(frame, Name);
}
else if(c == SPACE_KEY){ //if spacebar capture frame and run detection program
keyPressed=true;
captureImage.startTimer(TIMEOUT, capture, userFaces);
}
}
cvDestroyAllWindows();
return;
}
/*
Adds processed face image to array of faces
@params processedFace(single image); preProcessedFaces(array); faceLabels(array)
*/
void storeFaces(Mat &processedFace, vector<Mat>& preProcessedFaces, vector<int>& faceLabels)
{
Mat mirror;
flip(processedFace, mirror, 1);
//flip and store image so that FaceRecognizer has more training data
preProcessedFaces.push_back(processedFace);
preProcessedFaces.push_back(mirror);
//only one user so label is always the same
faceLabels.push_back(0);
faceLabels.push_back(0);
cout << "processed faces: " << preProcessedFaces.size() << endl;
return;
}
/*
writes image to specified location in png format
@params - image, name(string);
*/
void writeImage(Mat &image, string name)
{
//set parameters fopr appropriate file formate
vector<int> compression_params;
compression_params.push_back(CV_IMWRITE_PNG_COMPRESSION);
compression_params.push_back(9);
string filename = DATABASE_DIR + name+".png";
//write image
try {
imwrite(filename, image, compression_params);
}
catch (cv::Exception &ex) {
fprintf(stderr, "Exception converting image to PNG format: %s\n", ex.what());
return;
}
fprintf(stdout, "Saved PNG file with alpha data.\n");
return;
}