File indexing completed on 2025-03-09 03:55:01

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam
0004  *
0005  * Date        : 2010-06-16
0006  * Description : Recognizing functions of recognition wrapper
0007  *
0008  * SPDX-FileCopyrightText:      2010 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
0009  * SPDX-FileCopyrightText:      2010 by Aditya Bhatt <adityabhatt1991 at gmail dot com>
0010  * SPDX-FileCopyrightText: 2010-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0011  * SPDX-FileCopyrightText:      2019 by Thanh Trung Dinh <dinhthanhtrung1996 at gmail dot com>
0012  * SPDX-FileCopyrightText:      2020 by Nghia Duong <minhnghiaduong997 at gmail dot com>
0013  *
0014  * SPDX-License-Identifier: GPL-2.0-or-later
0015  *
0016  * ============================================================ */
0017 
0018 #include "facialrecognition_wrapper_p.h"
0019 
0020 namespace Digikam
0021 {
0022 
0023 QList<Identity> FacialRecognitionWrapper::recognizeFaces(ImageListProvider* const images)
0024 {
0025     if (!d || !d->dbAvailable)
0026     {
0027         return QList<Identity>();
0028     }
0029 
0030     QMutexLocker lock(&d->mutex);
0031 
0032     QVector<int> ids;
0033 
0034     try
0035     {
0036         ids = d->recognizer->recognize(images->images());
0037     }
0038     catch (cv::Exception& e)
0039     {
0040         qCCritical(DIGIKAM_FACESENGINE_LOG) << "cv::Exception:" << e.what();
0041     }
0042     catch (...)
0043     {
0044         qCCritical(DIGIKAM_FACESENGINE_LOG) << "Default exception from OpenCV";
0045     }
0046 
0047     QList<Identity> results;
0048 
0049     for (int i = 0 ; i < ids.size() ; ++i)
0050     {
0051         results << d->identityCache.value(ids.at(i));
0052     }
0053 
0054     return results;
0055 }
0056 
0057 QList<Identity> FacialRecognitionWrapper::recognizeFaces(const QList<QImage*>& images)
0058 {
0059     QListImageListProvider provider;
0060     provider.setImages(images);
0061 
0062     return recognizeFaces(&provider);
0063 }
0064 
0065 Identity FacialRecognitionWrapper::recognizeFace(QImage* const image)
0066 {
0067     QList<Identity> result = recognizeFaces(QList<QImage*>() << image);
0068 
0069     if (result.isEmpty())
0070     {
0071         return Identity();
0072     }
0073 
0074     return result.first();
0075 }
0076 
0077 } // namespace Digikam