-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathcontrolClassify.cpp
330 lines (232 loc) · 9.22 KB
/
controlClassify.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
/*
* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
*
* 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 "controlClassify.h"
#include "imageNet.h"
#define STATUS_MSG "Status - "
#define SELECT_LABEL_FILE_MSG STATUS_MSG "select output dataset path and label file"
#define DEFAULT_JPEG_QUALITY 95
// constructor
ControlClassifyWidget::ControlClassifyWidget( commandLine* cmdLine, CaptureWindow* capture )
{
captureWindow = capture;
/*
* create layout
*/
QVBoxLayout* layout = new QVBoxLayout();
layout->setAlignment(Qt::AlignTop);
// dataset location
QHBoxLayout* datasetLayout = new QHBoxLayout();
QPushButton* datasetButton = new QPushButton(tr("..."));
datasetButton->setMaximumWidth(50);
connect(datasetButton, SIGNAL(clicked()), this, SLOT(selectDatasetPath()));
datasetWidget = new QLabel();
datasetWidget->setFrameStyle(QFrame::Panel|QFrame::Sunken);
datasetWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
datasetLayout->addWidget(new QLabel(tr("Dataset Path ")));
datasetLayout->addWidget(datasetWidget);
datasetLayout->addWidget(datasetButton);
layout->addItem(datasetLayout);
// class label file
QHBoxLayout* labelLayout = new QHBoxLayout();
QPushButton* labelButton = new QPushButton(tr("..."));
labelButton->setMaximumWidth(50);
connect(labelButton, SIGNAL(clicked()), this, SLOT(selectLabelFile()));
labelWidget = new QLabel();
labelWidget->setFrameStyle(QFrame::Panel|QFrame::Sunken);
labelWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
labelLayout->addWidget(new QLabel(tr("Class Labels ")));
labelLayout->addWidget(labelWidget);
labelLayout->addWidget(labelButton);
layout->addItem(labelLayout);
// subset drop-down
QHBoxLayout* subsetLayout = new QHBoxLayout();
setDropdown = new QComboBox();
setDropdown->addItem(tr("train"));
setDropdown->addItem(tr("val"));
setDropdown->addItem(tr("test"));
setDropdown->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
subsetLayout->addWidget(new QLabel(tr("Current Set ")));
subsetLayout->addWidget(setDropdown);
layout->addItem(subsetLayout);
// class label drop-down
QHBoxLayout* classLayout = new QHBoxLayout();
labelDropdown = new QComboBox();
labelDropdown->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
classLayout->addWidget(new QLabel(tr("Current Class ")));
classLayout->addWidget(labelDropdown);
layout->addItem(classLayout);
// jpeg quality
QHBoxLayout* qualityLayout = new QHBoxLayout();
qualitySlider = new QSlider(Qt::Horizontal);
qualitySlider->setRange(1,100);
qualitySlider->setValue(DEFAULT_JPEG_QUALITY);
connect(qualitySlider, SIGNAL(valueChanged(int)), this, SLOT(onQualityChanged(int)));
qualityLabel = new QLabel(QString::number(DEFAULT_JPEG_QUALITY));
qualityLayout->addWidget(new QLabel(tr("JPEG Quality ")));
qualityLayout->addWidget(qualitySlider);
qualityLayout->addWidget(qualityLabel);
layout->addLayout(qualityLayout);
// capture button
captureButton = new QPushButton("Capture (space)");
captureButton->setEnabled(false);
captureButton->setShortcut(QKeySequence(Qt::Key_Space));
connect(captureButton, SIGNAL(clicked()), this, SLOT(onCapture()));
layout->addWidget(captureButton);
// status bar
statusBar = new QStatusBar();
statusBar->setStyleSheet("QStatusBar{border-top: 1px outset grey; color: rgb(150,150,150);}");
statusBar->showMessage(tr(SELECT_LABEL_FILE_MSG));
layout->addWidget(statusBar);
/*
* configure options
*/
setLayout(layout);
}
// destructor
ControlClassifyWidget::~ControlClassifyWidget()
{
}
// createDatasetDirectories
void ControlClassifyWidget::createDatasetDirectories()
{
// check that we have a valid path to the dataset
if( datasetPath.size() == 0 )
return;
// check that we have loaded class labels
const int numClasses = labelDropdown->count();
if( numClasses == 0 )
return;
// check that each subdirectory exists
QDir dir(QString::fromStdString(datasetPath));
// create directories for each training set and class
const int numSets = setDropdown->count();
for( int s=0; s < numSets; s++ )
{
const QString setName = setDropdown->itemText(s);
if( !dir.exists(setName) )
{
if( !dir.mkdir(setName) )
{
const QString msg = QString("Failed to create dataset subdirectory '%1/'").arg(setName);
QMessageBox::critical(this, tr("Error Creating Dataset Directories"), msg);
statusBar->showMessage(QString(STATUS_MSG) + msg);
continue;
}
}
QDir setDir = dir;
if( !setDir.cd(setName) )
{
const QString msg = QString("Failed to cd to dataset subdirectory '%1/'").arg(setName);
QMessageBox::critical(this, tr("Error Creating Dataset Directories"), msg);
statusBar->showMessage(QString(STATUS_MSG) + msg);
continue;
}
for( int n=0; n < numClasses; n++ )
{
const QString subdir = labelDropdown->itemText(n);
if( !setDir.exists(subdir) )
{
if( !setDir.mkdir(subdir) )
{
const QString msg = QString("Failed to create dataset subdirectory '%1/%2/'").arg(setName, subdir);
QMessageBox::critical(this, tr("Error Creating Dataset Directories"), msg);
statusBar->showMessage(QString(STATUS_MSG) + msg);
continue;
}
}
}
}
}
// selectDatasetPath
void ControlClassifyWidget::selectDatasetPath()
{
// prompt user to select the file
QString qPath = QFileDialog::getExistingDirectory(this, tr("Select Dataset Directory"));
if( qPath.size() == 0 )
return;
datasetPath = qPath.toUtf8().constData();
// make sure the directories exist
createDatasetDirectories();
// enable capture button
if( datasetPath.size() > 0 && labelPath.size() > 0 )
captureButton->setEnabled(true);
// update label with new path
QFontMetrics metrics(datasetWidget->font());
int width = datasetWidget->width() - 2;
QString clippedText = metrics.elidedText(qPath, Qt::ElideLeft, width);
datasetWidget->setText(clippedText);
}
// selectLabelFile
void ControlClassifyWidget::selectLabelFile()
{
// prompt user to select the file
QString qFilename = QFileDialog::getOpenFileName(this, tr("Select Label File"), QString(), tr("Text Files (*.txt)"));
if( qFilename.size() == 0 )
return;
labelPath = qFilename.toUtf8().constData();
// load the class descriptions
std::vector<std::string> classDesc;
if( !imageNet::LoadClassLabels(labelPath.c_str(), classDesc) )
{
QMessageBox::critical(this, tr("Failed to Load Class Labels"), tr("There was an error loading the label files from: ") + qFilename);
statusBar->showMessage(tr(SELECT_LABEL_FILE_MSG));
return;
}
// update drop-down with new labels
const size_t numClasses = classDesc.size();
labelDropdown->clear();
for( size_t n=0; n < numClasses; n++ )
labelDropdown->addItem(QString::fromStdString(classDesc[n]));
statusBar->showMessage(QString(STATUS_MSG "loaded %1 class labels").arg(numClasses));
// make sure the directories exist
createDatasetDirectories();
// enable capture button
if( datasetPath.size() > 0 && labelPath.size() > 0 )
captureButton->setEnabled(true);
// update label with new filename
QFontMetrics metrics(labelWidget->font());
int width = labelWidget->width() - 2;
QString clippedText = metrics.elidedText(qFilename, Qt::ElideLeft, width);
labelWidget->setText(clippedText);
}
// onCapture
void ControlClassifyWidget::onCapture()
{
const std::string subsetLabel = setDropdown->currentText().toUtf8().constData();
const std::string classLabel = labelDropdown->currentText().toUtf8().constData();
const std::string timestamp = QDateTime::currentDateTime().toString("yyyyMMdd-hhmmss").toUtf8().constData();
const std::string subdirPath = subsetLabel + "/" + classLabel;
const std::string directory = datasetPath + "/" + subdirPath;
const std::string filename = directory + "/" + timestamp + ".jpg";
if( !captureWindow->Save(filename.c_str()) )
{
statusBar->showMessage(QString(STATUS_MSG "failed to save ") + QString::fromStdString(timestamp) + QString(".jpg"));
return;
}
const int numFiles = QDir(directory.c_str()).count() - 2;
statusBar->showMessage(QString(STATUS_MSG "%1 images in %2").arg(QString::number(numFiles), QString::fromStdString(subdirPath)));
}
// onQualityChanged
void ControlClassifyWidget::onQualityChanged( int value )
{
qualityLabel->setText(QString::number(value));
}