-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
208 lines (167 loc) · 5.05 KB
/
mainwindow.cpp
File metadata and controls
208 lines (167 loc) · 5.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
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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "config.h"
#include "FaceLog.h"
#include "LogMaker.h"
using namespace cv;
using namespace std;
QStringList qList;
QStringList qList2;
int nLinhas;
int sec=1000;
float fps;
FaceLog* faceLog;
LogMaker* logMaker;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->label->setPixmap(QPixmap(QString(":/img/off.png")));
// Connect button signal to appropriate slot
connect(ui->startButton, SIGNAL(released()), this, SLOT(toggleCapture()));
}
MainWindow::~MainWindow()
{
delete faceLog;
delete logMaker;
delete ui;
}
void MainWindow::on_actionPreferences_triggered()
{
if (ui->startButton->text()=="Stop")
{
this->toggleCapture();
}
Config conf(this);
conf.exec();
}
void MainWindow::saveToFile(string fileName){
if (!logMaker)
{
logMaker=new LogMaker(fileName);
}
time_t now;
time(&now);
tm* localTime=localtime(&now);
logMaker->MakeLog(localTime,faceLog->getCountFaces());
}
void MainWindow::updateList(){
this->saveToFile();
qList.append(QString::number(faceLog->getCountFaces()));
if (qList.size()<=5)
{
qList2=qList;
}
else
{
qList2.clear();
int i;
for (i=5;i>0;i--)
{
qList2.append(qList.at(qList.length()-i));
}
}
QStringListModel *listModel=new QStringListModel(qList2);
ui->listView->setModel(listModel);
}
QImage MainWindow::getQImageFromFrame(Mat frame) {
//converts the color model of the image from RGB to BGR because OpenCV uses BGR
cvtColor(frame, frame, CV_RGB2BGR);
return QImage((uchar*) (frame.data), frame.cols, frame.rows, frame.step, QImage::Format_RGB888);
}
void MainWindow::displayFrame() {
faceLog->detectAndDraw(false);
QImage image = getQImageFromFrame(faceLog->getCurrentFrame());
//set the image of the label to be the captured frame and resize the label appropriately
ui->cameraLabel->setPixmap(QPixmap::fromImage(image));
ui->cameraLabel->resize(ui->cameraLabel->pixmap()->size());
}
void MainWindow::toggleCapture() {
if (ui->startButton->text()=="Stop")
{
this->stopCapture();
}
else
{
this->startCapture();
}
}
void MainWindow::startCapture() {
this->loadPreferences(CONFIG_FILE);
faceLog=new FaceLog(this->cameraNumber);
faceLog->setMinSize(Size(this->minSize,this->minSize));
faceLog->setMaxSize(Size(this->maxSize,this->maxSize));
faceLog->startCamera();
this->timer1 = new QTimer(this);
this->timer2 = new QTimer(this);
this->timer1->start(this->interval/1000);
this->timer2->start(this->interval);
this->connect(this->timer1, SIGNAL(timeout()), this, SLOT(displayFrame()));
this->connect(this->timer2, SIGNAL(timeout()), this, SLOT(updateList()));
ui->label->setPixmap(QPixmap(QString(":/img/on.png")));
ui->startButton->setText("Stop");
}
void MainWindow::stopCapture() {
faceLog->stopCamera();
delete faceLog;
logMaker->CloseLog();
this->timer1->stop();
this->timer2->stop();
delete this->timer1;
delete this->timer2;
this->disconnect(this,SLOT(displayFrame()));
this->disconnect(this,SLOT(updateList()));
ui->cameraLabel->setPixmap(QPixmap(""));
ui->label->setPixmap(QPixmap(QString(":/img/off.png")));
ui->startButton->setText("Start");
}
void MainWindow::loadPreferences(QString fileName)
{
QFile config(fileName);
std::string msg;
std::vector< std::vector<std::string> > keyValues;
if(config.exists()){
msg= "File doesn't exist.";
}
if(!config.open(QIODevice::ReadOnly|QIODevice::Text)){
msg="Failed to open file.";
}
else
{
QTextStream in(&config);
while (!in.atEnd()) {
QString line = in.readLine();
keyValues.push_back(split(line.toStdString(), ':'));
}
msg="File loaded.";
}
std::cout << msg<<std::endl;
config.close();
vector< std::vector<std::string> >::iterator keyValuesIterator;
for(keyValuesIterator = keyValues.begin();
keyValuesIterator != keyValues.end();
keyValuesIterator++)
{
std::string key=(*keyValuesIterator)[0];
int val;
std::istringstream ss((*keyValuesIterator)[1]);
ss >> val;
if (key=="minSize")
{
this->minSize=val;
}
if (key=="maxSize")
{
this->maxSize=val;
}
if (key=="interval")
{
this->interval=val;
}
if (key=="camera")
{
this->cameraNumber=val;
}
}
}