-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathmainwindow.cpp
264 lines (232 loc) · 11.9 KB
/
mainwindow.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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtCore>
#include <QFileDialog>
#include <QDebug>
#include <ctime>
#include <sstream>
#include <string>
#include <iostream>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/contrib/contrib.hpp>
#include <opencv2/objdetect/objdetect.hpp>
/** Global variables **/
std::string faceCascadeName = "/home/josh/projects/uWho/lbpcascade_frontalface.xml";
std::string eyesCascadeName = "/home/josh/projects/uWho/haarcascade_eye_tree_eyeglasses.xml";
cv::CascadeClassifier faceCascade;
cv::CascadeClassifier eyesCascade;
std::string face_file = "/home/josh/projects/uWho/face.xml";
QFile face("/home/josh/projects/uWho/face.xml");
/** end of global variables **/
using namespace std;
using namespace cv;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QPixmap webcam("/home/josh/projects/uWho/webcam.png");
QPixmap videofile("/home/josh/projects/uWho/videofile.png");
ui->webcamButton->setIcon(webcam);
ui->videofileButton->setIcon(videofile);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_videofileButton_clicked()
{
//Below is the open file dialog, and saves said file selected to videoFileNames
QFileDialog videoFile(this);
videoFile.setFileMode(QFileDialog::AnyFile);
videoFile.setNameFilter(tr("Images (*.avi *.mpg *.mpeg *.mkv *.webm *.ogv *.mp4)"));
videoFile.setViewMode(QFileDialog::List);
QStringList vFN;
if (videoFile.exec()){
vFN = videoFile.selectedFiles();
}
QString videoFileName;
if(vFN.size()!=NULL){
videoFileName = vFN.at(0);
}else{
return;
}
qDebug() << ( videoFileName.toUtf8().constData() ) ;
cv::VideoCapture cap = cv::VideoCapture(( videoFileName.toUtf8().constData() ));
qDebug() << "Video loaded" ;
long int videoFrameCount = cap.get(CV_CAP_PROP_FRAME_COUNT);
// Startup of Recognizer defaults
std::srand(std::time(NULL));
Ptr<cv::FaceRecognizer> model = cv::createLBPHFaceRecognizer(1,8,8,8, 100);
if (face.exists()){
model->load(face_file);
qDebug() << "Loaded model." ;
}else{
qDebug() << "Generating starting model..." ;
vector<cv::Mat> images (10);
vector<int> labels (10);
images[0] = (imread("/home/josh/projects/uWho/startingfaces/josh1.png", CV_LOAD_IMAGE_GRAYSCALE));
labels[0] = 0;
images[1] = (imread("/home/josh/projects/uWho/startingfaces/josh2.png", CV_LOAD_IMAGE_GRAYSCALE));
labels[1] = 0;
images[2] = (imread("/home/josh/projects/uWho/startingfaces/josh3.png", CV_LOAD_IMAGE_GRAYSCALE));
labels[2] = 0;
images[3] = (imread("/home/josh/projects/uWho/startingfaces/josh4.png", CV_LOAD_IMAGE_GRAYSCALE));
labels[3] = 0;
images[4] = (imread("/home/josh/projects/uWho/startingfaces/josh5.png", CV_LOAD_IMAGE_GRAYSCALE));
labels[4] = 0;
images[5] = (imread("/home/josh/projects/uWho/startingfaces/josh6.png", CV_LOAD_IMAGE_GRAYSCALE));
labels[5] = 0;
images[6] = (imread("/home/josh/projects/uWho/startingfaces/josh7.png", CV_LOAD_IMAGE_GRAYSCALE));
labels[6] = 0;
images[7] = (imread("/home/josh/projects/uWho/startingfaces/josh8.png", CV_LOAD_IMAGE_GRAYSCALE));
labels[7] = 0;
images[8] = (imread("/home/josh/projects/uWho/startingfaces/josh9.png", CV_LOAD_IMAGE_GRAYSCALE));
labels[8] = 0;
images[9] = (imread("/home/josh/projects/uWho/startingfaces/josh10.png", CV_LOAD_IMAGE_GRAYSCALE));
labels[9] = 0;
model->train(images, labels);
cv::Mat testingImage = (imread("/home/josh/projects/uWho/startingfaces/josh11.png", CV_LOAD_IMAGE_GRAYSCALE));
int predicted = -1; // Sanity check. We throw a face I know is mine to the predictor.
double confidence ;
model->predict(testingImage, predicted, confidence);
qDebug() << "Testing predicted/confidence: " << predicted << confidence ;
}
cv::namedWindow("VidWindow");
cv::Mat frame;
unsigned long frameNumber = 0;
do{
cap.read(frame);
if(!frame.empty()){
frameNumber = frameNumber + 1 ;
std::ostringstream a;
a << frameNumber ;
cv::putText(frame, a.str(), cv::Point(0,20),FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(255,255,255), 1,8, false);
cv::blur(frame, frame, cv::Size(3,3));
faceCascade.load(faceCascadeName);
eyesCascade.load(eyesCascadeName);
std::vector<cv::Rect> faces;
cv::Mat frame_gray;
cv::cvtColor(frame, frame_gray, CV_BGR2GRAY);
faceCascade.detectMultiScale(frame_gray, faces, 1.1, 3, CV_HAAR_SCALE_IMAGE, cv::Size(50,50));
for(int i = 0; i < faces.size(); i++){
std::vector<cv::Mat> facePicture (1);
std::vector<int> faceIndex (1);
std::vector<Rect> eyes;
facePicture[0] = frame(faces[i]); // Gets the face only as the variable facePicture
cv::rectangle(frame, faces[i], cv::Scalar(255,0,255), 1, 8, 0); // Draws rectangles on webcam video
string faceString = static_cast<ostringstream*>( &(ostringstream() << i) )->str();
cv::putText(frame, faceString, cv::Point(faces[i].x, (faces[i].y+10)),FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(255,255,255), 1,8, false);
eyesCascade.detectMultiScale( facePicture[0], eyes, 1.1, 2, 0 |CV_HAAR_SCALE_IMAGE, Size(30, 30) );
if (eyes.size() != 0){
cv::cvtColor(facePicture[0], facePicture[0], CV_BGR2GRAY); // colorspace change to gray
int predicted = -1;
double confidence ;
model->predict(facePicture[0], predicted, confidence); // Check the machine learner and ask if it's seen this face before
string predictString = static_cast<ostringstream*>( &(ostringstream() << predicted) )->str();
cv::putText(frame, predictString, cv::Point((faces[i].x + faces[i].width - 40), (faces[i].y + 10)),FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(255,255,255), 1,8, false);
if (predicted == -1){
faceIndex[0] = std::rand()%30000;
model->update(facePicture,faceIndex); // If its not in the FaceRecognizer, add it
}else{
faceIndex[0] = predicted;
model->update(facePicture,faceIndex); // if the face is already in, add this as another data point
}
qDebug() << "face ID#" << predicted << "confidence :"<< confidence;
}
}
}
std::ostringstream b;
b << videoFrameCount ;
cv::putText(frame, b.str(), cv::Point((cap.get(CV_CAP_PROP_FRAME_WIDTH)-61),20),FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(255,255,255), 1,8, false);
imshow("VidWindow" ,frame);
}while((cv::waitKey(1)<30) || frame.empty() );
model->save(face_file);
cv::destroyWindow("VidWindow");
}
void MainWindow::on_webcamButton_clicked()
{
std::srand(std::time(NULL));
Ptr<cv::FaceRecognizer> model = cv::createLBPHFaceRecognizer(1,8,8,8, 100);
if (face.exists()){
model->load(face_file);
qDebug() << "Loaded model." ;
}else{
qDebug() << "Generating starting model..." ;
vector<cv::Mat> images (10);
vector<int> labels (10);
images[0] = (imread("/home/josh/projects/uWho/startingfaces/josh1.png", CV_LOAD_IMAGE_GRAYSCALE));
labels[0] = 0;
images[1] = (imread("/home/josh/projects/uWho/startingfaces/josh2.png", CV_LOAD_IMAGE_GRAYSCALE));
labels[1] = 0;
images[2] = (imread("/home/josh/projects/uWho/startingfaces/josh3.png", CV_LOAD_IMAGE_GRAYSCALE));
labels[2] = 0;
images[3] = (imread("/home/josh/projects/uWho/startingfaces/josh4.png", CV_LOAD_IMAGE_GRAYSCALE));
labels[3] = 0;
images[4] = (imread("/home/josh/projects/uWho/startingfaces/josh5.png", CV_LOAD_IMAGE_GRAYSCALE));
labels[4] = 0;
images[5] = (imread("/home/josh/projects/uWho/startingfaces/josh6.png", CV_LOAD_IMAGE_GRAYSCALE));
labels[5] = 0;
images[6] = (imread("/home/josh/projects/uWho/startingfaces/josh7.png", CV_LOAD_IMAGE_GRAYSCALE));
labels[6] = 0;
images[7] = (imread("/home/josh/projects/uWho/startingfaces/josh8.png", CV_LOAD_IMAGE_GRAYSCALE));
labels[7] = 0;
images[8] = (imread("/home/josh/projects/uWho/startingfaces/josh9.png", CV_LOAD_IMAGE_GRAYSCALE));
labels[8] = 0;
images[9] = (imread("/home/josh/projects/uWho/startingfaces/josh10.png", CV_LOAD_IMAGE_GRAYSCALE));
labels[9] = 0;
model->train(images, labels);
qDebug() << "Training successful";
cv::Mat testingImage = (imread("/home/josh/projects/uWho/startingfaces/josh11.png", CV_LOAD_IMAGE_GRAYSCALE));
int predicted = -1; // Sanity check. We throw a face I know is mine to the predictor.
double confidence ;
model->predict(testingImage, predicted, confidence);
qDebug() << "Testing predicted/confidence: " << predicted << confidence ;
}
cv::namedWindow("VidWindow");
cv::VideoCapture cap = cv::VideoCapture(0);
cap.set(CV_CAP_PROP_FRAME_WIDTH, 800 );
cap.set(CV_CAP_PROP_FRAME_HEIGHT, 600 );
cv::Mat frame;
do{
cap >> frame;
if(!frame.empty()){
cv::blur(frame, frame, cv::Size(3,3));
faceCascade.load(faceCascadeName);
eyesCascade.load(eyesCascadeName);
std::vector<cv::Rect> faces;
cv::Mat frame_gray;
cv::cvtColor(frame, frame_gray, CV_BGR2GRAY);
faceCascade.detectMultiScale(frame_gray, faces, 1.1, 3, CV_HAAR_SCALE_IMAGE, cv::Size(50,50));
for(int i = 0; i < faces.size(); i++){
std::vector<cv::Mat> facePicture (1);
std::vector<int> faceIndex (1);
std::vector<Rect> eyes;
facePicture[0] = frame(faces[i]); // Gets the face only as the variable facePicture
cv::rectangle(frame, faces[i], cv::Scalar(255,0,255), 1, 8, 0); // Draws rectangles on webcam video
string faceString = static_cast<ostringstream*>( &(ostringstream() << i) )->str();
cv::putText(frame, faceString, cv::Point(faces[i].x, (faces[i].y+30)),FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(255,255,255), 1,8, false);
eyesCascade.detectMultiScale( facePicture[0], eyes, 1.1, 2, 0 |CV_HAAR_SCALE_IMAGE, Size(30, 30) );
if (eyes.size() != 0){
cv::cvtColor(facePicture[0], facePicture[0], CV_BGR2GRAY); // colorspace change to gray
int predicted = -1;
double confidence ;
model->predict(facePicture[0], predicted, confidence); // Check the machine learner and ask if it's seen this face before
string predictString = static_cast<ostringstream*>( &(ostringstream() << predicted) )->str();
cv::putText(frame, predictString, cv::Point((faces[i].x + faces[i].width - 40), (faces[i].y + 30)),FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(255,255,255), 1,8, false);
if (predicted == -1){
faceIndex[0] = std::rand()%30000;
model->update(facePicture,faceIndex); // If its not in the FaceRecognizer, add it
}else{
faceIndex[0] = predicted;
model->update(facePicture,faceIndex); // if the face is already in, add this as another data point
}
qDebug() << "face # " << predicted << confidence;
}
}
imshow("VidWindow" ,frame);}
}while(cv::waitKey(30)<30);
model->save(face_file);
cv::destroyWindow("VidWindow");
}