Debian Codegraph @

core/utilities/facemanagement/pipelines/detectrecognize/facepipelinedetectrecognize.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 "facepipelinedetectrecognize.h"
17
18 // C++ includes
19
20 #include <climits>
21 #include <cmath>
22
23 // Qt includes
24
25 #include <QSet>
26 #include <QList>
27 #include <QRectF>
28 #include <QElapsedTimer>
29
30 // KDE includes
31
32 #include <klocalizedstring.h>
33
34 // Local includes
35
36 #include "digikam_debug.h"
37 #include "digikam_opencv.h"
38 #include "sharedqueue.h"
39 #include "collectionmanager.h"
40 #include "album.h"
41 #include "coredb.h"
42 #include "iteminfo.h"
43 #include "previewloadthread.h"
44 #include "faceutils.h"
45 #include "facescansettings.h"
46 #include "facepipelinepackagebase.h"
47 #include "thumbnailloadthread.h"
48 #include "faceclassifier.h"
49 #include "identityprovider.h"
50 #include "qtopencvimg.h"
51 #include "ocvocldnnsetter.h"
52
53 namespace Digikam
54 {
55
56 FacePipelineDetectRecognize::FacePipelineDetectRecognize(const FaceScanSettings& _settings)
57 : FacePipelineBase(_settings)
58 {
59 }
60
61 FacePipelineDetectRecognize::~FacePipelineDetectRecognize()
62 {
63 if (faceDetector)
64 {
65 delete faceDetector;
66 }
67
68 // extractor is singleton, so no need to delete it
69 }
70
71 bool FacePipelineDetectRecognize::start()
72 {
73 // create the face detector and extractor
74
75 try
76 {
77 faceDetector = new DNNFaceDetectorYuNet;
78 faceExtractor = static_cast<DNNModelSFace*>(DNNModelManager::instance()->getModel(QStringLiteral("sface"),
79 DNNModelUsage::DNNUsageFaceRecognition));
80 faceExtractor->getNet();
81 }
82
83 catch (const std::exception& e)
84 {
85 std::cerr << e.what() << '\n';
86
87 if (faceDetector)
88 {
89 delete faceDetector;
90 faceDetector = nullptr;
91 }
92
93 // extractor is singleton, so no need to delete it
94 }
95
96 catch (...)
97 {
98 if (faceDetector)
99 {
100 delete faceDetector;
101 faceDetector = nullptr;
102 }
103 }
104
105 // check if the detector and extractor were created
106
107 if ((nullptr == faceDetector) || (nullptr == faceExtractor) || !faceExtractor->modelLoaded)
108 {
109 return false;
110 }
111
112 // set the face detection size and accuracy
113
114 faceDetector->setFaceDetectionSize(settings.detectSize);
115 faceDetector->uiConfidenceThreshold = settings.detectAccuracy;
116
117 {
118 // use the mutex to synchronize the start of the threads
119
120 QMutexLocker lock(&mutex);
121
122 // add the worker threads for this pipeline
123
124 addWorker(MLPipelineStage::Finder);
125 addWorker(MLPipelineStage::Loader);
126 addWorker(MLPipelineStage::Extractor);
127 addWorker(MLPipelineStage::Classifier);
128 addWorker(MLPipelineStage::Writer);
129 }
130
131 return FacePipelineBase::start();
132 }
133
134 bool FacePipelineDetectRecognize::finder()
135 {
136 ActionThreadBase::setCurrentThreadName(QString::fromLatin1("FaceDetReco%1").arg(QLatin1String(__FUNCTION__))); // To customize thread name
137
138 MLPIPELINE_FINDER_START(settings.workerThreadPriority, MLPipelineStage::Loader);
139
140 /* =========================================================================================
141 * Pipeline finder specific initialization code
142 *
143 * Use the block from here to MLPIPELINE_FINDER_END to find the IDs images to process.
144 * The code in this block is run once per stage initialization. The number of instances
145 * is always 1.
146 */
147
148 // get the IDs to process
149
150 QSet<qlonglong> filter;
151 int serialNumber = 0;
152
153 for (const Album* const album : std::as_const(settings.albums))
154 {
155 if (cancelled)
156 {
157 break;
158 }
159
160 if (!album->isTrashAlbum())
161 {
162 QList<qlonglong> imageIds;
163
164 // get the image IDs for the album
165
166 if (album->type() == Album::PHYSICAL)
167 {
168 imageIds = CoreDbAccess().db()->getImageIds(album->id(),
169 DatabaseItem::Status::Visible,
170 (FaceScanSettings::AlreadyScannedHandling::Skip !=
171 settings.alreadyScannedHandling));
172 }
173 else if (album->type() == Album::TAG)
174 {
175 imageIds = CoreDbAccess().db()->getItemIDsInTag(album->id());
176 }
177
178 // iterate over the image IDs and add unique IDs to the queue for processing
179
180 for (qlonglong imageId : std::as_const(imageIds))
181 {
182 // filter out duplicate image IDs
183
184 if (!filter.contains(imageId))
185 {
186 // quick check if we should add threads.
187
188 if (!moreCpu)
189 {
190 moreCpu = checkMoreWorkers(totalItemCount, imageIds.size(), settings.useFullCpu);
191 }
192
193 ++totalItemCount;
194 filter << imageId;
195
196 // add the image ID to the queue for processing
197
198 enqueue(nextQueue, new FacePipelinePackageBase(imageId, ++serialNumber));
199 }
200 }
201 }
202 }
203
204 for (const ItemInfo& info : std::as_const(settings.infos))
205 {
206 // filter out duplicate image IDs
207
208 qlonglong imageId = info.id();
209
210 if (!filter.contains(imageId))
211 {
212 // quick check if we should add threads.
213
214 if (!moreCpu)
215 {
216 moreCpu = checkMoreWorkers(totalItemCount, settings.infos.size(), settings.useFullCpu);
217 }
218
219 ++totalItemCount;
220 filter << imageId;
221
222 // add the image ID to the queue for processing
223
224 enqueue(nextQueue, new FacePipelinePackageBase(imageId, ++serialNumber));
225 }
226 }
227
228 /* =========================================================================================
229 * Pipeline finder specific cleanup
230 *
231 * Use the block from here to MLPIPELINE_FINDER_END to clean up any resources used by the stage.
232 */
233
234 MLPIPELINE_FINDER_END(MLPipelineStage::Loader);
235 }
236
237 bool FacePipelineDetectRecognize::loader()
238 {
239 ActionThreadBase::setCurrentThreadName(QString::fromLatin1("FaceDetReco%1").arg(QLatin1String(__FUNCTION__))); // To customize thread name
240
241 MLPIPELINE_STAGE_START(QThread::LowPriority, MLPipelineStage::Loader, MLPipelineStage::Extractor);
242 FacePipelinePackageBase* package = nullptr;
243
244 /* =========================================================================================
245 * Pipeline stage specific initialization code
246 *
247 * Use the block from here to MLPIPELINE_LOOP_START to initialize the stage.
248 * The code in this block is run once per stage initialization. The number of instances
249 * is at least 1. More instances are created by addMoreWorkers if needed.
250 */
251
252 MLPIPELINE_LOOP_START(MLPipelineStage::Loader, thisQueue);
253 package = static_cast<FacePipelinePackageBase*>(mlpackage);
254
255 /* =========================================================================================
256 * Start pipeline stage specific loop
257 *
258 * All code from here to MLPIPELINE_LOOP_END is in a try/catch block and loop.
259 * This loop is run once per image.
260 */
261 {
262 // check if the ID is for an image (not video or other file type)
263
264 bool sendNotification = true;
265
266 if (DatabaseItem::Category::Image == package->info.category())
267 {
268 PreviewSettings::RawLoading rawLoadingMode = PreviewSettings::RawPreviewAutomatic;
269
270 if (QStringLiteral("RAW-ARW") == package->info.format())
271 {
272 // fix for #447767: ARW preview is different aspect than the image
273
274 bool loadFullSize =
275 (
276 qMin(package->info.dimensions().width() / 2, package->info.dimensions().height() / 2) <
277 qMin(faceDetector->nnInputSizeRequired().height / 2, faceDetector->nnInputSizeRequired().width / 2)
278 );
279
280 rawLoadingMode = (loadFullSize ? PreviewSettings::RawPreviewFromRawFullSize
281 : PreviewSettings::RawPreviewFromRawHalfSize);
282 }
283
284 // load high quality image for detection.
285
286 package->image = PreviewLoadThread::loadHighQualitySynchronously(package->info.filePath(), rawLoadingMode);
287
288 // check for corrupted images that cannot be loaded
289
290 if (!package->image.isNull())
291 {
292 // create a thumbnail for the notification
293
294 package->thumbnailIcon = QIcon(package->image.smoothScale(48, 48, Qt::KeepAspectRatio).convertToPixmap());
295
296 // send to the next stage
297
298 enqueue(nextQueue, package);
299
300 package = nullptr;
301
302 sendNotification = false;
303 }
304 }
305
306 if (sendNotification)
307 {
308 // send a notification that the file was skipped
309
310 notify(MLPipelineNotification::notifySkipped,
311 package->info.name(),
312 package->info.relativePath(),
313 QString(),
314 0,
315 package->thumbnailIcon);
316
317 // delete the package since it is not needed
318
319 delete package;
320
321 package = nullptr;
322 }
323 }
324
325 /* =========================================================================================
326 * End pipeline stage specific loop
327 */
328
329 MLPIPELINE_LOOP_END(MLPipelineStage::Loader, "FacePipelineDetectRecognize::loader");
330
331 /* =========================================================================================
332 * Pipeline stage specific cleanup
333 *
334 * Use the block from here to MLPIPELINE_STAGE_END to clean up any resources used by the stage.
335 */
336
337 MLPIPELINE_STAGE_END(MLPipelineStage::Loader, MLPipelineStage::Extractor);
338 }
339
340 bool FacePipelineDetectRecognize::extractor()
341 {
342 ActionThreadBase::setCurrentThreadName(QString::fromLatin1("FaceDetReco%1").arg(QLatin1String(__FUNCTION__))); // To customize thread name
343
344 MLPIPELINE_STAGE_START(QThread::LowPriority, MLPipelineStage::Extractor, MLPipelineStage::Classifier);
345 FacePipelinePackageBase* package = nullptr;
346
347 /* =========================================================================================
348 * Pipeline stage specific initialization code
349 *
350 * Use the block from here to MLPIPELINE_LOOP_START to initialize the stage.
351 * The code in this block is run once per stage initialization. The number of instances
352 * is at least 1. More instances are created by addMoreWorkers if needed.
353 */
354
355 FaceUtils utils;
356
357 MLPIPELINE_LOOP_START(MLPipelineStage::Extractor, thisQueue);
358 package = static_cast<FacePipelinePackageBase*>(mlpackage);
359
360 /* =========================================================================================
361 * Start pipeline stage specific loop
362 *
363 * All code from here to MLPIPELINE_LOOP_END is in a try/catch block and loop.
364 * This loop is run once per image.
365 */
366
367 {
368 // preprocess the image
369
370 // copy the image to a cv::UMat
371
372 cv::UMat cvUResizedImage;
373 cv::UMat cvUImage = QtOpenCVImg::image2Mat(
374 package->image,
375 CV_8UC3,
376 QtOpenCVImg::MatColorOrder::MCO_BGR
377 )
378 .getUMat(cv::ACCESS_RW);
379
380 // resize the image if needed. Only resize if the image is larger than the input size of the detector
381
382 cv::Size inputImageSize = faceDetector->nnInputSizeRequired();
383 float resizeFactor = 1.0F;
384
385 if (std::max(cvUImage.cols, cvUImage.rows) > std::max(inputImageSize.width, inputImageSize.height))
386 {
387 // Image should be resized. YuNet image sizes are much more flexible than SSD and YOLO
388 // so we just need to make sure no one bound exceeds the max. No padding needed.
389
390 resizeFactor = std::min(static_cast<float>(inputImageSize.width) / static_cast<float>(cvUImage.cols),
391 static_cast<float>(inputImageSize.height) / static_cast<float>(cvUImage.rows));
392
393 int newWidth = (int)(resizeFactor * cvUImage.cols);
394 int newHeight = (int)(resizeFactor * cvUImage.rows);
395 cv::resize(cvUImage, cvUResizedImage, cv::Size(newWidth, newHeight));
396 }
397 else
398 {
399 cvUResizedImage = cvUImage;
400 }
401
402 // get reciprocal factor for resizing the face to back the original image size
403
404 float reciprocalFactor = 1.0F / resizeFactor;
405
406 // lock OpenCV OpenCL DNN settings while using UMat
407
408 OpenCVOpenCLDNNSetter openCLDNNSetter;;
409
410 // detect any faces in the image
411
412 cv::UMat udetectionResults = faceDetector->callModel(cvUResizedImage);
413
414 // process detected faces
415
416 if (udetectionResults.rows > 0)
417 {
418 cv::Mat detectionResults = udetectionResults.getMat(cv::ACCESS_READ);
419
420 // get list of all previously detected faces
421
422 QList<FaceTagsIface> faces = utils.databaseFaces(package->info.id());
423
424 QList<QRectF> faceFRects;
425
426 // Loop through the faces found.
427
428 for (int i = 0 ; i < detectionResults.rows ; ++i)
429 {
430 // Create the rect of the face.
431
432 const float x = detectionResults.at<float>(i, 0);
433 const float y = detectionResults.at<float>(i, 1);
434 const float width = detectionResults.at<float>(i, 2);
435 const float height = detectionResults.at<float>(i, 3);
436
437 if (!std::isfinite(x) ||
438 !std::isfinite(y) ||
439 !std::isfinite(width) ||
440 !std::isfinite(height) ||
441 (width <= 0.0F) ||
442 (height <= 0.0F))
443 {
444 continue;
445 }
446
447 const int X = static_cast<int>(x);
448 const int Y = static_cast<int>(y);
449 const int iwidth = static_cast<int>(width);
450 const int iheight = static_cast<int>(height);
451
452 if (
453 (X == INT_MIN) ||
454 (Y == INT_MIN) ||
455 (iwidth == INT_MIN) ||
456 (iheight == INT_MIN)
457 )
458 {
459 qCWarning(DIGIKAM_FACESENGINE_LOG) << "Invalid detection rectangle"
460 << "(" << X<< ", " << Y
461 << "[" << iwidth << ", " << iheight << "]"
462 << "for image"
463 << package->info.filePath();
464
465 continue;
466 }
467
468 // Add the rect to result list.
469
470 QRectF currentFaceFRect(qreal(X) / qreal(cvUResizedImage.cols),
471 qreal(Y) / qreal(cvUResizedImage.rows),
472 qreal(iwidth) / qreal(cvUResizedImage.cols),
473 qreal(iheight)/ qreal(cvUResizedImage.rows));
474
475 faceFRects << currentFaceFRect;
476
477 // compute current image relative rect
478
479 QRect rect = QRect(package->image.width() * currentFaceFRect.x(),
480 package->image.height() * currentFaceFRect.y(),
481 package->image.width() * currentFaceFRect.width(),
482 package->image.height() * currentFaceFRect.height());
483
484 // check if rect is already assigned to a face to filter out confirmed and ignored faces
485
486 bool found = false;
487 FaceTagsIface face;
488
489 if (faces.size() > 0)
490 {
491 for (int j = 0; j < faces.size(); ++j)
492 {
493 if (faces[j].region().intersects(TagRegion(rect), 0.5))
494 {
495 if (faces[j].isConfirmedName() || faces[j].isIgnoredName())
496 {
497 // face is already confirmed or ignored
498
499 found = true;
500 break;
501 }
502 else
503 {
504 // face is not confirmed or ignored, but already detected
505
506 face = faces[j];
507 break;
508 }
509 }
510 }
511 }
512
513 // if face is not found (confirmed), then extract for classification
514
515 if (!found)
516 {
517 cv::UMat ualignedFace;
518 cv::UMat uface_features;
519 cv::Mat face_features;
520
521 // extract the face vectors (features) for classification
522
523 // get cvMat of the face landmarks
524
525 cv::Mat row = detectionResults.row(i);
526
527 // convert the face landmarks to the full size image
528
529 for (int j = 0 ; j < row.cols ; ++j)
530 {
531 row.at<float>(j) = (int)(row.at<float>(j) * reciprocalFactor);
532 }
533
534 // convert the face landmarks to a UMat
535
536 cv::UMat urow = row.getUMat(cv::ACCESS_READ);
537
538 // align and crop the face
539
540 QMutexLocker lock(&(faceExtractor->mutex));
541
542 faceExtractor->getNet()->alignCrop(cvUImage, urow, ualignedFace);
543
544 cv::UMat paddedFace;
545
546 if (std::min(ualignedFace.cols, ualignedFace.rows) > 112)
547 {
548 // Image should be resized. YuNet image sizes are much more flexible than SSD and YOLO.
549 // So we just need to make sure no one bound exceeds the max. No padding needed.
550
551 float resizeFactor2 = std::min(static_cast<float>(112) / static_cast<float>(ualignedFace.cols),
552 static_cast<float>(112) / static_cast<float>(ualignedFace.rows));
553
554 int newWidth = (int)(resizeFactor2 * ualignedFace.cols);
555 int newHeight = (int)(resizeFactor2 * ualignedFace.rows);
556 cv::resize(ualignedFace, paddedFace, cv::Size(newWidth, newHeight));
557 }
558 else
559 {
560 paddedFace = ualignedFace.clone();
561 }
562
563 ualignedFace.release();
564
565 // get the face features
566
567 faceExtractor->getNet()->feature(paddedFace, uface_features);
568
569 face_features = uface_features.getMat(cv::ACCESS_READ);
570
571 // normalize the face features if we have any
572
573 if (0 < face_features.rows)
574 {
575 // normalize the face features
576
577 cv::Mat normalized_features;
578 normalize(face_features, normalized_features);
579
580 // add the face features and face rect to the package
581
582 package->featuresList << normalized_features;
583 package->faceRects << currentFaceFRect;
584 package->faceList << face;
585 }
586 }
587 }
588 }
589
590 // send the package to the next stage
591
592 enqueue(nextQueue, package);
593
594 package = nullptr;
595 }
596
597 /* =========================================================================================
598 * End pipeline stage specific loop
599 */
600
601 MLPIPELINE_LOOP_END(MLPipelineStage::Extractor, "FacePipelineDetectRecognize::extractor");
602
603 /* =========================================================================================
604 * Pipeline stage specific cleanup
605 *
606 * Use the block from here to MLPIPELINE_STAGE_END to clean up any resources used by the stage.
607 */
608
609 MLPIPELINE_STAGE_END(MLPipelineStage::Extractor, MLPipelineStage::Classifier);
610 }
611
612 bool FacePipelineDetectRecognize::classifier()
613 {
614 ActionThreadBase::setCurrentThreadName(QString::fromLatin1("FaceDetReco%1").arg(QLatin1String(__FUNCTION__))); // To customize thread name
615
616 MLPIPELINE_STAGE_START(QThread::LowPriority, MLPipelineStage::Classifier, MLPipelineStage::Writer);
617 FacePipelinePackageBase* package = nullptr;
618
619 /* =========================================================================================
620 * Pipeline stage specific initialization code
621 *
622 * Use the block from here to MLPIPELINE_LOOP_START to initialize the stage.
623 * The code in this block is run once per stage initialization. The number of instances
624 * is at least 1. More instances are created by addMoreWorkers if needed.
625 */
626
627 FaceClassifier* const classifier = FaceClassifier::instance();
628 classifier->setParameters(settings);
629
630 MLPIPELINE_LOOP_START(MLPipelineStage::Classifier, thisQueue);
631 package = static_cast<FacePipelinePackageBase*>(mlpackage);
632
633 /* =========================================================================================
634 * Start pipeline stage specific loop
635 *
636 * All code from here to MLPIPELINE_LOOP_END is in a try/catch block and loop.
637 * This loop is run once per image.
638 */
639
640 {
641 for (int i = 0 ; i < package->featuresList.size() ; ++i)
642 {
643 // verify the feature mat is not empty
644
645 if (0 < package->featuresList[i].rows)
646 {
647 QList<int> exclusionIdentityIds;
648
649 for (const auto tagId : package->faceList[i].rejectedFaceTagList())
650 {
651 // add the Identity ID for the rejected face tag to the exclusion list
652
653 QMultiMap<QString, QString> attributes = FaceTags::identityAttributes(tagId);
654 Identity identity = IdentityProvider::instance()->findIdentity(attributes);
655
656 if (!identity.isNull())
657 {
658 exclusionIdentityIds << identity.id();
659 }
660 }
661
662 // classify the features
663
664 package->labelList << classifier->predict(package->featuresList[i], exclusionIdentityIds);
665 }
666 else
667 {
668 package->labelList << FaceClassifier::UNKNOWN_LABEL_ID;
669 }
670 }
671
672 enqueue(nextQueue, package);
673
674 package = nullptr;
675 }
676
677 /* =========================================================================================
678 * End pipeline stage specific loop
679 */
680
681 MLPIPELINE_LOOP_END(MLPipelineStage::Classifier, "FacePipelineDetectRecognize::classifier");
682
683 /* =========================================================================================
684 * Pipeline stage specific cleanup
685 *
686 * Use the block from here to MLPIPELINE_STAGE_END to clean up any resources used by the stage.
687 */
688
689 MLPIPELINE_STAGE_END(MLPipelineStage::Classifier, MLPipelineStage::Writer);
690 }
691
692 bool FacePipelineDetectRecognize::writer()
693 {
694 ActionThreadBase::setCurrentThreadName(QString::fromLatin1("FaceDetReco%1").arg(QLatin1String(__FUNCTION__))); // To customize thread name
695
696 MLPIPELINE_STAGE_START(QThread::LowPriority, MLPipelineStage::Writer, MLPipelineStage::None);
697 FacePipelinePackageBase* package = nullptr;
698
699 /* =========================================================================================
700 * Pipeline stage specific initialization code
701 *
702 * Use the block from here to MLPIPELINE_LOOP_START to initialize the stage.
703 * The code in this block is run once per stage initialization. The number of instances
704 * is at least 1. More instances are created by addMoreWorkers if needed.
705 */
706
707 const IdentityProvider* const idProvider = IdentityProvider::instance();
708 FaceUtils utils;
709 ThumbnailLoadThread* const thumbnailLoadThread = ThumbnailLoadThread::defaultThread();
710 const QList<AlbumRootInfo> roots = CoreDbAccess().db()->getAlbumRoots();
711 Q_UNUSED(roots);
712
713 MLPIPELINE_LOOP_START(MLPipelineStage::Writer, thisQueue);
714 package = static_cast<FacePipelinePackageBase*>(mlpackage);
715
716 /* =========================================================================================
717 * Start pipeline stage specific loop
718 *
719 * All code from here to MLPIPELINE_LOOP_END is in a try/catch block and loop.
720 * This loop is run once per image.
721 */
722
723 {
724 switch (settings.alreadyScannedHandling)
725 {
726 case FaceScanSettings::Rescan:
727 {
728 // remove old unconfirmed face rects
729
730 QList<FaceTagsIface> oldEntries = utils.unconfirmedFaceTagsIfaces(package->info.id());
731 utils.removeFaces(oldEntries);
732 break;
733 }
734
735 case FaceScanSettings::ClearAll:
736 {
737 // remove all face rects
738
739 utils.removeAllFaces(package->info.id());
740 break;
741 }
742
743 case FaceScanSettings::RecognizeOnly:
744 case FaceScanSettings::Skip:
745 {
746 // do nothing
747 // Skipped images were skipped in the finder stage
748 // RecognizeOnly defines the pipeline
749
750 break;
751 }
752 }
753
754 // mark the image as scanned
755
756 utils.markAsScanned(package->info);
757
758 // create thumbnails and write the new face rects to the database
759
760 QStringList names;
761
762 if (package->faceRects.size())
763 {
764 QList<FaceTagsIface> databaseFaces;
765 QList<Identity> identities;
766 QList<QRectF> paddedRectList;
767
768 for (int i = 0 ; i < package->faceRects.size() ; ++i)
769 {
770 // pad the rect by FaceTagsIface::faceThumbnailResizeFactor (1/2 on each side)
771
772 QRectF paddedRect = QRectF(qMax(0.0, package->faceRects[i].x() - (package->faceRects[i].width() * (FaceTagsIface::faceThumbnailResizeFactor / 2.0F))),
773 qMax(0.0, package->faceRects[i].y() - (package->faceRects[i].height() * (FaceTagsIface::faceThumbnailResizeFactor / 2.0F))),
774 qMin(1.0 ,package->faceRects[i].width() + (package->faceRects[i].width() * FaceTagsIface::faceThumbnailResizeFactor)),
775 qMin(1.0 ,package->faceRects[i].height() + (package->faceRects[i].height() * FaceTagsIface::faceThumbnailResizeFactor)));
776
777 paddedRectList << paddedRect;
778
779 QRect faceRect(std::round(package->image.width() * paddedRect.x()),
780 std::round(package->image.height() * paddedRect.y()),
781 std::round(package->image.width() * paddedRect.width()),
782 std::round(package->image.height() * paddedRect.height()));
783
784 if (package->labelList[i] != FaceClassifier::UNKNOWN_LABEL_ID)
785 {
786 Identity identity = idProvider->identity(package->labelList[i]);
787 names << identity.attribute(QStringLiteral("name"));
788 identities << identity;
789 databaseFaces << FaceTagsIface(FaceTagsIface::Type::UnconfirmedName,
790 package->info.id(),
791 FaceTags::unconfirmedPersonTagId(),
792 TagRegion(faceRect));
793 }
794 else
795 {
796 identities << Identity();
797 databaseFaces << FaceTagsIface(FaceTagsIface::Type::UnknownName,
798 package->info.id(),
799 FaceTags::unknownPersonTagId(),
800 TagRegion(faceRect));
801 }
802 }
803
804 // store the thumbnails
805
806 if (!package->image.isNull())
807 {
808 utils.storeThumbnails(thumbnailLoadThread, package->info.filePath(),
809 databaseFaces, package->image);
810 }
811
812 // write the new face rects to the database
813
814 utils.writeUnconfirmedResults(package->info.id(),
815 paddedRectList,
816 identities,
817 package->image.originalSize());
818 }
819
820 QString albumName = CollectionManager::instance()->albumRootLabel(package->info.albumRootId());
821
822 // send a notification that the image was processed
823
824 notify(MLPipelineNotification::notifyProcessed,
825 package->info.name(),
826 albumName + package->info.relativePath(),
827 names.join(QLatin1String(", ")),
828 package->faceRects.size(),
829 package->thumbnailIcon);
830
831 // delete the package
832
833 delete package;
834
835 package = nullptr;
836 }
837
838 /* =========================================================================================
839 * End pipeline stage specific loop
840 */
841
842 MLPIPELINE_LOOP_END(MLPipelineStage::Writer, "FacePipelineDetectRecognize::writer");
843
844 /* =========================================================================================
845 * Pipeline stage specific cleanup
846 *
847 * Use the block from here to MLPIPELINE_STAGE_END to clean up any resources used by the stage.
848 */
849
850 MLPIPELINE_STAGE_END(MLPipelineStage::Writer, MLPipelineStage::None);
851 }
852
853 void FacePipelineDetectRecognize::addMoreWorkers()
854 {
855 /* =========================================================================================
856 * Use the performanceProfile metrics to find the slowest stages
857 * and add more workers to those stages.
858 *
859 * For the Face detection and recognition pipeline, the loader is the
860 * slowest stage so add 3 more loaders, 2 more extractors, and 1 more classifier.
861 */
862
863 addWorker(Loader);
864 addWorker(Loader);
865 addWorker(Loader);
866 addWorker(Extractor);
867 addWorker(Extractor);
868 addWorker(Classifier);
869 }
870
871 } // namespace Digikam
872
873 #include "moc_facepipelinedetectrecognize.cpp"