-
Notifications
You must be signed in to change notification settings - Fork 9
/
mainwindow.cpp
257 lines (216 loc) · 7.59 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
#include <QApplication>
#include <QFileDialog>
#include <QMessageBox>
#include <QPixmap>
#include <QKeyEvent>
#include <QDebug>
//代码中引入头文件
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
#include <QtWidgets>
#endif
#if (QT_VERSION >= QT_VERSION_CHECK(6,0,0))
#include <QtCore5Compat>
#include <QCamera>
#include <QCameraDevice>
#include <QMediaDevices>
#endif
#if (QT_VERSION < QT_VERSION_CHECK(6,0,0))
#include <QCameraInfo>
#endif
#include <QGridLayout>
#include <QIcon>
#include <QStandardItem>
#include <QSize>
#include "mainwindow.h"
#include "utilities.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
, fileMenu(nullptr)
, capturer(nullptr)
{
initUI();
data_lock = new QMutex();
}
MainWindow::~MainWindow()
{
}
void MainWindow::initUI()
{
this->resize(1000, 800);
// setup menubar
fileMenu = menuBar()->addMenu("&File");
// main area
QGridLayout *main_layout = new QGridLayout();
imageScene = new QGraphicsScene(this);
imageView = new QGraphicsView(imageScene);
main_layout->addWidget(imageView, 0, 0, 12, 1);
// tools
QGridLayout *tools_layout = new QGridLayout();
main_layout->addLayout(tools_layout, 12, 0, 1, 1);
shutterButton = new QPushButton(this);
shutterButton->setText("Take a Photo");
tools_layout->addWidget(shutterButton, 0, 0, Qt::AlignHCenter);
connect(shutterButton, SIGNAL(clicked(bool)), this, SLOT(takePhoto()));
// masks
QGridLayout *masks_layout = new QGridLayout();
main_layout->addLayout(masks_layout, 13, 0, 1, 1);
masks_layout->addWidget(new QLabel("Select Masks:", this));
#ifdef USERDEBUG
{
qDebug()<<"CaptureThread::MASK_COUNT"<<CaptureThread::MASK_COUNT;
qDebug()<<"CaptureThread::MOUSE_NOSE"<<CaptureThread::MOUSE_NOSE;
qDebug()<<"CaptureThread::MUSTACHE"<<CaptureThread::MUSTACHE;
qDebug()<<"CaptureThread::GLASSES"<<CaptureThread::GLASSES;
qDebug()<<"CaptureThread::LANDMARKS"<<CaptureThread::LANDMARKS;
qDebug()<<"CaptureThread::RECTANGLE"<<CaptureThread::RECTANGLE;
}
#endif
for (int i = 0; i < CaptureThread::MASK_COUNT; i++){
mask_checkboxes[i] = new QCheckBox(this);
masks_layout->addWidget(mask_checkboxes[i], 0, i + 1);
connect(mask_checkboxes[i], SIGNAL(stateChanged(int)), this, SLOT(updateMasks(int)));
}
mask_checkboxes[0]->setText("Rectangle");
mask_checkboxes[1]->setText("Landmarks");
mask_checkboxes[2]->setText("Glasses");
mask_checkboxes[3]->setText("Mustache");
mask_checkboxes[4]->setText("Mouse Nose");
// list of saved photos
saved_list = new QListView(this);
saved_list->setViewMode(QListView::IconMode);
saved_list->setResizeMode(QListView::Adjust);
saved_list->setSpacing(5);
saved_list->setWrapping(false);
list_model = new QStandardItemModel(this);
saved_list->setModel(list_model);
main_layout->addWidget(saved_list, 14, 0, 4, 1);
QWidget *widget = new QWidget();
widget->setLayout(main_layout);
setCentralWidget(widget);
// setup status bar
mainStatusBar = statusBar();
mainStatusLabel = new QLabel(mainStatusBar);
mainStatusBar->addPermanentWidget(mainStatusLabel);
mainStatusLabel->setText("Facetious is Ready");
createActions();
populateSavedList();
}
void MainWindow::createActions()
{
// create actions, add them to menus
cameraInfoAction = new QAction("Camera &Information", this);
fileMenu->addAction(cameraInfoAction);
openCameraAction = new QAction("&Open Camera", this);
fileMenu->addAction(openCameraAction);
exitAction = new QAction("E&xit", this);
fileMenu->addAction(exitAction);
// connect the signals and slots
connect(exitAction, SIGNAL(triggered(bool)), QApplication::instance(), SLOT(quit()));
connect(cameraInfoAction, SIGNAL(triggered(bool)), this, SLOT(showCameraInfo()));
connect(openCameraAction, SIGNAL(triggered(bool)), this, SLOT(openCamera()));
}
void MainWindow::showCameraInfo()
{
#if (QT_VERSION < QT_VERSION_CHECK(6,0,0))
QList<QCameraInfo> cameras = QCameraInfo::availableCameras();
QString info = QString("Available Cameras: \n");
foreach (const QCameraInfo &cameraInfo, cameras)
info += " - " + cameraInfo.deviceName() + "\n";
QMessageBox::information(this, "Cameras", info);
#else
QMediaDevices devices;
connect(&devices, &QMediaDevices::audioInputsChanged,
[]() { qDebug() << "available audio inputs have changed"; });
#endif
}
void MainWindow::openCamera()
{
if(capturer != nullptr) {
// if a thread is already running, stop it
capturer->setRunning(false);
disconnect(capturer, &CaptureThread::frameCaptured, this, &MainWindow::updateFrame);
disconnect(capturer, &CaptureThread::photoTaken, this, &MainWindow::appendSavedPhoto);
connect(capturer, &CaptureThread::finished, capturer, &CaptureThread::deleteLater);
}
for (int i = 0; i < CaptureThread::MASK_COUNT; i++){
mask_checkboxes[i]->setCheckState(Qt::Unchecked);
}
// I am using my second camera whose Index is 2. Usually, the
// Index of the first camera is 0.
// int camID = 2;
int camID = 0;
capturer = new CaptureThread(camID, data_lock);
connect(capturer, &CaptureThread::frameCaptured, this, &MainWindow::updateFrame);
connect(capturer, &CaptureThread::photoTaken, this, &MainWindow::appendSavedPhoto);
capturer->start();
mainStatusLabel->setText(QString("Capturing Camera %1").arg(camID));
}
void MainWindow::updateFrame(cv::Mat *mat)
{
data_lock->lock();
currentFrame = *mat;
data_lock->unlock();
QImage frame(
currentFrame.data,
currentFrame.cols,
currentFrame.rows,
currentFrame.step,
QImage::Format_RGB888);
QPixmap image = QPixmap::fromImage(frame);
imageScene->clear();
// imageView->resetMatrix();
imageView->resetTransform();
imageScene->addPixmap(image);
imageScene->update();
imageView->setSceneRect(image.rect());
}
void MainWindow::takePhoto()
{
if(capturer != nullptr) {
capturer->takePhoto();
}
}
void MainWindow::populateSavedList()
{
QDir dir(Utilities::getDataPath());
QStringList nameFilters;
nameFilters << "*.jpg";
QFileInfoList files = dir.entryInfoList(
nameFilters, QDir::NoDotAndDotDot | QDir::Files, QDir::Name);
foreach(QFileInfo photo, files) {
QString name = photo.baseName();
QStandardItem *item = new QStandardItem();
list_model->appendRow(item);
QModelIndex index = list_model->indexFromItem(item);
list_model->setData(index, QPixmap(photo.absoluteFilePath()).scaledToHeight(145), Qt::DecorationRole);
list_model->setData(index, name, Qt::DisplayRole);
}
}
void MainWindow::appendSavedPhoto(QString name)
{
QString photo_path = Utilities::getPhotoPath(name, "jpg");
QStandardItem *item = new QStandardItem();
list_model->appendRow(item);
QModelIndex index = list_model->indexFromItem(item);
#ifdef USERDEBUG
{
qDebug()<<"appendSavedPhoto--->index"<<index;
qDebug()<<"appendSavedPhoto--->photo_path"<<photo_path;
}
#endif
list_model->setData(index, QPixmap(photo_path).scaledToHeight(145), Qt::DecorationRole);
list_model->setData(index, name, Qt::DisplayRole);
saved_list->scrollTo(index);
}
void MainWindow::updateMasks(int status)
{
if(capturer == nullptr) {
return;
}
QCheckBox *box = qobject_cast<QCheckBox*>(sender());
for (int i = 0; i < CaptureThread::MASK_COUNT; i++){
if (mask_checkboxes[i] == box) {
capturer->updateMasksFlag(static_cast<CaptureThread::MASK_TYPE>(i), status != 0);
}
}
}