1 /* ============================================================ 2 * 3 * This file is a part of digiKam 4 * 5 * Date : 2019-06-01 6 * Description : Face recognition using deep learning 7 * The internal DNN library interface 8 * 9 * Copyright (C) 2019 by Thanh Trung Dinh <dinhthanhtrung1996 at gmail dot com> 10 * Copyright (C) 2020-2021 by Gilles Caulier <caulier dot gilles at gmail dot com> 11 * Copyright (C) 2020 by Nghia Duong <minhnghiaduong997 at gmail dot com> 12 * 13 * This program is free software; you can redistribute it 14 * and/or modify it under the terms of the GNU General 15 * Public License as published by the Free Software Foundation; 16 * either version 2, or (at your option) 17 * any later version. 18 * 19 * This program is distributed in the hope that it will be useful, 20 * but WITHOUT ANY WARRANTY; without even the implied warranty of 21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 22 * GNU General Public License for more details. 23 * 24 * ============================================================ */ 25 26 #ifndef DNN_FACE_EXTRACTOR_H 27 #define DNN_FACE_EXTRACTOR_H 28 29 // C++ includes 30 31 #include <vector> 32 33 // Qt include 34 35 #include <QJsonArray> 36 37 // Local includes 38 39 #include "digikam_opencv.h" 40 #include "digikam_export.h" 41 42 namespace Digikam 43 { 44 45 // TODO: remove export macro after testing 46 47 class DIGIKAM_GUI_EXPORT DNNFaceExtractor 48 { 49 50 public: 51 52 explicit DNNFaceExtractor(); 53 DNNFaceExtractor(const DNNFaceExtractor&); 54 ~DNNFaceExtractor(); 55 56 public: 57 58 /** 59 * Read pretrained neural network for face recognition. 60 */ 61 bool loadModels(); 62 63 64 cv::Mat alignFace(const cv::Mat& inputImage) const; 65 cv::Mat getFaceEmbedding(const cv::Mat& faceImage); 66 67 /** 68 * Calculate different between 2 vectors 69 */ 70 static double cosineDistance(std::vector<float> v1, std::vector<float> v2); 71 static double L2squareDistance(std::vector<float> v1, std::vector<float> v2); 72 static double L2squareNormDistance(std::vector<float> v1, std::vector<float> v2); 73 74 /** 75 * Convert face embedding between different formats 76 */ 77 static cv::Mat vectortomat(const std::vector<float>& vector); 78 static QJsonArray encodeVector(const std::vector<float>& vector); 79 static std::vector<float> decodeVector(const QJsonArray& json); 80 81 private: 82 83 /// Disable 84 DNNFaceExtractor& operator=(const DNNFaceExtractor&) = delete; 85 86 private: 87 88 class Private; 89 Private* d; 90 }; 91 92 } // namespace Digikam 93 94 #endif // DNN_FACE_EXTRACTOR_H 95