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

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam
0004  *
0005  * Date        : 2020-05-22
0006  * Description : Wrapper of face recognition using OpenFace
0007  *
0008  * SPDX-FileCopyrightText: 2019      by Thanh Trung Dinh <dinhthanhtrung1996 at gmail dot com>
0009  * SPDX-FileCopyrightText: 2020-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0010  * SPDX-FileCopyrightText: 2020      by Nghia Duong <minhnghiaduong997 at gmail dot com>
0011  *
0012  * SPDX-License-Identifier: GPL-2.0-or-later
0013  *
0014  * ============================================================ */
0015 
0016 #ifndef OPENCV_DNN_FACERECOGNIZER_H
0017 #define OPENCV_DNN_FACERECOGNIZER_H
0018 
0019 // Qt includes
0020 
0021 #include <QImage>
0022 
0023 // Local includes
0024 
0025 #include "digikam_opencv.h"
0026 #include "digikam_export.h"
0027 #include "identity.h"
0028 
0029 namespace Digikam
0030 {
0031 
0032 class DIGIKAM_GUI_EXPORT OpenCVDNNFaceRecognizer
0033 {
0034 public:
0035 
0036     enum Classifier
0037     {
0038         SVM = 0,
0039         OpenCV_KNN,
0040         Tree,
0041         DB,
0042     };
0043 
0044     /**
0045      *  @brief OpenCVDNNFaceRecognizer:Master class to control entire recognition using OpenFace algorithm
0046      */
0047     explicit OpenCVDNNFaceRecognizer(Classifier method = Tree);
0048     ~OpenCVDNNFaceRecognizer();
0049 
0050 public:
0051 
0052     /**
0053      *  Returns a cvMat created from the inputImage, optimized for recognition
0054      */
0055     static cv::Mat prepareForRecognition(QImage& inputImage);
0056 
0057     /**
0058      *  Returns a cvMat created from the cvinputImage, optimized for recognition
0059      */
0060     static cv::Mat prepareForRecognition(const cv::Mat& cvinputImage);
0061 
0062     /**
0063      * Register faces corresponding to an identity
0064      */
0065     void train(const QList<QImage*>& images, const int label, const QString& context);
0066 
0067     /**
0068      * Try to recognize the given image.
0069      * Returns the identity id.
0070      * If the identity cannot be recognized, returns -1.
0071      * TODO: verify workflow to economize this routine
0072      */
0073     int recognize(QImage* inputImage);
0074 
0075     /**
0076      * Try to recognize a list of given images.
0077      * Returns a list of identity ids.
0078      * If an identity cannot be recognized, returns -1.
0079      */
0080     QVector<int> recognize(const QList<QImage*>& inputImages);
0081 
0082     /**
0083      * clear specified trained data
0084      */
0085     void clearTraining(const QList<int>& idsToClear, const QString& trainingContext);
0086 
0087     /**
0088      * set K parameter of K-Nearest neighbors algorithm
0089      */
0090     void setNbNeighBors(int k);
0091 
0092     /**
0093      * set maximum square distance of 2 vector
0094      */
0095     void setThreshold(float threshold);
0096 
0097     /**
0098      * @brief register training data for unit test
0099      */
0100     bool registerTrainingData(const cv::Mat& preprocessedImage, int label);
0101 
0102     /**
0103      * @brief predict label of test data for unit test
0104      */
0105     int verifyTestData(const cv::Mat& preprocessedImage);
0106 
0107 private:
0108 
0109     // Disable
0110     OpenCVDNNFaceRecognizer(const OpenCVDNNFaceRecognizer&)            = delete;
0111     OpenCVDNNFaceRecognizer& operator=(const OpenCVDNNFaceRecognizer&) = delete;
0112 
0113 private:
0114 
0115     class Private;
0116     Private* d;
0117 };
0118 
0119 } // namespace Digikam
0120 
0121 #endif // OPENCV_DNN_FACERECOGNIZER_H