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 | /* ============================================================
*
* This file is a part of digiKam project
* https://www.digikam.org
*
* Date : 2023-05-13
* Description : Integrated testing tool for bencmark deployed model and hyper parameters
* for object detection
* SPDX-FileCopyrightText: 2023 by Quoc Hung TRAN <quochungtran1999 at gmail dot com>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* ============================================================ */
// Qt includes
#include <QApplication>
#include <QCommandLineParser>
#include <QMainWindow>
#include <QScrollArea>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QFormLayout>
#include <QListWidget>
#include <QListWidgetItem>
#include <QDir>
#include <QImage>
#include <QElapsedTimer>
#include <QLabel>
#include <QPen>
#include <QPainter>
#include <QLineEdit>
#include <QPushButton>
#include <QRandomGenerator>
#include <QRadioButton>
#include <QCheckBox>
// Local includes
#include "digikam_debug.h"
#include "dnnbasedetectormodel.h"
#include "dnnyolodetector.h"
#include "dcombobox.h"
using namespace Digikam;
class MainWindow: public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(const QDir& directory, QWidget* const parent = nullptr);
~MainWindow() override;
private:
QWidget* setupFullImageArea();
QWidget* setupImageList(const QDir& directory);
QWidget* setupControlPanel();
private:
void drawObjects(QImage& img, const QHash<QString, QVector<QRect> >& detectedObjects);
private:
QHash<QString, QVector<QRect> > detectObjects(const QString& imagePath) const;
private Q_SLOTS:
void slotDetectObjects(const QListWidgetItem* imageItem);
void slotApplySettings();
void slotSettingsChanged(int type);
void slotObjectSelected();
private:
QLabel* m_fullImage = nullptr;
QListWidget* m_imageListView = nullptr;
DNNYoloDetector* m_yoloDetector = nullptr;
YoloVersions m_modelType;
QList<QString> m_objectSelected;
// control pann
QPushButton* m_applyButton = nullptr;
DComboBox* m_modelOptionsBox = nullptr;
QList<QCheckBox*> m_objectOptions;
Q_SIGNALS:
void signalApplyForImage(const QListWidgetItem* imageItem);
};
MainWindow::MainWindow(const QDir &directory, QWidget* const parent)
: QMainWindow(parent)
{
setWindowTitle(QLatin1String("Object Detection Test"));
m_yoloDetector = new DNNYoloDetector(YoloVersions::YOLOV5NANO);
// Image area
QWidget* const imageArea = new QWidget(this);
QHBoxLayout* processingLayout = new QHBoxLayout(imageArea);
processingLayout->addWidget(setupFullImageArea());
processingLayout->addWidget(setupControlPanel());
QSizePolicy spHigh(QSizePolicy::Preferred, QSizePolicy::Preferred);
spHigh.setVerticalPolicy(QSizePolicy::Expanding);
imageArea->setSizePolicy(spHigh);
QWidget* const mainWidget = new QWidget(this);
QVBoxLayout* const mainLayout = new QVBoxLayout(mainWidget);
mainLayout->addWidget(imageArea);
mainLayout->addWidget(setupImageList(directory));
setCentralWidget(mainWidget);
// set up connection
connect(m_modelOptionsBox, SIGNAL(activated(int)),
this, SLOT(slotSettingsChanged(int)));
connect(m_applyButton, SIGNAL(clicked()),
this, SLOT(slotApplySettings()));
for (auto opt : std::as_const(m_objectOptions))
{
connect(opt, SIGNAL(toggled(bool)),
this, SLOT(slotObjectSelected()));
}
connect(m_imageListView, &QListWidget::currentItemChanged,
this, &MainWindow::slotDetectObjects);
connect(this, SIGNAL(signalApplyForImage(const QListWidgetItem*)),
this, SLOT(slotDetectObjects(const QListWidgetItem*)));
}
MainWindow::~MainWindow()
{
delete m_imageListView;
delete m_applyButton;
delete m_yoloDetector;
delete m_modelOptionsBox;
}
QHash<QString, QVector<QRect> > MainWindow::detectObjects(const QString& imagePath) const
{
cv::Mat cvImage = cv::imread(imagePath.toStdString());
if (cvImage.empty())
{
qCDebug(DIGIKAM_TESTS_LOG) << "Image Path is not available";
return {};
}
return m_yoloDetector->detectObjects(cvImage);
}
void MainWindow::drawObjects(QImage& img, const QHash<QString, QVector<QRect> >& detectedObjects)
{
if (detectedObjects.empty())
{
qCDebug(DIGIKAM_TESTS_LOG) << "No object detected";
return;
}
QPainter painter(&img);
QPen paintPen(Qt::red);
paintPen.setWidth(2);
painter.setPen(paintPen);
// TODO Show cropped tags
// Decide which object we want to show
for (auto label : std::as_const(m_objectSelected))
{
QString cleanLabel = label.remove(QLatin1Char('&'));
for (auto rectDraw : detectedObjects[cleanLabel])
{
painter.drawRect(rectDraw);
}
}
}
void MainWindow::slotDetectObjects(const QListWidgetItem* imageItem)
{
QString imagePath = imageItem->text();
qCDebug(DIGIKAM_TESTS_LOG) << "Loading" << imagePath;
QImage img(imagePath);
QHash<QString, QVector<QRect> > objects = detectObjects(imagePath);
drawObjects(img, objects);
qCDebug(DIGIKAM_TESTS_LOG) << QString::fromLatin1("Inference Time %1").arg(m_yoloDetector->showInferenceTime());
m_fullImage->setPixmap(QPixmap::fromImage(img));
}
QWidget* MainWindow::setupFullImageArea()
{
m_fullImage = new QLabel;
m_fullImage->setScaledContents(true);
QSizePolicy spImage(QSizePolicy::Preferred, QSizePolicy::Expanding);
spImage.setHorizontalStretch(3);
m_fullImage->setSizePolicy(spImage);
return m_fullImage;
}
QWidget* MainWindow::setupControlPanel()
{
QWidget* const controlPanel = new QWidget();
QSizePolicy spImage(QSizePolicy::Preferred, QSizePolicy::Expanding);
spImage.setHorizontalStretch(2);
controlPanel->setSizePolicy(spImage);
m_applyButton = new QPushButton(this);
m_applyButton->setText(QLatin1String("Apply"));
m_modelOptionsBox = new DComboBox(this);
m_modelOptionsBox->insertItem(int(YoloVersions::YOLOV5XLARGE), QLatin1String("yolov5 Xlarge"));
m_modelOptionsBox->insertItem(int(YoloVersions::YOLOV5NANO), QLatin1String("yolov5 nano"));
m_modelOptionsBox->setDefaultIndex(int(YoloVersions::YOLOV5NANO));
QScrollArea* const objectsSelectArea = new QScrollArea();
objectsSelectArea->setWidgetResizable(true);
objectsSelectArea->setAlignment(Qt::AlignRight);
objectsSelectArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
objectsSelectArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
QWidget* const objectsSelectView = new QWidget();
QVBoxLayout* const vLayout = new QVBoxLayout(this);
QList<QString> predClass = m_yoloDetector->getPredefinedClasses();
for (int i = 0 ; i < predClass.size() ; i++)
{
QCheckBox* checkBox = new QCheckBox(predClass[i], objectsSelectView);<--- Variable 'checkBox' can be declared as pointer to const
m_objectOptions.append(checkBox);
vLayout->addWidget(checkBox);
}
objectsSelectView->setLayout(vLayout);
objectsSelectArea->setWidget(objectsSelectView);
QFormLayout* const layout = new QFormLayout(this);
layout->insertRow(0, m_modelOptionsBox);
layout->insertRow(1, m_applyButton);
layout->insertRow(2, objectsSelectArea);
controlPanel->setLayout(layout);
return controlPanel;
}
void MainWindow::slotObjectSelected()
{
m_objectSelected.clear();
for (const auto opt : std::as_const(m_objectOptions))
{
if (opt->isChecked())
{
m_objectSelected.append(QString::fromLatin1("%1").arg(opt->text()));
}
}
}
void MainWindow::slotApplySettings()
{
m_yoloDetector = new DNNYoloDetector(m_modelType);
QListWidgetItem* const item = m_imageListView->currentItem();
if (item && m_imageListView->selectedItems().contains(item))
{
Q_EMIT signalApplyForImage(m_imageListView->currentItem());
}
}
void MainWindow::slotSettingsChanged(int type)
{
m_modelType = static_cast<YoloVersions> (type);
}
QWidget* MainWindow::setupImageList(const QDir& directory)
{
// Itemlist erea
QScrollArea* const itemsArea = new QScrollArea;
itemsArea->setWidgetResizable(true);
itemsArea->setAlignment(Qt::AlignBottom);
itemsArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
itemsArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
QSizePolicy spLow(QSizePolicy::Preferred, QSizePolicy::Fixed);
itemsArea->setSizePolicy(spLow);
m_imageListView = new QListWidget(this);
m_imageListView->setViewMode(QListView::IconMode);
m_imageListView->setIconSize(QSize(200, 150));
m_imageListView->setResizeMode(QListWidget::Adjust);
m_imageListView->setFlow(QListView::LeftToRight);
m_imageListView->setWrapping(false);
m_imageListView->setDragEnabled(false);
// generate list widget items
QVector<QListWidgetItem*> imageItems;
QFileInfoList filesInfo = directory.entryInfoList(QDir::Files | QDir::NoDotAndDotDot);
for (int j = 0 ; j < filesInfo.size() ; j++)
{
QImage img(filesInfo[j].absoluteFilePath());
if(! img.isNull())
{
imageItems.append(new QListWidgetItem(QIcon(filesInfo[j].absoluteFilePath()),
filesInfo[j].absoluteFilePath()));
}
}
for (auto item : imageItems)
{
m_imageListView->addItem(item);
}
// set list view into items area
itemsArea->setWidget(m_imageListView);
return itemsArea;
}
QCommandLineParser* parseOptions(const QCoreApplication& app)
{
QCommandLineParser* const parser = new QCommandLineParser();
parser->addOption(QCommandLineOption(QLatin1String("dataset"),
QLatin1String("Data set folder"),
QLatin1String("path relative to data folder")));
parser->addHelpOption();
parser->process(app);
return parser;
}
int main(int argc, char** argv)
{
QApplication app(argc, argv);
app.setApplicationName(QString::fromLatin1("digikam"));
// Options for commandline parser
QCommandLineParser* const parser = parseOptions(app);
if (!parser->isSet(QLatin1String("dataset")))
{
qWarning(DIGIKAM_TESTS_LOG) << "Data set is not set !!!";
}
QDir dataset(parser->value(QLatin1String("dataset")));
MainWindow* const window = new MainWindow(dataset, nullptr);
window->show();
return app.exec();
}
#include "detect_object_gui.moc"
|