-
Notifications
You must be signed in to change notification settings - Fork 0
/
tabwidget.cpp
347 lines (323 loc) · 12.6 KB
/
tabwidget.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
#include "tabwidget.h"
#include "ui_tabwidget.h"
//QT
#include <QFileDialog>
#include <QFile>
#include <QFileInfo>
#include <QDebug>
#include <QDateTime>
//CV
#include <opencv.hpp>
#include <opencv_modules.hpp>
//STD
#include <iostream>
#include <sstream>
#include <vector>
#include <algorithm>
#include <string>
#include <chrono>
#include <sys/stat.h>
#include <filesystem>
TabWidget::TabWidget(QWidget *parent) :
QTabWidget(parent),
ui(new Ui::TabWidget)
{
ui->setupUi(this);
setSysIdx();
m_pTimer = new QTimer(this);
m_pTimer->setInterval(17);
compression_params.push_back(cv::IMWRITE_PNG_COMPRESSION);
compression_params.push_back(9);
ui->snpStopBtn->setEnabled(false);
ui->snpCB1->setCurrentIndex(1);
ui->snpCB2->setCurrentIndex(2);
ui->snpSnapBtn->setEnabled(false);
ui->snpLbl1->setScaledContents(true);
ui->snpLbl2->setScaledContents(true);
ui->fdLbl->setScaledContents(true);
connect(m_pTimer, &QTimer::timeout, this, &TabWidget::snapPlay);
//Snap
connect(ui->snpPlaybtn, &QPushButton::clicked, this, [=](){
timerStartRefresh();
// if(m_pVideo1->get(cv::CAP_PROP_FRAME_HEIGHT) != 1080){
// m_pVideo1->set(cv::CAP_PROP_FRAME_WIDTH, 1920);
// m_pVideo1->set(cv::CAP_PROP_FRAME_HEIGHT, 1080);
// m_pVideo2->set(cv::CAP_PROP_FRAME_WIDTH, 1920);
// m_pVideo2->set(cv::CAP_PROP_FRAME_HEIGHT, 1080);
// }
});
connect(ui->snpStopBtn, &QPushButton::clicked, this, &TabWidget::timerStopRefresh);
connect(ui->snpSnapBtn, &QPushButton::clicked, this, &TabWidget::snapSnap);
connect(ui->snpCBT, &QComboBox::currentIndexChanged, this, &TabWidget::roicountUpdater);
//FD
connect(ui->fdfBtn, &QPushButton::clicked, this, &TabWidget::loadFile);
connect(ui->fdnBtn, &QPushButton::clicked, this, &TabWidget::loadNet);
connect(ui->fddBtn, &QPushButton::clicked, this, &TabWidget::defectDetect);
//RTD
//Settings
connect(ui->stgImgBtn, &QPushButton::clicked, this, &TabWidget::setImgDir);
}
TabWidget::~TabWidget()
{
delete ui;
}
bool TabWidget::winOrLnx(){
#ifdef Q_OS_WIN
return false;
#endif
#ifdef Q_OS_LINUX
return true;
#endif
}
void TabWidget::setSysIdx(){
if(winOrLnx())
m_sysIdx = 2;
else
m_sysIdx = 1;
return;
}
cv::Mat TabWidget::litPic(cv::Mat src, int cols, int rows){
cv::cvtColor(src, src, cv::COLOR_BGR2GRAY);
cv::resize(src, src, cv::Size(cols, rows));
return src;
}
void TabWidget::snapPlay(){
cv::Mat frame1, frame2;
*m_pVideo1 >> frame1;
*m_pVideo2 >> frame2;
if (frame1.empty() || frame2.empty())
{
return;
}
cv::cvtColor(frame1, frame1, cv::COLOR_BGR2RGB);
QImage disImage1 = QImage((const unsigned char*)(frame1.data),
frame1.cols,frame1.rows,
frame1.cols*frame1.channels(),
QImage::Format_RGB888);
cv::cvtColor(frame2, frame2, cv::COLOR_BGR2RGB);
QImage disImage2 = QImage((const unsigned char*)(frame2.data),
frame2.cols,frame2.rows,
frame2.cols*frame2.channels(),
QImage::Format_RGB888);
ui->snpLbl1->setPixmap(QPixmap::fromImage(disImage1));
ui->snpLbl2->setPixmap(QPixmap::fromImage(disImage2));
}
void TabWidget::snapSnap(){
std::string* src_pic_name1 = new std::string;
std::string* src_pic_name2 = new std::string;
*src_pic_name1 = ui->stgImgLine->text().toStdString()
+ "/src/"
+ ui->snpCBT->currentText().toStdString()
+ "/left/"
+ std::to_string(ROI_Count)
+ ".png";
*src_pic_name2 = ui->stgImgLine->text().toStdString()
+ "/src/"
+ ui->snpCBT->currentText().toStdString()
+ "/right/"
+ std::to_string(ROI_Count)
+ ".png";
cv::Mat* frame1 = new cv::Mat;
cv::Mat* frame2 = new cv::Mat;
*m_pVideo1 >> *frame1;
*m_pVideo2 >> *frame2;
cv::imwrite(src_pic_name1->data(), *frame1, compression_params);
cv::imwrite(src_pic_name2->data(), *frame2, compression_params);
*frame1 = litPic(*frame1, 64, 64);
*frame2 = litPic(*frame2, 64, 64);
*src_pic_name1 = ui->stgImgLine->text().toStdString()
+ "/lit/"
+ ui->snpCBT->currentText().toStdString()
+ "/left/"
+ std::to_string(ROI_Count)
+ ".png";
*src_pic_name2 = ui->stgImgLine->text().toStdString()
+ "/lit/"
+ ui->snpCBT->currentText().toStdString()
+ "/right/"
+ std::to_string(ROI_Count)
+ ".png";
cv::imwrite(src_pic_name1->data(), *frame1, compression_params);
cv::imwrite(src_pic_name2->data(), *frame2, compression_params);
delete frame1;
delete frame2;
delete src_pic_name1;
delete src_pic_name2;
++ROI_Count;
}
void TabWidget::timerStartRefresh(){
m_pVideo1 = new cv::VideoCapture(m_sysIdx*(ui->snpCB1->currentIndex()));
m_pVideo2 = new cv::VideoCapture(m_sysIdx*(ui->snpCB2->currentIndex()));
m_pTimer->start();
ui->snpPlaybtn->setEnabled(false);
ui->snpStopBtn->setEnabled(true);
ui->snpSnapBtn->setEnabled(true);
ui->fdTab->setEnabled(false);
ui->rtdTab->setEnabled(false);
}
void TabWidget::timerStopRefresh(){
ui->snpPlaybtn->setEnabled(true);
ui->snpStopBtn->setEnabled(false);
ui->snpSnapBtn->setEnabled(false);
ui->fdTab->setEnabled(true);
ui->rtdTab->setEnabled(true);
m_pTimer->stop();
delete m_pVideo1;
delete m_pVideo2;
}
bool TabWidget::fileExist(const std::string& name){
struct stat buffer;
return (stat (name.c_str(), &buffer) == 0);
}
void TabWidget::roicountUpdater(){
if(std::filesystem::exists(ui->stgImgLine->text().toStdString()
+ "/src/"
+ ui->snpCBT->currentText().toStdString()
+ "/left")){
auto dirIter = std::filesystem::directory_iterator(ui->stgImgLine->text().toStdString()
+ "/src/"
+ ui->snpCBT->currentText().toStdString()
+ "/left");
ROI_Count = 0;
for (auto& entry : dirIter)
{
if (entry.is_regular_file())
{
++ROI_Count;
}
}
}
else {
this->setCurrentWidget(ui->stgTab);
}
}
void TabWidget::setImgDir(){
QString dir = QFileDialog::getExistingDirectory(this,
"Set Saved Images Directory",
"./");
ui->stgImgLine->setText(dir);
if(ui->stgImgLine->text().isEmpty())
ui->stgImgLine->setText("./pic");
std::filesystem::create_directory(ui->stgImgLine->text().toStdString()
+ "/src");
std::filesystem::create_directory(ui->stgImgLine->text().toStdString()
+ "/lit");
for(int i = 0;i < ui->snpCBT->maxCount();++i){
std::filesystem::create_directory(ui->stgImgLine->text().toStdString()
+ "/src/"
+ ui->snpCBT->itemText(i).toStdString());
std::filesystem::create_directory(ui->stgImgLine->text().toStdString()
+ "/src/"
+ ui->snpCBT->itemText(i).toStdString()
+ "/left");
std::filesystem::create_directory(ui->stgImgLine->text().toStdString()
+ "/src/"
+ ui->snpCBT->itemText(i).toStdString()
+ "/right");
std::filesystem::create_directory(ui->stgImgLine->text().toStdString()
+ "/lit/"
+ ui->snpCBT->itemText(i).toStdString());
std::filesystem::create_directory(ui->stgImgLine->text().toStdString()
+ "/lit/"
+ ui->snpCBT->itemText(i).toStdString()
+ "/left");
std::filesystem::create_directory(ui->stgImgLine->text().toStdString()
+ "/lit/"
+ ui->snpCBT->itemText(i).toStdString()
+ "/right");
}
}
void TabWidget::loadFile(){
QString path = QFileDialog::getOpenFileName(this,
"Open Images or Videos",
"./",
"Images(*.png *.jpg *.jpeg)");
ui->fdfLE->setText(path);
if(path.isEmpty()){
qDebug() << "File: Load Nothing.";
return;
}
m_pImg = new cv::Mat(cv::imread(path.toStdString(),cv::IMREAD_COLOR));
if(m_pImg->empty()){
qDebug() << "Image/Video File can't be initialized by cv::Mat.";
return;
}
qDebug() << "Img loaded.";
cv::cvtColor(*m_pImg, *m_pImg, cv::COLOR_BGR2RGB);
QImage disImage1 = QImage((const unsigned char*)(m_pImg->data),
m_pImg->cols,m_pImg->rows,
m_pImg->cols*m_pImg->channels(),
QImage::Format_RGB888);
ui->fdLbl->setPixmap(QPixmap::fromImage(disImage1));
}
void TabWidget::loadNet(){
QString path = QFileDialog::getOpenFileName(this,
"Open Net",
"./",
"TFNN(*.pb)");
ui->fdnLE->setText(path);
if(path.isEmpty()){
qDebug() << "Net: Load Nothing.";
return;
}
m_pNet = new cv::dnn::Net(cv::dnn::readNetFromTensorflow(path.toStdString()));
if(m_pNet->empty()){
qDebug() << "Net: Read Failed.";
return;
}
qDebug() << "Net loaded.";
std::vector<cv::String> netLayerName = m_pNet->getLayerNames();
qDebug() << "Layer names: ";
for(unsigned long i = 0;i < netLayerName.size();++i){
qDebug() << netLayerName[i].data();
}
return;
}
void TabWidget::defectDetect(){
if(!m_pImg->empty()){
std::chrono::steady_clock::time_point timeStart = std::chrono::steady_clock::now();
cv::Mat preInput;
cv::resize(*m_pImg, preInput, cv::Size(64, 64));
cv::cvtColor(preInput, preInput, cv::COLOR_BGR2GRAY);
cv::equalizeHist(preInput, preInput);
preInput.convertTo(preInput, CV_32FC1, 1.0/255.0);
m_pNet->setInput(cv::dnn::blobFromImage(preInput));
cv::Mat result = m_pNet->forward();
std::chrono::steady_clock::time_point timeEnd = std::chrono::steady_clock::now();
std::chrono::duration<double, std::milli> elapsed = timeEnd - timeStart;
if(result.at<float>(0, 0) >= result.at<float>(0, 1) && result.at<float>(0, 0)>= 0.65){
ui->fdRst->setText(QString(
(std::string("Defect: ") +
std::to_string(result.at<float>(0, 0)) +
std::string(" Consumed Time: ") +
std::to_string(elapsed.count()) +
std::string(" ms"))
.data()
));
}
else if(result.at<float>(0, 0) < result.at<float>(0, 1) && result.at<float>(0, 1)>= 0.65){
ui->fdRst->setText(QString(
(std::string("Perfect: ") +
std::to_string(result.at<float>(0, 1)) +
std::string(" Consumed Time: ") +
std::to_string(elapsed.count()) +
std::string(" ms"))
.data()
));
}
else{
ui->fdRst->setText(QString(
(std::string("Not Sure...") +
std::string(" Consumed Time: ") +
std::to_string(elapsed.count()) +
std::string(" ms"))
.data()
));
}
return;
}
else{
qDebug() << "Img Empty.";
}
return;
}