-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCameraView.cpp
More file actions
504 lines (458 loc) · 21.8 KB
/
Copy pathCameraView.cpp
File metadata and controls
504 lines (458 loc) · 21.8 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
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
/************************************************************************/
/* qt-opencv-multithreaded: */
/* A multithreaded OpenCV application using the Qt framework. */
/* */
/* CameraView.cpp */
/* */
/* Nick D'Ademo <nickdademo@gmail.com> */
/* */
/* Copyright (c) 2012-2013 Nick D'Ademo */
/* */
/* Permission is hereby granted, free of charge, to any person */
/* obtaining a copy of this software and associated documentation */
/* files (the "Software"), to deal in the Software without restriction, */
/* including without limitation the rights to use, copy, modify, merge, */
/* publish, distribute, sublicense, and/or sell copies of the Software, */
/* and to permit persons to whom the Software is furnished to do so, */
/* subject to the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */
/* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS */
/* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN */
/* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN */
/* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */
/* SOFTWARE. */
/* */
/************************************************************************/
#include "CameraView.h"
#include "ui_CameraView.h"
#include <QSound>
// Qt
#include <QMessageBox>
CameraView::CameraView(QWidget *parent, int deviceNumber, SharedImageBuffer *sharedImageBuffer) :
QWidget(parent),
ui(new Ui::CameraView),
sharedImageBuffer(sharedImageBuffer)
{
// Setup UI
ui->setupUi(this);
// Save Device Number
this->deviceNumber=deviceNumber;
// Initialize internal flag
isCameraConnected=false;
// Set initial GUI state
ui->frameLabel->setText("No camera connected.");
ui->imageBufferBar->setValue(0);
ui->captureRateLabel->setText("");
ui->processingRateLabel->setText("");
ui->deviceNumberLabel->setText("");
ui->cameraResolutionLabel->setText("");
ui->roiLabel->setText("");
ui->mouseCursorPosLabel->setText("");
ui->clearImageBufferButton->setDisabled(true);
// Initialize ImageProcessingFlags structure
imageProcessingFlags.grayscaleOn=false;
imageProcessingFlags.smoothOn=false;
imageProcessingFlags.dilateOn=false;
imageProcessingFlags.erodeOn=false;
imageProcessingFlags.flipOn=false;
imageProcessingFlags.cannyOn=false;
// Connect signals/slots
connect(ui->frameLabel, SIGNAL(onMouseMoveEvent()), this, SLOT(updateMouseCursorPosLabel()));
connect(ui->clearImageBufferButton, SIGNAL(released()), this, SLOT(clearImageBuffer()));
connect(ui->frameLabel->menu, SIGNAL(triggered(QAction*)), this, SLOT(handleContextMenuAction(QAction*)));
// Register type
qRegisterMetaType<struct ThreadStatisticsData>("ThreadStatisticsData");
if (databaseTest()) {
connectDatabase();
qDebug() << "Connecting to database";
}
code = "";
}
CameraView::~CameraView()
{
if(isCameraConnected)
{
// Stop processing thread
if(processingThread->isRunning())
stopProcessingThread();
// Stop capture thread
if(captureThread->isRunning())
stopCaptureThread();
// Automatically start frame processing (for other streams)
if(sharedImageBuffer->isSyncEnabledForDeviceNumber(deviceNumber))
sharedImageBuffer->setSyncEnabled(true);
// Remove from shared buffer
sharedImageBuffer->removeByDeviceNumber(deviceNumber);
// Disconnect camera
if(captureThread->disconnectCamera())
qDebug() << "[" << deviceNumber << "] Camera successfully disconnected.";
else
qDebug() << "[" << deviceNumber << "] WARNING: Camera already disconnected.";
if (db.isOpen()) {
db.close();
qDebug() << "[" << deviceNumber << "] WARNING: Close Database Connection.";
}
}
// Delete UI
delete ui;
}
bool CameraView::connectToCamera(bool dropFrameIfBufferFull, int capThreadPrio, int procThreadPrio, bool enableFrameProcessing, int width, int height)
{
// Set frame label text
if(sharedImageBuffer->isSyncEnabledForDeviceNumber(deviceNumber))
ui->frameLabel->setText("摄像头已连接,请稍后...");
else
ui->frameLabel->setText("正在连接到摄像头...");
// Create capture thread
captureThread = new CaptureThread(sharedImageBuffer, deviceNumber, dropFrameIfBufferFull, width, height);
// Attempt to connect to camera
if(captureThread->connectToCamera())
{
// Create processing thread
processingThread = new ProcessingThread(sharedImageBuffer, deviceNumber);
// Create image processing settings dialog
imageProcessingSettingsDialog = new ImageProcessingSettingsDialog(this);
// Setup signal/slot connections
connect(processingThread, SIGNAL(newFrame(QImage)), this, SLOT(updateFrame(QImage)));
connect(processingThread, SIGNAL(updateStatisticsInGUI(struct ThreadStatisticsData)), this, SLOT(updateProcessingThreadStats(struct ThreadStatisticsData)));
connect(captureThread, SIGNAL(updateStatisticsInGUI(struct ThreadStatisticsData)), this, SLOT(updateCaptureThreadStats(struct ThreadStatisticsData)));
connect(imageProcessingSettingsDialog, SIGNAL(newImageProcessingSettings(struct ImageProcessingSettings)), processingThread, SLOT(updateImageProcessingSettings(struct ImageProcessingSettings)));
connect(this, SIGNAL(newImageProcessingFlags(struct ImageProcessingFlags)), processingThread, SLOT(updateImageProcessingFlags(struct ImageProcessingFlags)));
connect(this, SIGNAL(setROI(QRect)), processingThread, SLOT(setROI(QRect)));
connect(this, SIGNAL(setReceipt(QString)), processingThread, SLOT(captureScreen(QString)));
connect(this, SIGNAL(setLocalSavePath(QString)), processingThread, SLOT(setLocalSavePath(QString)));
// Only enable ROI setting/resetting if frame processing is enabled
if(enableFrameProcessing)
connect(ui->frameLabel, SIGNAL(newMouseData(struct MouseData)), this, SLOT(newMouseData(struct MouseData)));
// Set initial data in processing thread
emit setROI(QRect(0, 0, captureThread->getInputSourceWidth(), captureThread->getInputSourceHeight()));
emit newImageProcessingFlags(imageProcessingFlags);
imageProcessingSettingsDialog->updateStoredSettingsFromDialog();
// Start capturing frames from camera
captureThread->start((QThread::Priority)capThreadPrio);
// Start processing captured frames (if enabled)
if(enableFrameProcessing)
processingThread->start((QThread::Priority)procThreadPrio);
// Setup imageBufferBar with minimum and maximum values
ui->imageBufferBar->setMinimum(0);
ui->imageBufferBar->setMaximum(sharedImageBuffer->getByDeviceNumber(deviceNumber)->maxSize());
// Enable "Clear Image Buffer" push button
ui->clearImageBufferButton->setEnabled(true);
// Set text in labels
ui->deviceNumberLabel->setNum(deviceNumber);
ui->cameraResolutionLabel->setText(QString::number(captureThread->getInputSourceWidth())+QString("x")+QString::number(captureThread->getInputSourceHeight()));
// Set internal flag and return
isCameraConnected=true;
// Set frame label text
if(!enableFrameProcessing)
ui->frameLabel->setText("Frame processing disabled.");
return true;
}
// Failed to connect to camera
else
return false;
}
void CameraView::stopCaptureThread()
{
qDebug() << "[" << deviceNumber << "] About to stop capture thread...";
captureThread->stop();
sharedImageBuffer->wakeAll(); // This allows the thread to be stopped if it is in a wait-state
// Take one frame off a FULL queue to allow the capture thread to finish
if(sharedImageBuffer->getByDeviceNumber(deviceNumber)->isFull())
sharedImageBuffer->getByDeviceNumber(deviceNumber)->get();
captureThread->wait();
qDebug() << "[" << deviceNumber << "] Capture thread successfully stopped.";
}
void CameraView::stopProcessingThread()
{
qDebug() << "[" << deviceNumber << "] About to stop processing thread...";
processingThread->stop();
sharedImageBuffer->wakeAll(); // This allows the thread to be stopped if it is in a wait-state
processingThread->wait();
qDebug() << "[" << deviceNumber << "] Processing thread successfully stopped.";
}
void CameraView::updateCaptureThreadStats(struct ThreadStatisticsData statData)
{
// Show [number of images in buffer / image buffer size] in imageBufferLabel
// Show percentage of image bufffer full in imageBufferBar
ui->imageBufferBar->setValue(sharedImageBuffer->getByDeviceNumber(deviceNumber)->size());
// Show processing rate in captureRateLabel
ui->captureRateLabel->setText(QString::number(statData.averageFPS)+" fps");
// Show number of frames captured in nFramesCapturedLabel
// ui->nFramesCapturedLabel->setText(QString("[") + QString::number(statData.nFramesProcessed) + QString("]"));
}
void CameraView::updateProcessingThreadStats(struct ThreadStatisticsData statData)
{
// Show processing rate in processingRateLabel
ui->processingRateLabel->setText(QString::number(statData.averageFPS)+" fps");
// Show ROI information in roiLabel
ui->roiLabel->setText(QString("(")+QString::number(processingThread->getCurrentROI().x())+QString(",")+
QString::number(processingThread->getCurrentROI().y())+QString(") ")+
QString::number(processingThread->getCurrentROI().width())+
QString("x")+QString::number(processingThread->getCurrentROI().height()));
// Show number of frames processed in nFramesProcessedLabel
// ui->nFramesProcessedLabel->setText(QString("[") + QString::number(statData.nFramesProcessed) + QString("]"));
}
void CameraView::updateFrame(const QImage &frame)
{
// Display frame
ui->frameLabel->setPixmap(QPixmap::fromImage(frame).scaled(ui->frameLabel->width(), ui->frameLabel->height(),Qt::KeepAspectRatio));
}
void CameraView::clearImageBuffer()
{
if(sharedImageBuffer->getByDeviceNumber(deviceNumber)->clear())
qDebug() << "[" << deviceNumber << "] Image buffer successfully cleared.";
else
qDebug() << "[" << deviceNumber << "] WARNING: Could not clear image buffer.";
}
void CameraView::setImageProcessingSettings()
{
// Prompt user:
// If user presses OK button on dialog, update image processing settings
if(imageProcessingSettingsDialog->exec()==QDialog::Accepted)
imageProcessingSettingsDialog->updateStoredSettingsFromDialog();
// Else, restore dialog state
else
imageProcessingSettingsDialog->updateDialogSettingsFromStored();
}
void CameraView::updateMouseCursorPosLabel()
{
// Update mouse cursor position in mouseCursorPosLabel
ui->mouseCursorPosLabel->setText(QString("(")+QString::number(ui->frameLabel->getMouseCursorPos().x())+
QString(",")+QString::number(ui->frameLabel->getMouseCursorPos().y())+
QString(")"));
// Show pixel cursor position if camera is connected (image is being shown)
if(ui->frameLabel->pixmap()!=0)
{
// Scaling factor calculation depends on whether frame is scaled to fit label or not
if(!ui->frameLabel->hasScaledContents())
{
double xScalingFactor=((double) ui->frameLabel->getMouseCursorPos().x() - ((ui->frameLabel->width() - ui->frameLabel->pixmap()->width()) / 2)) / (double) ui->frameLabel->pixmap()->width();
double yScalingFactor=((double) ui->frameLabel->getMouseCursorPos().y() - ((ui->frameLabel->height() - ui->frameLabel->pixmap()->height()) / 2)) / (double) ui->frameLabel->pixmap()->height();
ui->mouseCursorPosLabel->setText(ui->mouseCursorPosLabel->text()+
QString(" [")+QString::number((int)(xScalingFactor*processingThread->getCurrentROI().width()))+
QString(",")+QString::number((int)(yScalingFactor*processingThread->getCurrentROI().height()))+
QString("]"));
}
else
{
double xScalingFactor=(double) ui->frameLabel->getMouseCursorPos().x() / (double) ui->frameLabel->width();
double yScalingFactor=(double) ui->frameLabel->getMouseCursorPos().y() / (double) ui->frameLabel->height();
ui->mouseCursorPosLabel->setText(ui->mouseCursorPosLabel->text()+
QString(" [")+QString::number((int)(xScalingFactor*processingThread->getCurrentROI().width()))+
QString(",")+QString::number((int)(yScalingFactor*processingThread->getCurrentROI().height()))+
QString("]"));
}
}
}
void CameraView::newMouseData(struct MouseData mouseData)
{
// Local variable(s)
int x_temp, y_temp, width_temp, height_temp;
QRect selectionBox;
// Set ROI
if(mouseData.leftButtonRelease)
{
double xScalingFactor;
double yScalingFactor;
double wScalingFactor;
double hScalingFactor;
// Selection box calculation depends on whether frame is scaled to fit label or not
if(!ui->frameLabel->hasScaledContents())
{
xScalingFactor=((double) mouseData.selectionBox.x() - ((ui->frameLabel->width() - ui->frameLabel->pixmap()->width()) / 2)) / (double) ui->frameLabel->pixmap()->width();
yScalingFactor=((double) mouseData.selectionBox.y() - ((ui->frameLabel->height() - ui->frameLabel->pixmap()->height()) / 2)) / (double) ui->frameLabel->pixmap()->height();
wScalingFactor=(double) processingThread->getCurrentROI().width() / (double) ui->frameLabel->pixmap()->width();
hScalingFactor=(double) processingThread->getCurrentROI().height() / (double) ui->frameLabel->pixmap()->height();
}
else
{
xScalingFactor=(double) mouseData.selectionBox.x() / (double) ui->frameLabel->width();
yScalingFactor=(double) mouseData.selectionBox.y() / (double) ui->frameLabel->height();
wScalingFactor=(double) processingThread->getCurrentROI().width() / (double) ui->frameLabel->width();
hScalingFactor=(double) processingThread->getCurrentROI().height() / (double) ui->frameLabel->height();
}
// Set selection box properties (new ROI)
selectionBox.setX(xScalingFactor*processingThread->getCurrentROI().width() + processingThread->getCurrentROI().x());
selectionBox.setY(yScalingFactor*processingThread->getCurrentROI().height() + processingThread->getCurrentROI().y());
selectionBox.setWidth(wScalingFactor*mouseData.selectionBox.width());
selectionBox.setHeight(hScalingFactor*mouseData.selectionBox.height());
// Check if selection box has NON-ZERO dimensions
if((selectionBox.width()!=0)&&((selectionBox.height())!=0))
{
// Selection box can also be drawn from bottom-right to top-left corner
if(selectionBox.width()<0)
{
x_temp=selectionBox.x();
width_temp=selectionBox.width();
selectionBox.setX(x_temp+selectionBox.width());
selectionBox.setWidth(width_temp*-1);
}
if(selectionBox.height()<0)
{
y_temp=selectionBox.y();
height_temp=selectionBox.height();
selectionBox.setY(y_temp+selectionBox.height());
selectionBox.setHeight(height_temp*-1);
}
// Check if selection box is not outside window
if((selectionBox.x()<0)||(selectionBox.y()<0)||
((selectionBox.x()+selectionBox.width())>(processingThread->getCurrentROI().x()+processingThread->getCurrentROI().width()))||
((selectionBox.y()+selectionBox.height())>(processingThread->getCurrentROI().y()+processingThread->getCurrentROI().height()))||
(selectionBox.x()<processingThread->getCurrentROI().x())||
(selectionBox.y()<processingThread->getCurrentROI().y()))
{
// Display error message
QMessageBox::warning(this,"ERROR:","Selection box outside range. Please try again.");
}
// Set ROI
else
emit setROI(selectionBox);
}
}
}
void CameraView::handleContextMenuAction(QAction *action)
{
if(action->text()=="Reset ROI")
emit setROI(QRect(0, 0, captureThread->getInputSourceWidth(), captureThread->getInputSourceHeight()));
else if(action->text()=="Scale to Fit Frame")
ui->frameLabel->setScaledContents(action->isChecked());
else if(action->text()=="Grayscale")
{
imageProcessingFlags.grayscaleOn=action->isChecked();
emit newImageProcessingFlags(imageProcessingFlags);
}
else if(action->text()=="Smooth")
{
imageProcessingFlags.smoothOn=action->isChecked();
emit newImageProcessingFlags(imageProcessingFlags);
}
else if(action->text()=="Dilate")
{
imageProcessingFlags.dilateOn=action->isChecked();
emit newImageProcessingFlags(imageProcessingFlags);
}
else if(action->text()=="Erode")
{
imageProcessingFlags.erodeOn=action->isChecked();
emit newImageProcessingFlags(imageProcessingFlags);
}
else if(action->text()=="Flip")
{
imageProcessingFlags.flipOn=action->isChecked();
emit newImageProcessingFlags(imageProcessingFlags);
}
else if(action->text()=="Canny")
{
imageProcessingFlags.cannyOn=action->isChecked();
emit newImageProcessingFlags(imageProcessingFlags);
}
else if(action->text()=="Settings...")
setImageProcessingSettings();
}
void CameraView::getSetPersonInfo()
{
QString receipt = ui->lineEditReceipt->text().toUpper();
QString table_name, name, gender, phone, code;
if (receipt.isEmpty()) return;
QChar chara = receipt.at(0);
if (chara == QChar('A')) table_name = "zen_male";
else table_name = "zen_female";
QSqlQuery query;
query.exec(QString("select name, gender, phone_num, code from %1 where receipt = '%2'").arg(table_name
).arg(receipt));
while(query.next()) {
name = query.value(0).toString();
gender = query.value(1).toString();
phone = query.value(2).toString();
code = query.value(3).toString();
qDebug() << name << gender << phone << code;
}
query.clear();
ui->labelCode->setText(code);
ui->labelName->setText(name);
ui->labelGender->setText(gender);
ui->labelPhone->setText(phone);
}
void CameraView::updateMark()
{
QString code = ui->labelCode->text().toUpper();
QString table_name;
if (code.isEmpty()) return;
QChar chara = code.at(0);
if (chara == QChar('A')) table_name = "zen_male";
else table_name = "zen_female";
QString sql = QString("update %1 set mark = 1 where code = '%2'").arg(table_name).arg(code);
qDebug() << sql;
QSqlQuery query;
query.exec(sql);
}
void CameraView::on_pushButtonCapture_clicked()
{
QString message;
QString receipt = ui->lineEditReceipt->text().trimmed();
if (ui->labelName->text().isEmpty()) {
message = "成功,但数据库中不存在此人。";
emit setReceipt("_unknow_" + receipt);
QSound::play(":/voice/failure.wav");
updateMark();
} else {
message = "成功";
emit setReceipt(receipt);
QSound::play(":/voice/play.wav");
}
ui->labelInfo->setText(message);
ui->labelCode->clear();
ui->labelName->clear();
ui->labelPhone->clear();
ui->labelGender->clear();
}
void CameraView::on_lineEditReceipt_returnPressed()
{
if (ui->lineEditReceipt->text().isEmpty()) {
return;
}
getSetPersonInfo();
}
bool CameraView::databaseTest()
{
bool ret;
QTcpSocket tsock;
tsock.connectToHost(SERVER_IP, 3306);
ret = tsock.waitForConnected(1000);
if (ret) tsock.close();
return ret;
}
bool CameraView::connectDatabase()
{
db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName(SERVER_IP);
db.setDatabaseName(DB_NAME);
db.setUserName(DB_USER);
db.setPassword(DB_PASS);
if(!db.open()) {
QMessageBox::critical(this, "数据库错误", db.lastError().text());
return false;
}
return true;
}
void CameraView::getLocalSavePath(QString localSavePath)
{
qDebug() << localSavePath;
emit setLocalSavePath(localSavePath);
}
void CameraView::on_pushButtonQuery_clicked()
{
getSetPersonInfo();
if (ui->labelCode->text().isEmpty()) {
ui->labelInfo->setText("查无此人");
} else {
ui->labelInfo->clear();
}
}