| 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 "facepipelinereset.h" |
| 17 |
|
| 18 |
// Qt includes |
| 19 |
|
| 20 |
#include <QSet> |
| 21 |
#include <QList> |
| 22 |
|
| 23 |
// KDE includes |
| 24 |
|
| 25 |
#include <klocalizedstring.h> |
| 26 |
|
| 27 |
// Local includes |
| 28 |
|
| 29 |
#include "digikam_debug.h" |
| 30 |
#include "digikam_opencv.h" |
| 31 |
#include "sharedqueue.h" |
| 32 |
#include "collectionmanager.h" |
| 33 |
#include "album.h" |
| 34 |
#include "coredb.h" |
| 35 |
#include "iteminfo.h" |
| 36 |
#include "faceutils.h" |
| 37 |
#include "facescansettings.h" |
| 38 |
#include "facepipelinepackagebase.h" |
| 39 |
#include "identityprovider.h" |
| 40 |
#include "identity.h" |
| 41 |
#include "dnnsfaceextractor.h" |
| 42 |
|
| 43 |
namespace Digikam |
| 44 |
{ |
| 45 |
|
| 46 |
FacePipelineReset::FacePipelineReset(const FaceScanSettings& _settings) |
| 47 |
: FacePipelineBase(_settings) |
| 48 |
{ |
| 49 |
} |
| 50 |
|
| 51 |
bool FacePipelineReset::start() |
| 52 |
{ |
| 53 |
{ |
| 54 |
// use the mutex to synchronize the start of the threads |
| 55 |
|
| 56 |
QMutexLocker lock(&mutex); |
| 57 |
|
| 58 |
// add the worker threads for this pipeline |
| 59 |
|
| 60 |
addWorker(MLPipelineStage::Finder); |
| 61 |
/* |
| 62 |
addWorker(MLPipelineStage::Loader); |
| 63 |
addWorker(MLPipelineStage::Extractor); |
| 64 |
addWorker(MLPipelineStage::Classifier); |
| 65 |
*/ |
| 66 |
addWorker(MLPipelineStage::Writer); |
| 67 |
} |
| 68 |
|
| 69 |
return FacePipelineBase::start(); |
| 70 |
} |
| 71 |
|
| 72 |
bool FacePipelineReset::finder() |
| 73 |
{ |
| 74 |
ActionThreadBase::setCurrentThreadName(QString::fromLatin1("FaceReset%1").arg(QLatin1String(__FUNCTION__))); // To customize thread name |
| 75 |
|
| 76 |
MLPIPELINE_FINDER_START(settings.workerThreadPriority, MLPipelineStage::Writer); |
| 77 |
|
| 78 |
/* ========================================================================================= |
| 79 |
* Pipeline finder specific initialization code |
| 80 |
* |
| 81 |
* Use the block from here to MLPIPELINE_FINDER_END to find the IDs images to process. |
| 82 |
* The code in this block is run once per stage initialization. The number of instances |
| 83 |
* is alaways 1. |
| 84 |
*/ |
| 85 |
|
| 86 |
// get the IDs to process |
| 87 |
|
| 88 |
FaceUtils utils; |
| 89 |
QSet<qlonglong> filter; |
| 90 |
int serialNumber = 0; |
| 91 |
|
| 92 |
for (const Album* const album : std::as_const(settings.albums)) |
| 93 |
{ |
| 94 |
if (cancelled) |
| 95 |
{ |
| 96 |
break; |
| 97 |
} |
| 98 |
|
| 99 |
if (!album->isTrashAlbum()) |
| 100 |
{ |
| 101 |
QList<qlonglong> imageIds = CoreDbAccess().db()->getImageIds(album->id(), |
| 102 |
DatabaseItem::Status::Visible, |
| 103 |
true); |
| 104 |
|
| 105 |
if (!moreCpu) |
| 106 |
{ |
| 107 |
moreCpu = checkMoreWorkers(totalItemCount, imageIds.size(), settings.useFullCpu); |
| 108 |
} |
| 109 |
|
| 110 |
for (qlonglong imageId : std::as_const(imageIds)) |
| 111 |
{ |
| 112 |
// filter out duplicate image IDs |
| 113 |
|
| 114 |
if (!filter.contains(imageId)) |
| 115 |
{ |
| 116 |
++totalItemCount; |
| 117 |
enqueue(nextQueue, new FacePipelinePackageBase(imageId, ++serialNumber)); |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
/* ========================================================================================= |
| 124 |
* Pipeline finder specific cleanup |
| 125 |
* |
| 126 |
* Use the block from here to MLPIPELINE_FINDER_END to clean up any resources used by the stage. |
| 127 |
*/ |
| 128 |
|
| 129 |
MLPIPELINE_FINDER_END(MLPipelineStage::Writer); |
| 130 |
} |
| 131 |
|
| 132 |
bool FacePipelineReset::writer() |
| 133 |
{ |
| 134 |
ActionThreadBase::setCurrentThreadName(QString::fromLatin1("FaceReset%1").arg(QLatin1String(__FUNCTION__))); // To customize thread name |
| 135 |
|
| 136 |
MLPIPELINE_STAGE_START(QThread::LowPriority, MLPipelineStage::Writer, MLPipelineStage::None); |
| 137 |
FacePipelinePackageBase* package = nullptr; |
| 138 |
|
| 139 |
/* ========================================================================================= |
| 140 |
* Pipeline stage specific initialization code |
| 141 |
* |
| 142 |
* Use the block from here to MLPIPELINE_LOOP_START to initialize the stage. |
| 143 |
* The code in this block is run once per stage initialization. The number of instances |
| 144 |
* is at least 1. More instances are created by addMoreWorkers if needed. |
| 145 |
*/ |
| 146 |
|
| 147 |
FaceUtils utils; |
| 148 |
FaceTagsEditor editor; |
| 149 |
IdentityProvider* const idProvider = IdentityProvider::instance(); |
| 150 |
QIcon icon = QIcon::fromTheme(QLatin1String("person")); |
| 151 |
|
| 152 |
// clear all identities and training from recognition DB before |
| 153 |
// we start looping through the results from the pipeline |
| 154 |
|
| 155 |
idProvider->clearAllTraining(); |
| 156 |
|
| 157 |
MLPIPELINE_LOOP_START(MLPipelineStage::Writer, thisQueue); |
| 158 |
package = static_cast<FacePipelinePackageBase*>(mlpackage); |
| 159 |
|
| 160 |
/* ========================================================================================= |
| 161 |
* Start pipeline stage specific loop |
| 162 |
* |
| 163 |
* All code from here to MLPIPELINE_LOOP_END is in a try/catch block and loop. |
| 164 |
* This loop is run once per image. |
| 165 |
*/ |
| 166 |
|
| 167 |
{ |
| 168 |
// remove the face tags from the image |
| 169 |
|
| 170 |
QList<FaceTagsIface> databaseFaces = editor.unconfirmedFaceTagsIfaces(package->info.id()); |
| 171 |
|
| 172 |
editor.removeFaces(databaseFaces); |
| 173 |
|
| 174 |
databaseFaces = editor.unconfirmedNameFaceTagsIfaces(package->info.id()); |
| 175 |
|
| 176 |
editor.removeFaces(databaseFaces); |
| 177 |
|
| 178 |
editor.removeAllFaces(package->info.id()); |
| 179 |
|
| 180 |
// mark the image as unscanned |
| 181 |
|
| 182 |
utils.markAsScanned(package->info, false); |
| 183 |
|
| 184 |
QString albumName = CollectionManager::instance()->albumRootLabel(package->info.albumRootId()); |
| 185 |
|
| 186 |
// send a notification that the image was processed |
| 187 |
|
| 188 |
notify(MLPipelineNotification::notifyProcessed, |
| 189 |
package->info.name(), |
| 190 |
albumName + package->info.relativePath(), |
| 191 |
QString(), |
| 192 |
package->faceRects.size(), |
| 193 |
icon); |
| 194 |
|
| 195 |
// delete the package |
| 196 |
|
| 197 |
delete package; |
| 198 |
|
| 199 |
package = nullptr; |
| 200 |
} |
| 201 |
|
| 202 |
/* ========================================================================================= |
| 203 |
* End pipeline stage specific loop |
| 204 |
*/ |
| 205 |
|
| 206 |
MLPIPELINE_LOOP_END(MLPipelineStage::Writer, "FacePipelineReset::writer"); |
| 207 |
|
| 208 |
/* ========================================================================================= |
| 209 |
* Pipeline stage specific cleanup |
| 210 |
* |
| 211 |
* Use the block from here to MLPIPELINE_STAGE_END to clean up any resources used by the stage. |
| 212 |
*/ |
| 213 |
|
| 214 |
// retrain the classifier after all results have been processed |
| 215 |
|
| 216 |
FaceClassifier::instance()->retrain(); |
| 217 |
|
| 218 |
MLPIPELINE_STAGE_END(MLPipelineStage::Writer, MLPipelineStage::None); |
| 219 |
} |
| 220 |
|
| 221 |
void FacePipelineReset::addMoreWorkers() |
| 222 |
{ |
| 223 |
// reset pipeline is always single thread per stage |
| 224 |
} |
| 225 |
|
| 226 |
} // namespace Digikam |
| 227 |
|
| 228 |
#include "moc_facepipelinereset.cpp" |