-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.cpp
691 lines (616 loc) · 24 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
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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
#include "MainWindow.h"
#include "ui_MainWindow.h"
#include <QFileDialog>
#include <QColorDialog>
#include <QKeyEvent>
#include <QDebug>
#include <QShortcut>
#include <QtCore/QProcess>
#include <QtCore/QJsonArray>
#include <QtCore/QJsonObject>
#include <QtWidgets/QInputDialog>
#include <iomanip>
#include <ValidationClassBoxes.h>
namespace {
auto openDatasetDir(QWidget* parent, QString const& datasetPath = {}) -> QStringList
{
auto datasetDir = datasetPath.isEmpty() ? QDir{QFileDialog::getExistingDirectory(parent, parent->tr("Open Dataset Directory"), "./", QFileDialog::ShowDirsOnly)}
: datasetPath;
auto datasetImagesList = datasetDir.entryList(QStringList() << "*.jpg" << "*.JPG" << "*.png" << "*.PNG", QDir::Files);
if (datasetImagesList.empty())
{
QMessageBox::critical(parent, "Error", "Could not be found images for dataset!");
return {};
}
auto const imgDir = datasetDir.canonicalPath();
for (QString& str: datasetImagesList)
{
str = imgDir + "/" + str;
}
return datasetImagesList;
}
} /// end namespace anonymous
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
_ui(new Ui::MainWindow)
{
_ui->setupUi(this);
connect(new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_S), this), &QShortcut::activated, this, &MainWindow::on_pushButton_save_clicked);
connect(new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_C), this), &QShortcut::activated, this, &MainWindow::clearAllClassBoxes);
connect(new QShortcut(QKeySequence(Qt::Key_S), this), &QShortcut::activated, this, &MainWindow::nextClass);
connect(new QShortcut(QKeySequence(Qt::Key_W), this), &QShortcut::activated, this, &MainWindow::prevClass);
connect(new QShortcut(QKeySequence(Qt::Key_A), this), &QShortcut::activated, this, &MainWindow::on_pushButton_prev_clicked);
connect(new QShortcut(QKeySequence(Qt::Key_D), this), &QShortcut::activated, this, &MainWindow::on_pushButton_next_clicked);
connect(new QShortcut(QKeySequence(Qt::Key_Space), this), &QShortcut::activated, this, &MainWindow::on_pushButton_next_clicked);
connect(_ui->label_image, &BoundingBoxSelector::datasetIteratorUpdated, this, &MainWindow::datasetListUpdated);
init();
initTableWidget();
}
MainWindow::~MainWindow()
{
delete _ui;
}
void MainWindow::on_pushButton_create_from_video_clicked()
{
if (!openVideos())
{
qDebug() << "Could not be opened video files";
}
}
void MainWindow::on_pushButtonOpenProject_clicked()
{
QString projectDirectory = QFileDialog::getExistingDirectory(this, tr("Open project directory"), "./");
if (!projectDirectory.isEmpty())
{
QString projectFilePath = projectDirectory + "/project.json";
if (!QFileInfo::exists(projectFilePath))
{
QFile file(projectFilePath);
file.open(QIODevice::WriteOnly);
}
_datasetProject.loadFromFile(projectFilePath);
QString classesNamesFilePath = projectDirectory + "/_.names";
loadClassNameList(QFileInfo::exists(classesNamesFilePath) ? classesNamesFilePath : "");
loadDatasetList(projectDirectory);
}
#if 0
QString projectFilePath = QFileDialog::getOpenFileName(this, tr("Open project file"), "./", tr("Yolo labeling tool project (*.json)"));
if (!projectFilePath.isEmpty())
{
_datasetProject.loadFromFile(projectFilePath);
loadClassNameList();
loadDatasetList();
}
#endif
init();
}
void MainWindow::on_pushButton_save_clicked()
{
exportClassListToFile();
for (auto it = _datasetList.begin(); it != _datasetList.end(); ++it)
{
_ui->label_image->exportClassBoxesToAnnotationFile(it, _classesList);
}
}
void MainWindow::on_pushButton_remove_clicked()
{
QFile::remove(_datasetIt.key());
QFile::remove(toTxtExtention(_datasetIt.key()));
_datasetIt = _datasetList.erase(_datasetIt);
if (!_datasetList.isEmpty() && (_datasetIt == _datasetList.end()))
{
--_datasetIt;
}
setCurrentImg();
updateButtonEnabling(!_datasetList.isEmpty());
}
void MainWindow::on_pushButton_prev_clicked()
{
setPreviousImage();
}
void MainWindow::on_pushButton_next_clicked()
{
setNextImage();
}
void MainWindow::on_tableWidget_label_cellDoubleClicked(int row, int column)
{
switch(column)
{
case 0: {
bool isAccepted{};
auto newClassName = QInputDialog::getText(this, "Rename class", "Class name:", QLineEdit::Normal, _ui->tableWidget_label->item(row, 0)->text(), &isAccepted, Qt::Dialog);
if (isAccepted && !newClassName.isEmpty())
{
// TODO: Should be moved to datasetProject class
auto const oldClassName = _ui->tableWidget_label->item(row, 0)->text();
_classesList.remove(oldClassName);
QList<QVariant> classData;
classData.push_back(row);
classData.push_back(static_cast<int>(_ui->tableWidget_label->item(row, 1)->backgroundColor().rgba()));
_classesList[newClassName] = classData;
_classesIt = _classesList.find(newClassName);
_datasetProject.set("class_names_list", _classesList);
for (auto it = _datasetList.begin(); it != _datasetList.end(); ++it)
{
auto classBoxes = it->toMap();
classBoxes[newClassName] = classBoxes.take(oldClassName);
it->setValue(classBoxes);
}
_datasetProject.set("dataset_list", _datasetList);
_ui->label_image->loadClassBoxes(_datasetIt);
_ui->tableWidget_label->item(row, 0)->setText(newClassName);
}
break;
}
case 1: {
auto color = QColorDialog::getColor(Qt::white, nullptr, "Set Label Color");
if(color.isValid())
{
// TODO: Should be moved to datasetProject class
auto const className = _ui->tableWidget_label->item(row, 0)->text();
QList<QVariant> classData;
classData.push_back(row);
classData.push_back(static_cast<int>(color.rgba()));
_classesList[className] = classData;
_classesIt = _classesList.find(className);
_datasetProject.set("class_names_list", _classesList);
_ui->tableWidget_label->item(row, 1)->setBackgroundColor(color);
}
break;
}
default:
break;
}
updateCurrentClass();
_ui->label_image->showImage();
}
void MainWindow::on_tableWidget_label_cellClicked(int row, int column)
{
Q_UNUSED(column)
_classesIt = _classesList.find(_ui->tableWidget_label->item(row, 0)->text());
//_classesIt = std::next(_classesList.begin(), row);
updateCurrentClass();
}
void MainWindow::on_horizontalSlider_images_sliderMoved(int position)
{
_datasetIt = std::next(_datasetList.begin(), position);
setCurrentImg();
}
void MainWindow::clearAllClassBoxes()
{
_ui->label_image->clearAllClassBoxex();
}
void MainWindow::nextClass()
{
if (_classesIt != _classesList.end())
{
++_classesIt;
}
updateCurrentClass();
}
void MainWindow::prevClass()
{
if (_classesIt != _classesList.begin())
{
--_classesIt;
}
updateCurrentClass();
}
// TODO: All logic for working with _datasetList and _classesList should be moved to DatasetProject class
void MainWindow::datasetListUpdated()
{
_datasetProject.set("dataset_list", _datasetList);
}
void MainWindow::wheelEvent(QWheelEvent* ev)
{
if (ev->delta() > 0)
{
setPreviousImage(); // up Wheel
}
else if(ev->delta() < 0)
{
setNextImage(); //down Wheel
}
}
void MainWindow::keyPressEvent(QKeyEvent* event)
{
if(event->key() == Qt::Key_QuoteLeft)
{
updateCurrentClass();
}
else if ((event->key() >= Qt::Key_0) && (event->key() <= Qt::Key_9))
{
int const asciiToNumber = event->key() - Qt::Key_0;
if (asciiToNumber < _classesList.size())
{
_classesIt = std::next(_classesList.begin(), asciiToNumber);
updateCurrentClass();
}
}
}
void MainWindow::init()
{
_ui->label_image->init();
updateButtonEnabling(!_datasetList.isEmpty());
updateDatasetNavigator();
_classesIt = _classesList.begin();
updateCurrentClass();
_datasetIt = _datasetList.begin();
setCurrentImg();
}
void MainWindow::initTableWidget()
{
_ui->tableWidget_label->horizontalHeader()->setVisible(true);
_ui->tableWidget_label->horizontalHeader()->setStretchLastSection(true);
disconnect(_ui->tableWidget_label->horizontalHeader(), SIGNAL(sectionPressed(int)),_ui->tableWidget_label, SLOT(selectColumn(int)));
initTableWidgetContextMenuSetup();
}
void MainWindow::initTableWidgetContextMenuSetup()
{
_ui->tableWidget_label->setContextMenuPolicy(Qt::CustomContextMenu);
QObject::connect(_ui->tableWidget_label, &QTableWidget::customContextMenuRequested, this, [this]() {
QMenu* menu = new QMenu;
auto addClass = menu->addAction("Add class");
auto point = _ui->tableWidget_label->mapFromGlobal(QCursor::pos() - QPoint{0, 25});
auto selectedItemPtr = _ui->tableWidget_label->itemAt(point);
if (selectedItemPtr)
{
auto removeClass = menu->addAction("Remove class");
QObject::connect(removeClass, &QAction::triggered, this, [this, menu, removeClass, selectedItemPtr]() {
_classesList.remove(selectedItemPtr->text());
_classesIt = _classesList.begin();
// TODO: May be should be set iterator _classesIt instead
_ui->label_image->setFocusObjectLabel(!_classesList.isEmpty() ? _classesIt.key() : "");
_datasetProject.set("class_names_list", _classesList);
_ui->tableWidget_label->removeRow(selectedItemPtr->row());
removeClass->deleteLater();
menu->deleteLater();
});
}
QObject::connect(addClass, &QAction::triggered, this, [this, menu, addClass]() {
bool isAccepted{};
while(true)
{
auto newClassName = QInputDialog::getText(this, "Create class", "Class name:", QLineEdit::Normal, "", &isAccepted, Qt::Dialog);
if (isAccepted && !newClassName.isEmpty())
{
if (_classesList.contains(newClassName))
{
// TODO: May be should be dialog box shown.
qDebug() << "The class with name " << newClassName << " has already exist, try again";
continue;
}
auto const lastRowIndex = _ui->tableWidget_label->rowCount();
auto classNameColor = QColor(QColor::colorNames().first());
qDebug() << newClassName << ", " << lastRowIndex << ", " << classNameColor;
_ui->tableWidget_label->insertRow(lastRowIndex);
_ui->tableWidget_label->setItem(lastRowIndex, 0, new QTableWidgetItem(newClassName));
_ui->tableWidget_label->item(lastRowIndex, 0)->setFlags(_ui->tableWidget_label->item(lastRowIndex, 0)->flags() ^ Qt::ItemIsEditable);
_ui->tableWidget_label->setItem(lastRowIndex, 1, new QTableWidgetItem(""));
_ui->tableWidget_label->item(lastRowIndex, 1)->setBackgroundColor(classNameColor);
_ui->tableWidget_label->item(lastRowIndex, 1)->setFlags(_ui->tableWidget_label->item(lastRowIndex, 1)->flags() ^ Qt::ItemIsEditable ^ Qt::ItemIsSelectable);
// TODO: May be should be set iterator _classesIt instead
_ui->label_image->setFocusObjectLabel(newClassName);
QList<QVariant> classData;
classData.push_back(lastRowIndex);
classData.push_back(static_cast<int>(classNameColor.rgba()));
_classesList[newClassName] = classData;
_classesIt = _classesList.find(newClassName);
_datasetProject.set("class_names_list", _classesList);
//ui->label_image->setClasses(&_classesList);
}
break;
}
addClass->deleteLater();
menu->deleteLater();
});
menu->exec(QCursor::pos());
menu->clear();
});
}
void MainWindow::setCurrentImg()
{
if (_datasetList.isEmpty())
{
return;
}
if (!_ui->label_image->openImage(_datasetIt.key()))
{
// TODO: Should be shown message box
qDebug() << "Could not open file";
return;
}
// TODO: May be more convinient to pass iterator to current dataset
_ui->label_image->loadClassBoxes(_datasetIt);
if (_ui->isDarknetBoxesVisible->isChecked())
{
detect();
}
_ui->label_image->showImage();
_ui->label_progress->setText(QString::number(std::distance(_datasetList.begin(), _datasetIt)) + " / " + QString::number(_datasetList.size() - 1));
_ui->label_file->setText("File: " + _datasetIt.key());
updateDatasetNavigator();
}
void MainWindow::setNextImage()
{
if ((_datasetIt != _datasetList.end()) &&
(std::next(_datasetIt) != _datasetList.end()))
{
++_datasetIt;
}
setCurrentImg();
}
void MainWindow::setPreviousImage()
{
if (_datasetIt != _datasetList.begin())
{
--_datasetIt;
}
setCurrentImg();
}
void MainWindow::updateCurrentClass()
{
if (!_classesList.isEmpty())
{
_ui->label_image->setFocusObjectLabel(_classesIt.key());
_ui->tableWidget_label->setCurrentCell(_classesIt.value().toList()[0].toInt(), 0);
}
}
void MainWindow::updateClassesTable()
{
qDebug() << _classesList.size();
_ui->tableWidget_label->setRowCount(_classesList.size());
for (auto it = _classesList.begin(); it != _classesList.end(); ++it)
{
auto const className = it.key();
auto const classData = it->toList();
// TODO: Should be fixed classData if wrong
auto const classNameColor = (classData.size() == 2)
? QColor::fromRgba(static_cast<uint32_t>(classData[1].toInt()))
: QColor(255, 255, 255, 255);
auto const rowIndex = classData[0].toInt();
qDebug() << className << ", " << rowIndex << ", " << classNameColor;
_ui->tableWidget_label->setItem(rowIndex, 0, new QTableWidgetItem(className));
_ui->tableWidget_label->item(rowIndex, 0)->setFlags(_ui->tableWidget_label->item(rowIndex, 0)->flags() ^ Qt::ItemIsEditable);
_ui->tableWidget_label->setItem(rowIndex, 1, new QTableWidgetItem(""));
_ui->tableWidget_label->item(rowIndex, 1)->setBackgroundColor(classNameColor);
_ui->tableWidget_label->item(rowIndex, 1)->setFlags(_ui->tableWidget_label->item(rowIndex, 1)->flags() ^ Qt::ItemIsEditable ^ Qt::ItemIsSelectable);
}
}
bool MainWindow::openVideos()
{
auto selectedFileList = QFileDialog::getOpenFileNames(this, "Select one or more files to open", "./", "Video (*.mp4 *.avi *.webm)");
if (!selectedFileList.isEmpty())
{
QProgressDialog progressDialog("Slicing video process", "&Cancel", 0, 100);
progressDialog.setWindowModality(Qt::WindowModal);
progressDialog.setMinimumSize(400, 40);
progressDialog.setRange(0, 100);
progressDialog.setValue(0);
QProcess slicingDatasetProcess{};
for (auto const& item : selectedFileList)
{
qDebug() << item;
auto workingDirectoryPath = QDir::currentPath() + "/" + QFileInfo(item).fileName().split(".")[0];
if (QDir().exists(workingDirectoryPath) || QDir().mkdir(workingDirectoryPath))
{
qDebug() << workingDirectoryPath;
}
else
{
qDebug() << "Could not create directory: " << workingDirectoryPath;
}
slicingDatasetProcess.setWorkingDirectory(workingDirectoryPath);
QObject::connect(&slicingDatasetProcess, &QProcess::readyReadStandardError, [&]() {
static auto durationSeconds = 0;
auto toSeconds = [](std::string const& duration) {
return (((duration[0] - '0')*10 + duration[1] - '0')*60*60) + (((duration[3] - '0')*10 + duration[4] - '0')*60) + ((duration[6] - '0')*10 + duration[7] - '0');
};
std::string string = slicingDatasetProcess.readAllStandardError().toStdString();
auto durationStart = string.find("Duration: ");
if (durationStart != std::string::npos)
{
auto durationString = string.substr(durationStart + 10, 8);
durationSeconds = toSeconds(durationString);
qDebug() << "Duration: " << QString::fromStdString(durationString) << ", " << durationSeconds;
}
auto start = string.rfind("time=");
if (start != std::string::npos)
{
start += 5;
auto end = string.rfind("bitrate=");
if (end != std::string::npos)
{
end -= 4;
auto currentPosString = string.substr(start, end - start);
auto currentPos = toSeconds(currentPosString);
qDebug() << QString::fromStdString(currentPosString) << ", " << currentPos;
progressDialog.setValue((currentPos * 100)/durationSeconds);
}
}
});
QObject::connect(&slicingDatasetProcess, static_cast<void(QProcess::*)(int)>(&QProcess::finished), [&](){
progressDialog.close();
});
QStringList ffmpegArguments("-i");
ffmpegArguments << item << "-vf" << "fps=1" << "%06d.png";//jpg";
slicingDatasetProcess.start("/usr/bin/ffmpeg", ffmpegArguments);
progressDialog.setLabelText(QString("Slicing video %1 to dataset").arg(item));
auto isCanceled = false;
QObject::connect(&progressDialog, &QProgressDialog::canceled, [&]() {
isCanceled = true;
slicingDatasetProcess.terminate();
});
progressDialog.exec();
qDebug() << "Result of process: " << slicingDatasetProcess.waitForFinished();
if (isCanceled)
{
qDebug() << "Cancelled slicing process";
break;
}
}
return true;
}
return false;
}
void MainWindow::updateButtonEnabling(bool isEnabled)
{
_ui->pushButton_next->setEnabled(isEnabled);
_ui->pushButton_prev->setEnabled(isEnabled);
_ui->pushButton_save->setEnabled(isEnabled);
_ui->pushButton_remove->setEnabled(isEnabled);
_ui->tableWidget_label->setEnabled(isEnabled);
_ui->label_image->setEnabled(isEnabled);
_ui->pushButtonValidate->setEnabled(isEnabled);
_ui->pushButtonGenerateDataset->setEnabled(isEnabled);
_ui->loadDarknetModel->setEnabled(isEnabled);
}
void MainWindow::updateDatasetNavigator()
{
_ui->horizontalSlider_images->setEnabled(true);
_ui->horizontalSlider_images->setRange(0, _datasetList.size() - 1);
_ui->horizontalSlider_images->blockSignals(true);
_ui->horizontalSlider_images->setValue(std::distance(_datasetList.begin(), _datasetIt));
_ui->horizontalSlider_images->blockSignals(false);
}
void MainWindow::loadClassNameList(QString const& classesFilePath)
{
_ui->tableWidget_label->clear();
_classesList = _datasetProject.get("class_names_list", [&]() -> QVariantMap {
QVariantMap data;
//auto classesFilePath = QFileDialog::getOpenFileName(this, tr("Open class list file"), "./", tr("Darknet class list file (*.txt *.names)"));
if (!classesFilePath.isEmpty())
{
QFile classesFile{classesFilePath};
if (classesFile.open(QIODevice::ReadOnly))
{
auto classIndex = 0;
QTextStream in{&classesFile};
while (!in.atEnd())
{
QList<QVariant> classData;
classData.push_back(classIndex++);
classData.push_back(-1); // White color with alpha 255
data[in.readLine()] = classData;
qDebug() << classData;
}
}
}
return data;
});
// Append additional class which will be excluded from annotation
QList<QVariant> classData;
classData.push_back(_classesList.size());
classData.push_back(-1);
_classesList["excluded_from_annotation"] = classData;
_classesIt = _classesList.begin();
_ui->label_image->setClasses(&_classesList);
if (_classesList.isEmpty())
{
return;
}
_ui->label_image->setFocusObjectLabel(_classesIt.key());
updateClassesTable();
}
void MainWindow::exportClassListToFile()
{
auto const classNameFilePath = QFileDialog::getSaveFileName(this, tr("Save class list file"), QFileInfo(_datasetIt.key()).absoluteDir().absolutePath(), tr("Darknet class list file (*.txt *.names)"));
if (classNameFilePath.isEmpty())
{
QMessageBox::warning(this, "Warning", "Since you reject selecting file for saving class names, it will not be saved!");
return;
}
qDebug() << _classesList;
auto classesList = _classesList.keys();
std::sort(classesList.begin(), classesList.end(), [&](auto const& left, auto const& right) {
return _classesList.find(left)->toList()[0] < _classesList.find(right)->toList()[0];
});
qDebug() << classesList;
QFile classesFile{classNameFilePath};
if (classesFile.open(QIODevice::WriteOnly | QIODevice::Truncate))
{
QTextStream out(&classesFile);
for (auto const& classItem : classesList)
{
if (classItem != "excluded_from_annotation")
{
out << classItem << '\n';
}
}
}
}
void MainWindow::detect()
{
if (_net == nullptr)
{
return;
}
auto boundingBoxesFromDarknet = BoundingBoxSelector::Label::Vector{};
auto detections = _net->detect(_datasetIt.key().toStdString());
for (const auto& detection : detections)
{
boundingBoxesFromDarknet.emplace_back(BoundingBoxSelector::Label{"excluded_from_annotation",
QRectF{static_cast<float>(detection.x/1920.0f),
static_cast<float>(detection.y/1280.0f),
static_cast<float>(detection.w/1920.0f),
static_cast<float>(detection.h/1280.0f)},
false});
// if (detection.prob > _confThreshold)
// {
// auto labelstr = _classes[detection.obj_id] + ": " + std::to_string(static_cast<uint32_t>(detection.prob * 100)) + "%";
// }
}
_ui->label_image->addClassBoxesFromDarknet(boundingBoxesFromDarknet);
}
void MainWindow::loadDatasetList(QString const& datasetPath)
{
_datasetList = _datasetProject.get("dataset_list", [&]() -> QVariantMap {
QVariantMap data;
for (auto const& item : openDatasetDir(this, datasetPath))
{
data[item] = _ui->label_image->importClassBoxesFromAnnotationFile(item, _classesList);
}
return data;
});
updateClassesTable();
_datasetIt = _datasetList.begin();
_ui->label_image->loadClassBoxes(_datasetIt);
}
void MainWindow::on_pushButtonValidate_clicked()
{
ValidationClassBoxes validationClassBoxes{this, &_datasetList, &_classesList};
connect(&validationClassBoxes, &ValidationClassBoxes::datasetListUpdated, this, &MainWindow::datasetListUpdated);
validationClassBoxes.exec();
}
void MainWindow::on_pushButtonGenerateDataset_clicked()
{
}
void MainWindow::on_loadDarknetModel_clicked()
{
auto modelFile = QFileDialog::getOpenFileName(this, tr("Select darknet model"), "./", tr("Darknet model file (*.cfg)"));
if (modelFile.isEmpty())
{
return;
}
auto weightsFile = QFileDialog::getOpenFileName(this, tr("Select darknet weights"), "./", tr("Darknet weights file (*.weights)"));
if (weightsFile.isEmpty())
{
return;
}
_net = std::make_unique<Detector>(modelFile.toStdString(), weightsFile.toStdString());
_ui->isDarknetBoxesVisible->setEnabled(true);
_ui->darknetAutoLabeling->setEnabled(true);
}
void MainWindow::on_darknetAutoLabeling_toggled(bool checked)
{
}
void MainWindow::on_isDarknetBoxesVisible_toggled(bool checked)
{
if (checked)
{
detect();
}
_ui->label_image->boxesFromDarknetVisible(checked);
_ui->label_image->setSelectionBoundingBoxFromDarknet(checked);
_ui->label_image->showImage();
}