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

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam
0004  *
0005  * Date        : 2019-06-01
0006  * Description : Face recognition using deep learning
0007  *               The internal DNN library interface
0008  *
0009  * SPDX-FileCopyrightText: 2019      by Thanh Trung Dinh <dinhthanhtrung1996 at gmail dot com>
0010  * SPDX-FileCopyrightText: 2020-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0011  * SPDX-FileCopyrightText: 2020      by Nghia Duong <minhnghiaduong997 at gmail dot com>
0012  *
0013  * SPDX-License-Identifier: GPL-2.0-or-later
0014  *
0015  * ============================================================ */
0016 
0017 #ifndef DNN_FACE_EXTRACTOR_H
0018 #define DNN_FACE_EXTRACTOR_H
0019 
0020 // C++ includes
0021 
0022 #include <vector>
0023 
0024 // Qt include
0025 
0026 #include <QJsonArray>
0027 
0028 // Local includes
0029 
0030 #include "digikam_opencv.h"
0031 #include "digikam_export.h"
0032 
0033 namespace Digikam
0034 {
0035 
0036 // TODO: remove export macro after testing
0037 
0038 class DIGIKAM_GUI_EXPORT DNNFaceExtractor
0039 {
0040 
0041 public:
0042 
0043     explicit DNNFaceExtractor();
0044     DNNFaceExtractor(const DNNFaceExtractor&);
0045     ~DNNFaceExtractor();
0046 
0047 public:
0048 
0049     /**
0050      * Read pretrained neural network for face recognition.
0051      */
0052     bool loadModels();
0053 
0054 
0055     cv::Mat alignFace(const cv::Mat& inputImage) const;
0056     cv::Mat getFaceEmbedding(const cv::Mat& faceImage);
0057 
0058     /**
0059      * Calculate different between 2 vectors
0060      */
0061     static double cosineDistance(std::vector<float> v1, std::vector<float> v2);
0062     static double L2squareDistance(std::vector<float> v1, std::vector<float> v2);
0063     static double L2squareNormDistance(std::vector<float> v1, std::vector<float> v2);
0064 
0065     /**
0066      * Convert face embedding between different formats
0067      */
0068     static cv::Mat            vectortomat(const std::vector<float>& vector);
0069     static QJsonArray         encodeVector(const std::vector<float>& vector);
0070     static std::vector<float> decodeVector(const QJsonArray& json);
0071 
0072 private:
0073 
0074     /// Disable
0075     DNNFaceExtractor& operator=(const DNNFaceExtractor&) = delete;
0076 
0077 private:
0078 
0079     class Private;
0080     Private* d;
0081 };
0082 
0083 } // namespace Digikam
0084 
0085 #endif // DNN_FACE_EXTRACTOR_H