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 | /* ============================================================
*
* This file is a part of digiKam project
* https://www.digikam.org
*
* Date : 2014-11-08
* Description : Object detection and image auto-tagging engine
*
* SPDX-FileCopyrightText: 2010 by Aditya Bhatt <adityabhatt1991 at gmail dot com>
* SPDX-FileCopyrightText: 2010-2025 by Gilles Caulier <caulier dot gilles at gmail dot com>
* SPDX-FileCopyrightText: 2012 by Andi Clemens <andi dot clemens at gmail dot com>
* SPDX-FileCopyrightText: 2024-2025 by Michael Miller <michael underscore miller at msn dot com>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* ============================================================ */
#include "autotagsengine.h"
// Qt includes
#include <QClipboard>
#include <QVBoxLayout>
#include <QTimer>
#include <QIcon>
#include <QPushButton>
#include <QApplication>
#include <QTextEdit>
#include <QHash>
#include <QPixmap>
// KDE includes
#include <kconfiggroup.h>
#include <klocalizedstring.h>
#include <ksharedconfig.h>
// Local includes
#include "facialrecognition_wrapper.h"
#include "digikam_debug.h"
#include "dnotificationwidget.h"
#include "coredb.h"
#include "album.h"
#include "albummanager.h"
#include "albumpointer.h"
#include "autotagsscansettings.h"
#include "iteminfojob.h"
#include "mlpipelinepackagenotify.h"
#include "autotagspipelineobject.h"
namespace Digikam
{
class Q_DECL_HIDDEN AutotagsEngine::Private
{
public:
Private() = default;
public:
AutotagsScanSettings settings;
bool benchmark = false;
AlbumPointerList<> albumTodoList;
ItemInfoList infoTodoList;
QList<qlonglong> idsTodoList;
ItemInfoJob albumListing;
AutotagsPipelineBase* newPipeline = nullptr;
int totalTagsAdded = 0;
};
AutotagsEngine::AutotagsEngine(const AutotagsScanSettings& _settings, ProgressItem* const parent)
: MaintenanceTool(QLatin1String("AutotagsEngine"), parent),
d (new Private)
{
d->settings = _settings;
d->newPipeline = new AutotagsPipelineObject(_settings);
connect(d->newPipeline, SIGNAL(finished()),
this, SLOT(slotDone()));
connect(d->newPipeline, SIGNAL(processed(MLPipelinePackageNotify::Ptr)),
this, SLOT(slotShowOneDetected(MLPipelinePackageNotify::Ptr)));
connect(d->newPipeline, SIGNAL(skipped(MLPipelinePackageNotify::Ptr)),
this, SLOT(slotImagesSkipped(MLPipelinePackageNotify::Ptr)));
connect(this, SIGNAL(progressItemCanceled(ProgressItem*)),
this, SLOT(slotCancel()));
connect(d->newPipeline, SIGNAL(signalUpdateItemCount(qlonglong)),
this, SLOT(slotUpdateItemCount(qlonglong)));
}
AutotagsEngine::~AutotagsEngine()
{
delete d->newPipeline;
delete d;
}
void AutotagsEngine::slotStart()
{
MaintenanceTool::slotStart();
setLabel(i18n("Autotags Assignment"));
setThumbnail(QIcon::fromTheme(QLatin1String("tag")).pixmap(48));
// Set label depending on settings.
if (d->settings.albums.size() > 0)
{
if (d->settings.albums.size() == 1)
{
setLabel(i18n("Scan for objects in album: %1", d->settings.albums.first()->title()));
}
else
{
setLabel(i18n("Scan for objects in %1 albums", d->settings.albums.size()));
}
}
ProgressManager::addProgressItem(this);
setUsesBusyIndicator(true);
// Get total count, cached by AlbumManager.
QHash<int, int> palbumCounts;
QHash<int, int> talbumCounts;
bool hasPAlbums = false;
bool hasTAlbums = false;
for (Album* const album : std::as_const(d->settings.albums))<--- Variable 'album' can be declared as pointer to const
{
if (album->type() == Album::PHYSICAL)
{
hasPAlbums = true;
}
else
{
hasTAlbums = true;
}
}
palbumCounts = AlbumManager::instance()->getPAlbumsCount();
talbumCounts = AlbumManager::instance()->getTAlbumsCount();
if (palbumCounts.isEmpty() && hasPAlbums)
{
QApplication::setOverrideCursor(Qt::WaitCursor);
palbumCounts = CoreDbAccess().db()->getNumberOfImagesInAlbums();
QApplication::restoreOverrideCursor();
}
if (talbumCounts.isEmpty() && hasTAlbums)
{
QApplication::setOverrideCursor(Qt::WaitCursor);
talbumCounts = CoreDbAccess().db()->getNumberOfImagesInTags();
QApplication::restoreOverrideCursor();
}
// First, we use the progressValueMap map to store absolute counts.
QHash<Album*, int> progressValueMap;
for (Album* const album : std::as_const(d->settings.albums))
{
if (album->type() == Album::PHYSICAL)
{
progressValueMap[album] = palbumCounts.value(album->id());
}
else
{
// This is possibly broken of course because we do not know if images have multiple tags,
// but there's no better solution without expensive operation.
progressValueMap[album] = talbumCounts.value(album->id());
}
}
// Second, calculate (approximate) overall sum.
int total = 0;
for (int count : std::as_const(progressValueMap))
{
// cppcheck-suppress useStlAlgorithm
total += count;
}
total = qMax(1, total);
qCDebug(DIGIKAM_GENERAL_LOG) << "Total is" << total;
setUsesBusyIndicator(false);
setTotalItems(total);
if (!d->newPipeline->start())
{
Q_EMIT signalScanNotification(QString(i18n("Error starting object detection.")), DNotificationWidget::Error);
}
}
void AutotagsEngine::slotUpdateItemCount(const qlonglong itemCount)
{
setTotalItems(itemCount);
}
void AutotagsEngine::slotDone()
{
QString lbl;
if (totalItems() > 1)
{
lbl.append(i18n("Items scanned for objects: %1\n", totalItems()));
}
else
{
lbl.append(i18n("Item scanned for objects: %1\n", totalItems()));
}
setLabel(lbl);
// Dispatch scan resume to the icon-view info pop-up.
Q_EMIT signalScanNotification(lbl, DNotificationWidget::Information);
MaintenanceTool::slotDone();
}
void AutotagsEngine::slotCancel()
{
d->newPipeline->cancel();
MaintenanceTool::slotCancel();
}
void AutotagsEngine::slotImagesSkipped(const MLPipelinePackageNotify::Ptr& package)
{
Q_UNUSED(package);
advance(1);
}
void AutotagsEngine::slotShowOneDetected(const MLPipelinePackageNotify::Ptr& package)
{
setThumbnail(package->thumbnail);
QString lbl = i18n("Album: %1\n", package->path);
lbl.append(i18n("Scanning: %1\n", package->name));
setLabel(lbl);
advance(1);
}
} // namespace Digikam
#include "moc_autotagsengine.cpp"
|