| 1 |
/* ============================================================ |
| 2 |
* |
| 3 |
* This file is a part of digiKam project |
| 4 |
* https://www.digikam.org |
| 5 |
* |
| 6 |
* Date : 2025-03-30 |
| 7 |
* Description : Face Background Recognition Controller |
| 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 "facebackgroundrecognition.h" |
| 17 |
|
| 18 |
// Qt includes |
| 19 |
|
| 20 |
#include <QTimer> |
| 21 |
#include <QThread> // For QThread::msleep |
| 22 |
#include <QException> |
| 23 |
#include <QCoreApplication> // For QCoreApplication::processEvents |
| 24 |
|
| 25 |
// Local includes |
| 26 |
|
| 27 |
#include "digikam_debug.h" |
| 28 |
#include "applicationsettings.h" |
| 29 |
#include "facescansettings.h" |
| 30 |
#include "facesengine.h" |
| 31 |
#include "albummanager.h" |
| 32 |
#include "faceclassifier.h" |
| 33 |
|
| 34 |
namespace Digikam |
| 35 |
{ |
| 36 |
|
| 37 |
class Q_DECL_HIDDEN FaceRecognitionBackgroundController::Private |
| 38 |
{ |
| 39 |
public: |
| 40 |
|
| 41 |
FacesEngine* facesEngine = nullptr; |
| 42 |
bool firstRun = true; |
| 43 |
bool runAgain = false; |
| 44 |
FaceScanSettings settings; |
| 45 |
}; |
| 46 |
|
| 47 |
class Q_DECL_HIDDEN FaceRecognitionBackgroundControllerCreator |
| 48 |
{ |
| 49 |
public: |
| 50 |
|
| 51 |
FaceRecognitionBackgroundController object; |
| 52 |
}; |
| 53 |
|
| 54 |
Q_GLOBAL_STATIC(FaceRecognitionBackgroundControllerCreator, creator) |
| 55 |
|
| 56 |
// ----------------------------------------------------------------- |
| 57 |
|
| 58 |
FaceRecognitionBackgroundController::FaceRecognitionBackgroundController() |
| 59 |
: QObject(), |
| 60 |
d (new Private) |
| 61 |
{ |
| 62 |
bool enabled = ApplicationSettings::instance()->getFaceRecognitionBackgroundScan(); |
| 63 |
|
| 64 |
// set the connection to the FaceClassifier |
| 65 |
|
| 66 |
slotSetEnabled(enabled); |
| 67 |
|
| 68 |
if (!enabled) |
| 69 |
{ |
| 70 |
// If the background scan is disabled, we need to set the firstRun flag to false |
| 71 |
// The firstRun flag is used to avoid running the scan when the FaceClassifier starts |
| 72 |
// but we want to run it when the user enables the background scan and not skip the |
| 73 |
// first notification |
| 74 |
|
| 75 |
d->firstRun = false; |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
FaceRecognitionBackgroundController::~FaceRecognitionBackgroundController() |
| 80 |
{ |
| 81 |
if (d->facesEngine) |
| 82 |
{ |
| 83 |
d->facesEngine->cancel(); |
| 84 |
waitForDone(); |
| 85 |
} |
| 86 |
|
| 87 |
delete d; |
| 88 |
} |
| 89 |
|
| 90 |
FaceRecognitionBackgroundController* FaceRecognitionBackgroundController::instance() |
| 91 |
{ |
| 92 |
return &creator->object; |
| 93 |
} |
| 94 |
|
| 95 |
void FaceRecognitionBackgroundController::slotSetEnabled(bool enabled) |
| 96 |
{ |
| 97 |
if (enabled) |
| 98 |
{ |
| 99 |
connect(FaceClassifier::instance(), &FaceClassifier::signalTrainingComplete, |
| 100 |
this, &FaceRecognitionBackgroundController::slotRescan); |
| 101 |
} |
| 102 |
else |
| 103 |
{ |
| 104 |
disconnect(FaceClassifier::instance(), &FaceClassifier::signalTrainingComplete, |
| 105 |
this, &FaceRecognitionBackgroundController::slotRescan); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
bool FaceRecognitionBackgroundController::slotRescan() |
| 110 |
{ |
| 111 |
// check if background scan already running |
| 112 |
|
| 113 |
if (d->facesEngine) |
| 114 |
{ |
| 115 |
d->runAgain = true; |
| 116 |
return false; |
| 117 |
} |
| 118 |
|
| 119 |
d->settings.readFromConfig(); |
| 120 |
|
| 121 |
// the background process is recognizing faces only |
| 122 |
|
| 123 |
d->settings.task = FaceScanSettings::RecognizeMarkedFaces; |
| 124 |
d->settings.alreadyScannedHandling = FaceScanSettings::RecognizeOnly; |
| 125 |
|
| 126 |
// use only a single thread to avoid overloading the CPU |
| 127 |
|
| 128 |
d->settings.useFullCpu = false; |
| 129 |
|
| 130 |
// scan all albums |
| 131 |
|
| 132 |
d->settings.wholeAlbums = true; |
| 133 |
d->settings.albums = AlbumManager::instance()->allPAlbums(); |
| 134 |
|
| 135 |
d->settings.infos.clear(); |
| 136 |
|
| 137 |
// set the thread priority to idle to avoid slowing down the UI |
| 138 |
|
| 139 |
d->settings.workerThreadPriority = QThread::IdlePriority; |
| 140 |
|
| 141 |
// set the source to background recognition |
| 142 |
|
| 143 |
d->settings.source = FaceScanSettings::BackgroundRecognition; |
| 144 |
|
| 145 |
// don't run the first time this function is called |
| 146 |
// this is to avoid running the scan when the application starts |
| 147 |
|
| 148 |
if (d->firstRun) |
| 149 |
{ |
| 150 |
d->firstRun = false; |
| 151 |
return false; |
| 152 |
} |
| 153 |
|
| 154 |
// clear the runAgain flag |
| 155 |
|
| 156 |
d->runAgain = false; |
| 157 |
|
| 158 |
try |
| 159 |
{ |
| 160 |
// create the faces engine |
| 161 |
|
| 162 |
d->facesEngine = new FacesEngine(d->settings, nullptr); |
| 163 |
|
| 164 |
// connect signals and slots |
| 165 |
|
| 166 |
connect(d->facesEngine, &FacesEngine::signalComplete, |
| 167 |
this, &FaceRecognitionBackgroundController::slotScanDone); |
| 168 |
|
| 169 |
/* |
| 170 |
connect(facesDetector, SIGNAL(signalComplete()), |
| 171 |
d->parentInstance, SLOT(slotScanComplete())); |
| 172 |
|
| 173 |
connect(facesDetector, SIGNAL(signalCanceled()), |
| 174 |
d->parentInstance, SLOT(slotScanComplete())); |
| 175 |
|
| 176 |
connect(facesDetector, SIGNAL(signalScanNotification(QString,int)), |
| 177 |
d->parentInstance, SIGNAL(signalNotificationError(QString,int))); |
| 178 |
*/ |
| 179 |
// start the faces engine |
| 180 |
|
| 181 |
d->facesEngine->start(); |
| 182 |
|
| 183 |
return true; |
| 184 |
} |
| 185 |
|
| 186 |
catch(const QException& e) |
| 187 |
{ |
| 188 |
Q_UNUSED(e); |
| 189 |
qCDebug(DIGIKAM_FACESENGINE_LOG) << "FaceRecognitionBackgroundController::start: exception"; |
| 190 |
} |
| 191 |
|
| 192 |
catch(const std::exception& e) |
| 193 |
{ |
| 194 |
qCDebug(DIGIKAM_FACESENGINE_LOG) << "FaceRecognitionBackgroundController::start: exception: " << e.what(); |
| 195 |
} |
| 196 |
|
| 197 |
catch(...) |
| 198 |
{ |
| 199 |
qCDebug(DIGIKAM_FACESENGINE_LOG) << "FaceRecognitionBackgroundController::start: exception"; |
| 200 |
} |
| 201 |
|
| 202 |
d->facesEngine = nullptr; |
| 203 |
|
| 204 |
return false; |
| 205 |
} |
| 206 |
|
| 207 |
void FaceRecognitionBackgroundController::stop() |
| 208 |
{ |
| 209 |
// stop the faces engine if it is running |
| 210 |
|
| 211 |
d->runAgain = false; |
| 212 |
|
| 213 |
if (d->facesEngine) |
| 214 |
{ |
| 215 |
d->facesEngine->cancel(); |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
void FaceRecognitionBackgroundController::slotScanDone() |
| 220 |
{ |
| 221 |
// clean up the faces engine |
| 222 |
|
| 223 |
if (d->facesEngine) |
| 224 |
{ |
| 225 |
// d->faceEngine is deleted automatically, just set the pointer to null |
| 226 |
|
| 227 |
d->facesEngine = nullptr; |
| 228 |
} |
| 229 |
|
| 230 |
// if we need to run again, start the faces engine again |
| 231 |
|
| 232 |
if (d->runAgain) |
| 233 |
{ |
| 234 |
d->runAgain = false; |
| 235 |
QTimer::singleShot(100, this, &FaceRecognitionBackgroundController::slotRescan); |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
bool FaceRecognitionBackgroundController::waitForDone() |
| 240 |
{ |
| 241 |
// wait for the faces engine to finish |
| 242 |
|
| 243 |
while (d->facesEngine) |
| 244 |
{ |
| 245 |
// Allow Qt to process events to keep the application responsive |
| 246 |
|
| 247 |
QCoreApplication::processEvents(QEventLoop::AllEvents, 100); |
| 248 |
QThread::msleep(10); // Avoid busy-waiting by adding a small delay |
| 249 |
} |
| 250 |
|
| 251 |
return true; |
| 252 |
} |
| 253 |
|
| 254 |
} // namespace Digikam |
| 255 |
|
| 256 |
#include "moc_facebackgroundrecognition.cpp" |