-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
435 lines (372 loc) · 16.2 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
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
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->setWindowTitle("Projekt_IM: Mouth");
ui->startButton->setEnabled(false);
ui->stopButton->setEnabled(false);
ui->mouthView->setAlignment(Qt::AlignCenter);
patternPath = qApp->applicationDirPath() + "/patterns/";
patternPreview = new QLabel();
patternPreview->setScaledContents(true);
connect(ui->startButton,SIGNAL(clicked()),this,SLOT(setSteeringEnabled()));
connect(ui->stopButton,SIGNAL(clicked()),this,SLOT(setSteeringDisabled()));
connect(ui->savePatternsButton,SIGNAL(clicked()),this,SLOT(savePatterns()));
connect(ui->loadPatternsButton,SIGNAL(clicked()),this,SLOT(loadPatterns()));
ui->algStateText->setText("Niezainicjowany");
ui->detectedGestText->setText("");
//podpiecie okna konfiguracji
cfgWindow = new ConfigWindow(0);
connect(ui->confWindowButton,SIGNAL(pressed()),cfgWindow,SLOT(show()));
connect(cfgWindow,SIGNAL(minObjSizeChanged(int)),this,SLOT(setMinObjectSize(int)));
connect(cfgWindow,SIGNAL(dblClickDelayChanged(int)),this,SLOT(setDblClickDelay(int)));
connect(cfgWindow,SIGNAL(maxPercDiffChanged(int)),this,SLOT(setMaxPercDiff(int)));
connect(cfgWindow,SIGNAL(sharpenCheckboxClicked(bool)),this,SLOT(setSharpen(bool)));
connect(cfgWindow,SIGNAL(mouseSpeedValueChanged(int)),this,SLOT(setMouseSpeed(int)));
connect(cfgWindow,SIGNAL(colorSpaceChanged(bool)),this,SLOT(setColorSpace(bool)));
connect(cfgWindow,SIGNAL(upPatternSaved()),this,SLOT(saveUpPattern()));
connect(cfgWindow,SIGNAL(downPatternSaved()),this,SLOT(saveDownPattern()));
connect(cfgWindow,SIGNAL(rightPatternSaved()),this,SLOT(saveRightPattern()));
connect(cfgWindow,SIGNAL(leftPatternSaved()),this,SLOT(saveLeftPattern()));
connect(cfgWindow,SIGNAL(clickPatternSaved()),this,SLOT(saveClickPattern()));
connect(cfgWindow,SIGNAL(neutralPatternSaved()),this,SLOT(saveNeutralPattern()));
//-----------------------------------------------
//inicjalizacja parametrow przetwarzania
minObjectSize=150;
maxPercDiff = 0.15;
dblClickDelay = 3;
steeringEnabled = false;
sharpen = true;
mouseSpeed = 5;
colorSpaceBGR = true;
dblClick = false;
allGestures = 0;
otherGestures = 0;
//-----------------------------------------------
//uruchomienie kamery i wczytanie kaskady haar'a
QStringList args = qApp->arguments();
if(args.size() > 1){
videoCapture = new VideoCapture(args.at(1).toStdString()); //otwiera podany film
}
else{
videoCapture = new VideoCapture(-1); //otwiera pierwsza wolna kamere
}
QString cascadePath = qApp->applicationDirPath() +
"/haarcascade_frontalface.xml";
faceCC = new CascadeClassifier(cascadePath.toStdString());
if(videoCapture->isOpened()){
Mat testFrame;
*videoCapture >> testFrame;
if(!testFrame.empty()){
//this->setFixedHeight(testFrame.rows+70);
//this->setFixedWidth(testFrame.cols);
QTimer::singleShot(FRAME_INTERVAL, this, SLOT(updateImages()));
}
}
else{
ui->allFrameView->setText("Nie wykryto kamery!");
ui->mouthView->setText("Nie wykryto kamery!");
}
if(faceCC->empty()){
ui->mouthView->setText("Nie znaleziono pliku z kaskadą Haar'a!\nPowinien mieć nazwę: haarcascade_frontalface.xml");
qDebug() << "Error: Could not load one of cascade classifiers.";
}
//-----------------------------------------------------
}
void MainWindow::updateImages(){
*videoCapture >> frame;
if(!frame.empty()){
//wlaczenie przyciskow kiedy wszystkie gesty sa uzupelnione
if(!patternCLICK.empty() && !patternDOWN.empty()
&& !patternLEFT.empty() && !patternNEUTRAL.empty()
&& !patternRIGHT.empty() && !patternUP.empty()
&& !ui->startButton->isEnabled()
&& !ui->stopButton->isEnabled()){
ui->startButton->setEnabled(true);
ui->stopButton->setEnabled(true);
ui->algStateText->setText("Wyłączony");
}
//---------------------------------------------------------
//jesli przestrzen kamery to RGB a nie BGR to trzeba przekonwertowac
if(!colorSpaceBGR){
cvtColor(frame,frame,CV_RGB2BGR);
}
//wyostrzanie
if(sharpen){
Mat tmp;
GaussianBlur(frame, tmp, cv::Size(0, 0), 3);
addWeighted(frame, 1.5,tmp , -0.5, 0, frame);
}
//---
int facesCount = detectFace(frame, &faceRect, faceCC);
if(facesCount > 0){
calcMouthArea(&mouthRect,faceRect); //wyznaczenie obszary ust
Mat mouth;
mouth = frame(mouthRect).clone(); //wyciecie kawalka z ustami
Mat processedMouth;
//wykrycie ust na podanym kawalku twarzy
detectMouth(mouth,&processedMouth);
//wyciecie malych obiektow ktore nie naleza do ust z obrazu i wypelnienie ust
removeSmallObjects(&processedMouth,minObjectSize);
finalMouth = processedMouth;
QImage mouthImage = convertMatToQImage(finalMouth.clone());
QImage scaledMouth = mouthImage.scaled( mouthImage.width()*4,
mouthImage.height()*4);
ui->mouthView->setPixmap(QPixmap::fromImage(scaledMouth));
//odczytanie stanu ust i wykonanie akcji
//nacisniecie CTRL odblokowuje kursor
if(steeringEnabled &&
QApplication::queryKeyboardModifiers() != Qt::ControlModifier){
MouthState mouthState = detectMouthState(finalMouth,patternUP,
patternDOWN, patternRIGHT,
patternLEFT,patternCLICK,
patternNEUTRAL,maxPercDiff);
if(ui->algStateText->text()!="Włączony"){
ui->algStateText->setText("Włączony");
}
QPoint cursorPos = QCursor::pos();
if(dblClick){allGestures++;}
if(mouthState == MOUTH_LEFT){
cursorPos.setX( cursorPos.x() - mouseSpeed );
if(dblClick){otherGestures++;}
ui->detectedGestText->setText("W lewo");
}
else if(mouthState == MOUTH_RIGHT){
cursorPos.setX( cursorPos.x() + mouseSpeed );
if(dblClick){otherGestures++;}
ui->detectedGestText->setText("W prawo");
}
else if(mouthState == MOUTH_UP){
cursorPos.setY( cursorPos.y() - mouseSpeed );
if(dblClick){otherGestures++;}
ui->detectedGestText->setText("W górę");
}
else if(mouthState == MOUTH_DOWN){
cursorPos.setY( cursorPos.y() + mouseSpeed );
if(dblClick){otherGestures++;}
ui->detectedGestText->setText("W dół");
}
else if(mouthState == MOUTH_CLICK){
if(!dblClick){
leftClick();
dblClick = true;
QTimer::singleShot(dblClickDelay*1000, this,SLOT(checkDblClick()));
}
else{
allGestures++;
}
ui->detectedGestText->setText("Kliknięcie");
}else if(mouthState == MOUTH_NEUTRAL){
if(dblClick){otherGestures++;}
ui->detectedGestText->setText("Neutralny");
}
else{
if(dblClick){otherGestures++;}
ui->detectedGestText->setText("Niezdefiniowany");
}
QCursor::setPos(cursorPos);
/*QString state = (mouthState==MOUTH_UNDEFINED) ? "MOUTH_UNDEFINED" : "";
state = (mouthState==MOUTH_LEFT) ? "MOUTH_LEFT" : state;
state = (mouthState==MOUTH_RIGHT) ? "MOUTH_RIGHT" : state;
state = (mouthState==MOUTH_UP) ? "MOUTH_UP" : state;
state = (mouthState==MOUTH_DOWN) ? "MOUTH_DOWN" : state;
state = (mouthState==MOUTH_CLICK) ? "MOUTH_CLICK" : state;
state = (mouthState==MOUTH_NEUTRAL) ? "MOUTH_NEUTRAL" : state;
qDebug() << state;*/
}else if(steeringEnabled){
ui->algStateText->setText("Wstrzymany");
}else{
ui->algStateText->setText("Wyłączony");
}
//-------------------------------
}
//narysowanie ramki z kamery z zaznaczonym obszarem twarzy i ust
drawRect(frame, faceRect, BLUE);
drawRect(frame, mouthRect, RED);
QImage faceImage = convertMatToQImage(frame);
ui->allFrameView->setPixmap(QPixmap::fromImage(faceImage));
//--------------------------------------------------------------
}
//zakolejkowanie ponownego wywołania
QTimer::singleShot(FRAME_INTERVAL, this,SLOT(updateImages()));
}
QImage MainWindow::convertMatToQImage(const Mat& mat)
{
// 8-bits unsigned, NO. OF CHANNELS=1
if(mat.type()==CV_8UC1)
{
// Set the color table (used to translate colour indexes to qRgb values)
QVector<QRgb> colorTable;
for (int i=0; i<256; i++)
colorTable.push_back(qRgb(i,i,i));
// Copy input Mat
const uchar *qImageBuffer = (const uchar*)mat.data;
// Create QImage with same dimensions as input Mat
QImage img(qImageBuffer, mat.cols, mat.rows, mat.step, QImage::Format_Indexed8);
img.setColorTable(colorTable);
return img;
}
// 8-bits unsigned, NO. OF CHANNELS=3
if(mat.type()==CV_8UC3)
{
// Copy input Mat
const uchar *qImageBuffer = (const uchar*)mat.data;
// Create QImage with same dimensions as input Mat
QImage img(qImageBuffer, mat.cols, mat.rows, mat.step, QImage::Format_RGB888);
return img.rgbSwapped();
}
else
{
qDebug() << "ERROR: Mat could not be converted to QImage.";
return QImage();
}
}
void MainWindow::setMinObjectSize(int value){
minObjectSize = value;
}
void MainWindow::setMaxPercDiff(int value){
maxPercDiff = (((double)value) / 100);
}
void MainWindow::setDblClickDelay(int value){
dblClickDelay = value;
}
void MainWindow::setSharpen(bool value){
sharpen = value;
}
void MainWindow::setMouseSpeed(int val){
this->mouseSpeed = val;
}
void MainWindow::saveUpPattern(){
if(!finalMouth.empty()){
this->patternUP = preparePattern(finalMouth);
showPatternPreview(this->patternUP, "Gest: do góry");
}
}
void MainWindow::saveDownPattern(){
if(!finalMouth.empty()){
this->patternDOWN = preparePattern(finalMouth);
showPatternPreview(this->patternDOWN, "Gest: w dół");
}
}
void MainWindow::saveLeftPattern(){
if(!finalMouth.empty()){
this->patternLEFT = preparePattern(finalMouth);
showPatternPreview(this->patternLEFT, "Gest: w lewo");
}
}
void MainWindow::saveRightPattern(){
if(!finalMouth.empty()){
this->patternRIGHT = preparePattern(finalMouth);
showPatternPreview(this->patternRIGHT, "Gest: w prawo");
}
}
void MainWindow::saveClickPattern(){
if(!finalMouth.empty()){
this->patternCLICK = preparePattern(finalMouth);
showPatternPreview(this->patternCLICK, "Gest: kliknięcie");
}
}
void MainWindow::saveNeutralPattern(){
if(!finalMouth.empty()){
this->patternNEUTRAL = preparePattern(finalMouth);
showPatternPreview(this->patternNEUTRAL, "Gest: neutralny");
}
}
void MainWindow::showPatternPreview(Mat img,QString windowTitle){
QImage image = convertMatToQImage(img);
patternPreview->clear();
patternPreview->setWindowTitle(windowTitle);
patternPreview->setFixedHeight(image.height()*4);
patternPreview->setFixedWidth(image.width()*4);
patternPreview->setPixmap(QPixmap::fromImage(image.scaled( image.width()*4,
image.height()*4)));
patternPreview->show();
}
void MainWindow::closeEvent(QCloseEvent *event){
QMainWindow::closeEvent(event);
patternPreview->close();
cfgWindow->close();
}
void MainWindow::leftClick()
{
INPUT Input={0}; // Create our input.
Input.type = INPUT_MOUSE; // Let input know we are using the mouse.
Input.mi.dwFlags = MOUSEEVENTF_LEFTDOWN; // We are setting left mouse button down.
SendInput( 1, &Input, sizeof(INPUT) ); // Send the input.
ZeroMemory(&Input,sizeof(INPUT)); // Fills a block of memory with zeros.
Input.type = INPUT_MOUSE; // Let input know we are using the mouse.
Input.mi.dwFlags = MOUSEEVENTF_LEFTUP; // We are setting left mouse button up.
SendInput( 1, &Input, sizeof(INPUT) ); // Send the input.
}
void MainWindow::savePatterns(){
if(!patternUP.empty() && !patternDOWN.empty()
&& !patternLEFT.empty() && !patternRIGHT.empty()
&&!patternCLICK.empty() && !patternNEUTRAL.empty()){
imwrite(patternPath.toStdString() + "patternUP.jpg",patternUP);
imwrite(patternPath.toStdString() + "patternDOWN.jpg",patternDOWN);
imwrite(patternPath.toStdString() + "patternLEFT.jpg",patternLEFT);
imwrite(patternPath.toStdString() + "patternRIGHT.jpg",patternRIGHT);
imwrite(patternPath.toStdString() + "patternCLICK.jpg",patternCLICK);
imwrite(patternPath.toStdString() + "patternNEUTRAL.jpg",patternNEUTRAL);
QMessageBox::information(this,"Gesty zapisane","Twoje gesty zostały zapisane.");
}else{
QMessageBox::warning(this,"Gesty nie zostały zapisane","Musisz najpierw ustalić wszystkie gesty.");
}
}
void MainWindow::loadPatterns(){
if(QFile::exists(patternPath+ "patternUP.jpg")
&& QFile::exists(patternPath+ "patternDOWN.jpg")
&& QFile::exists(patternPath+ "patternLEFT.jpg")
&& QFile::exists(patternPath+ "patternRIGHT.jpg")
&& QFile::exists(patternPath+ "patternCLICK.jpg")
&& QFile::exists(patternPath+ "patternNEUTRAL.jpg")){
patternUP = imread(patternPath.toStdString() + "patternUP.jpg",0);
patternDOWN = imread(patternPath.toStdString() + "patternDOWN.jpg",0);
patternLEFT = imread(patternPath.toStdString() + "patternLEFT.jpg",0);
patternRIGHT = imread(patternPath.toStdString() + "patternRIGHT.jpg",0);
patternCLICK = imread(patternPath.toStdString() + "patternCLICK.jpg",0);
patternNEUTRAL = imread(patternPath.toStdString() + "patternNEUTRAL.jpg",0);
if(patternUP.type() != CV_8UC1
|| patternDOWN.type() != CV_8UC1
|| patternLEFT.type() != CV_8UC1
|| patternRIGHT.type() != CV_8UC1
|| patternCLICK.type() != CV_8UC1
|| patternNEUTRAL.type() != CV_8UC1){
patternUP = Mat();
patternDOWN = Mat();
patternLEFT = Mat();
patternRIGHT = Mat();
patternCLICK = Mat();
patternNEUTRAL = Mat();
QMessageBox::warning(this,"Nie wczytano gestów","Odnaleziono pliki, jednak były one nieprawidłowego typu.");
}
else{
QMessageBox::information(this,"Gesty zostały wczytane","Twoje gesty zostały wczytane.");
}
}
else{
QMessageBox::warning(this,"Nie wczytano gestów","Nie odnaleziono wszystkich gestów.");
}
}
void MainWindow::checkDblClick(){
float ratio = (float)otherGestures / (float)allGestures;
if(ratio < 0.25){
doubleLeftClick();
ui->detectedGestText->setText("Podwójne kliknięcie");
//qDebug() << ">>DOUBLE CLICK<<";
}
otherGestures = 0;
allGestures = 0;
dblClick = false;
}
MainWindow::~MainWindow()
{
delete cfgWindow;
delete faceCC;
delete videoCapture;
delete ui;
delete patternPreview;
}