Debian Codegraph @

core/utilities/facemanagement/pipelines/retrain/facepipelineretrain.cpp

raw
1 /* ============================================================
2 *
3 * This file is a part of digiKam project
4 * https://www.digikam.org
5 *
6 * Date : 2024-11-10
7 * Description : Performs face detection and recognition
8 *
9 * SPDX-FileCopyrightText: 2024-2026 by Gilles Caulier <caulier dot gilles at gmail dot com>
10 * SPDX-FileCopyrightText: 2024-2025 by Michael Miller <michael underscore miller at msn dot com>
11 *
12 * SPDX-License-Identifier: GPL-2.0-or-later
13 *
14 * ============================================================ */
15
16 #include "facepipelineretrain.h"
17
18 // Qt includes
19
20 #include <QList>
21 #include <QSet>
22 #include <QElapsedTimer>
23 #include <QRectF>
24
25 // KDE includes
26
27 #include <klocalizedstring.h>
28
29 // Local includes
30
31 #include "digikam_debug.h"
32 #include "digikam_opencv.h"
33 #include "sharedqueue.h"
34 #include "collectionmanager.h"
35 #include "album.h"
36 #include "iteminfo.h"
37 #include "coredb.h"
38 #include "facescansettings.h"
39 #include "dimg.h"
40 #include "previewloadthread.h"
41 #include "faceutils.h"
42 #include "facedbaccess.h"
43 #include "facedb.h"
44 #include "facepipelinepackagebase.h"
45 #include "thumbnailloadthread.h"
46 #include "identityprovider.h"
47 #include "identity.h"
48 #include "dnnsfaceextractor.h"
49 #include "faceclassifier.h"
50
51 namespace Digikam
52 {
53
54 FacePipelineRetrain::FacePipelineRetrain(const FaceScanSettings& _settings)
55 : FacePipelineBase(_settings)
56 {
57 }
58
59 bool FacePipelineRetrain::start()
60 {
61 QVariantMap params;
62 params[QLatin1String("detectAccuracy")] = settings.detectAccuracy;
63 params[QLatin1String("detectModel")] = settings.detectModel;
64 params[QLatin1String("detectSize")] = settings.detectSize;
65 params[QLatin1String("recognizeAccuracy")] = settings.recognizeAccuracy;
66 params[QLatin1String("recognizeModel")] = settings.recognizeModel;
67 detector.setParameters(params);
68
69 {
70 // use the mutex to synchronize the start of the threads
71
72 QMutexLocker lock(&mutex);
73
74 // add the worker threads for this pipeline
75
76 addWorker(MLPipelineStage::Finder);
77 addWorker(MLPipelineStage::Loader);
78 addWorker(MLPipelineStage::Extractor);
79 addWorker(MLPipelineStage::Classifier);
80 addWorker(MLPipelineStage::Writer);
81 }
82
83 return FacePipelineBase::start();
84 }
85
86 bool FacePipelineRetrain::finder()
87 {
88 ActionThreadBase::setCurrentThreadName(QString::fromLatin1("FaceRetrain%1").arg(QLatin1String(__FUNCTION__))); // To customize thread name
89
90 MLPIPELINE_FINDER_START(settings.workerThreadPriority, MLPipelineStage::Loader);
91
92 /* =========================================================================================
93 * Pipeline finder specific initialization code
94 *
95 * Use the block from here to MLPIPELINE_FINDER_END to find the IDs images to process.
96 * The code in this block is run once per stage initialization. The number of instances
97 * is always 1.
98 */
99
100 FaceUtils utils;
101
102 // get the IDs to process
103
104 QSet<qlonglong> filter;
105 int serialNumber = 0;
106
107 for (const Album* const album : std::as_const(settings.albums))
108 {
109 if (cancelled)
110 {
111 break;
112 }
113
114 if (!album->isTrashAlbum())
115 {
116 QList<qlonglong> imageIds = CoreDbAccess().db()->getImageIds(album->id(),
117 DatabaseItem::Status::Visible,
118 true);
119
120 if (!moreCpu)
121 {
122 moreCpu = checkMoreWorkers(totalItemCount, imageIds.size(), settings.useFullCpu);
123 }
124
125 for (qlonglong imageId : std::as_const(imageIds))
126 {
127 // filter out duplicate image IDs
128
129 if (!filter.contains(imageId))
130 {
131 QList<FaceTagsIface> faces = utils.confirmedFaceTagsIfaces(imageId);
132
133 for (const FaceTagsIface& face : std::as_const(faces))
134 {
135 ++totalItemCount;
136
137 enqueue(nextQueue, new FacePipelinePackageBase(imageId, face, ++serialNumber));
138 }
139 }
140 }
141 }
142 }
143
144 /* =========================================================================================
145 * Pipeline finder specific cleanup
146 *
147 * Use the block from here to MLPIPELINE_FINDER_END to clean up any resources used by the stage.
148 */
149
150 MLPIPELINE_FINDER_END(MLPipelineStage::Loader);
151 }
152
153 bool FacePipelineRetrain::loader()
154 {
155 ActionThreadBase::setCurrentThreadName(QString::fromLatin1("FaceRetrain%1").arg(QLatin1String(__FUNCTION__))); // To customize thread name
156
157 return commonFaceThumbnailLoader(QStringLiteral("FacePipelineRetrain"),
158 QThread::LowPriority,
159 MLPipelineStage::Loader,
160 MLPipelineStage::Extractor);
161 }
162
163 bool FacePipelineRetrain::extractor()
164 {
165 ActionThreadBase::setCurrentThreadName(QString::fromLatin1("FaceRetrain%1").arg(QLatin1String(__FUNCTION__))); // To customize thread name
166
167 return commonFaceThumbnailExtractor(QStringLiteral("FacePipelineRetrain"),
168 QThread::LowPriority,
169 MLPipelineStage::Extractor,
170 MLPipelineStage::Writer,
171 true);
172 }
173
174 bool FacePipelineRetrain::writer()
175 {
176 ActionThreadBase::setCurrentThreadName(QString::fromLatin1("FaceRetrain%1").arg(QLatin1String(__FUNCTION__))); // To customize thread name
177
178 MLPIPELINE_STAGE_START(QThread::LowPriority, MLPipelineStage::Writer, MLPipelineStage::None);
179 FacePipelinePackageBase* package = nullptr;
180
181 /* =========================================================================================
182 * Pipeline stage specific initialization code
183 *
184 * Use the block from here to MLPIPELINE_LOOP_START to initialize the stage.
185 * The code in this block is run once per stage initialization. The number of instances
186 * is at least 1. More instances are created by addMoreWorkers if needed.
187 */
188
189 FaceUtils utils;
190 IdentityProvider* const idProvider = IdentityProvider::instance();
191
192 // clear all identities and training from recognition DB before
193 // we start looping through the results from the pipeline
194
195 idProvider->clearAllTraining();
196
197 MLPIPELINE_LOOP_START(MLPipelineStage::Writer, thisQueue);
198 package = static_cast<FacePipelinePackageBase*>(mlpackage);
199
200 /* =========================================================================================
201 * Start pipeline stage specific loop
202 *
203 * All code from here to MLPIPELINE_LOOP_END is in a try/catch block and loop.
204 * This loop is run once per image.
205 */
206
207 {
208 QString displayName;
209
210 if (0 != package->features.rows)
211 {
212 Identity identity = utils.identityForTag(package->face.tagId());
213 displayName = identity.attribute(QStringLiteral("name"));
214
215 if (package->useForTraining)
216 {
217 idProvider->addTraining(identity, package->face.hash(), package->features);
218 }
219 else
220 {
221 qCDebug(DIGIKAM_FACESENGINE_LOG) << "FacePipelineEdit::writer(): not using for training: "
222 << package->info.filePath();
223 }
224 }
225 else
226 {
227 qCDebug(DIGIKAM_FACESENGINE_LOG) << "FacePipelineEdit::writer(): bad mat";
228 }
229
230 QString albumName = CollectionManager::instance()->albumRootLabel(package->info.albumRootId());
231
232 // send a notification that the image was processed
233
234 notify(MLPipelineNotification::notifyProcessed,
235 package->info.name(),
236 albumName + package->info.relativePath(),
237 displayName,
238 (displayName.isEmpty() ? 0 : 1),
239 package->thumbnail);
240
241 // delete the package
242
243 delete package;
244
245 package = nullptr;
246 }
247
248 /* =========================================================================================
249 * End pipeline stage specific loop
250 */
251
252 MLPIPELINE_LOOP_END(MLPipelineStage::Writer, "FacePipelineRetrain::writer");
253
254 /* =========================================================================================
255 * Pipeline stage specific cleanup
256 *
257 * Use the block from here to MLPIPELINE_STAGE_END to clean up any resources used by the stage.
258 */
259
260 // retrain the classifier after all results have been processed
261
262 FaceClassifier::instance()->retrain();
263
264 // set the training version info to the latest info
265
266 FaceDbAccess().db()->setTrainingVersionInfo(IdentityProvider::FaceTrainingVersion, IdentityProvider::ExtractorModel);
267
268 MLPIPELINE_STAGE_END(MLPipelineStage::Writer, MLPipelineStage::None);
269 }
270
271 void FacePipelineRetrain::addMoreWorkers()
272 {
273 addWorker(Loader);
274 addWorker(Extractor);
275 addWorker(Extractor);
276 }
277
278 } // namespace Digikam
279
280 #include "moc_facepipelineretrain.cpp"