-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValidationClassBoxes.cpp
127 lines (116 loc) · 5.09 KB
/
ValidationClassBoxes.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
#include "ValidationClassBoxes.h"
#include "ui_ValidationClassBoxes.h"
#include "Utils.h"
#include <QMenu>
#include <QDebug>
#include <QProgressDialog>
ValidationClassBoxes::ValidationClassBoxes(QWidget *parent, QVariantMap* datasetList, QVariantMap* classList)
: QDialog(parent)
, _ui(new Ui::ValidationClassBoxes)
, _datasetList{datasetList}
{
_ui->setupUi(this);
_ui->tabWidget->clear();
QProgressDialog progressDialog("Generation crops process...", "&Cancel", 0, 100);
progressDialog.setWindowModality(Qt::WindowModal);
progressDialog.setMinimumSize(400, 40);
progressDialog.setRange(0, 100);
progressDialog.setValue(0);
for (auto const& item : classList->keys())
{
_classBoxesList[item];
}
for (auto datasetIt = datasetList->begin(); datasetIt != datasetList->end(); ++datasetIt)
{
progressDialog.setValue((std::distance(datasetList->begin(), datasetIt) * 100)/datasetList->size());
QImage image{datasetIt.key()};
extractClassBoxes(datasetIt, [&](QString const& className, QRectF&& boxRect) {
_classBoxesList[className].push_back(
ClassBoxes{datasetIt,
datasetIt.key(),
boxRect,
QPixmap::fromImage(image.copy(toAbsolute(boxRect, image.size()))
.scaled(100, 100, Qt::AspectRatioMode::KeepAspectRatio))
});
// TODO: Should be moved to dedicated function
// auto croppedImageFile = datasetIt.key().toStdString();
// croppedImageFile = croppedImageFile.substr(croppedImageFile.find_last_of('/') + 1,
// croppedImageFile.find_last_of('.') - croppedImageFile.find_last_of('/') - 1);
// cropped.save(QString::fromStdString(croppedImageFile) + "_cropped_" + QString::number(i) + ".png");
});
}
for (auto it = _classBoxesList.begin(); it != _classBoxesList.end(); ++it)
{
QListView* currentClassList = new QListView(this);
currentClassList->setViewMode(QListView::IconMode);
currentClassList->setIconSize(QSize(100,100));
currentClassList->setResizeMode(QListView::Adjust);
currentClassList->setSelectionMode(QAbstractItemView::ExtendedSelection);
ValidationClassBoxesListModel* classBoxesListModel = new ValidationClassBoxesListModel{this, &it.value()};
currentClassList->setModel(classBoxesListModel);
_ui->tabWidget->addTab(currentClassList, it.key());
_listWidgets[it.key()] = currentClassList;
currentClassList->setContextMenuPolicy(Qt::CustomContextMenu);
connect(currentClassList, &QListView::customContextMenuRequested, this, [&, currentClassList, currentClassKey = it.key()](const QPoint &) {
QMenu menu;
for (auto const& className : _classBoxesList.keys())
{
if (className != currentClassKey)
{
menu.addAction(className);
}
}
menu.addAction("remove");
auto action = menu.exec(QCursor::pos());
if (action)
{
auto currentListViewModel = currentClassList->selectionModel();
if (action->text() != "remove")
{
auto selectedListViewModel = _listWidgets[action->text()]->model();
for (auto selectedIndex : currentListViewModel->selectedIndexes())
{
selectedListViewModel->insertRow(selectedListViewModel->rowCount(), selectedIndex);
}
}
while (true)
{
auto selectedIndex = currentListViewModel->selectedIndexes();
if (selectedIndex.isEmpty())
{
break;
}
currentClassList->model()->removeRow(selectedIndex.front().row());
}
}
});
}
progressDialog.close();
_ui->tabWidget->show();
}
void ValidationClassBoxes::sync()
{
for (auto datasetIt = _datasetList->begin(); datasetIt != _datasetList->end(); ++datasetIt)
{
QMap<QString, QVariant> classBoxes;
for (auto classBoxIt = _classBoxesList.begin(); classBoxIt != _classBoxesList.end(); ++classBoxIt)
{
for (auto boxIt = classBoxIt->begin(); boxIt != classBoxIt->end(); ++boxIt)
{
if (boxIt->it == datasetIt)
{
auto boxesList = classBoxes[classBoxIt.key()].toList();
boxesList.push_back(QList<QVariant>{boxIt->boxCoords.x(), boxIt->boxCoords.y(), boxIt->boxCoords.width(), boxIt->boxCoords.height()});
classBoxes[classBoxIt.key()] = boxesList;
}
}
}
datasetIt->setValue(classBoxes);
}
}
ValidationClassBoxes::~ValidationClassBoxes()
{
sync();
emit datasetListUpdated();
delete _ui;
}