digiKam
facedb.h
Go to the documentation of this file.
1 /* ============================================================
2  *
3  * This file is a part of digiKam
4  *
5  * Date : 02-02-2012
6  * Description : Face database interface to train identities.
7  *
8  * Copyright (C) 2012-2013 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
9  * Copyright (C) 2010-2022 by Gilles Caulier <caulier dot gilles at gmail dot com>
10  * Copyright (C) 2020 by Nghia Duong <minhnghiaduong997 at gmail dot com>
11  *
12  * This program is free software; you can redistribute it
13  * and/or modify it under the terms of the GNU General
14  * Public License as published by the Free Software Foundation;
15  * either version 2, or (at your option)
16  * any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * ============================================================ */
24 
25 #ifndef DIGIKAM_FACE_DB_H
26 #define DIGIKAM_FACE_DB_H
27 
28 // C++ includes
29 
30 #include <vector>
31 
32 // Qt includes
33 
34 #include <QString>
35 #include <QFile>
36 #include <QDataStream>
37 #include <QStandardPaths>
38 
39 // Local includes
40 
41 #include "digikam_config.h"
42 #include "digikam_opencv.h"
43 #include "identity.h"
44 #include "facedbbackend.h"
45 
46 namespace Digikam
47 {
48 
49 class KDTree;
50 
51 class FaceDb
52 {
53 public:
54 
55  explicit FaceDb(FaceDbBackend* const db);
56  ~FaceDb();
57 
58  BdEngineBackend::QueryState setSetting(const QString& keyword, const QString& value);
59  QString setting(const QString& keyword) const;
60 
64  bool integrityCheck();
65 
69  void vacuum();
70 
71 public:
72 
73  // --- Identity management (facedb_identity.cpp)
74 
75  int addIdentity() const;
76  int getNumberOfIdentities() const;
77 
78  void updateIdentity(const Identity& p);
79  void deleteIdentity(int id);
80  void deleteIdentity(const QString& uuid);
81  void clearIdentities();
82 
84  QList<int> identityIds() const;
85 
86 public:
87 
88  // --- OpenCV DNN
89 
96  int insertFaceVector(const cv::Mat& faceEmbedding,
97  const int label,
98  const QString& context) const;
99 
104  KDTree* reconstructTree() const;
105 
110  cv::Ptr<cv::ml::TrainData> trainData() const;
111 
119  bool insertToTreeDb(const int nodeID,
120  const cv::Mat& faceEmbedding) const;
121 
133  QMap<double, QVector<int> > getClosestNeighborsTreeDb(const cv::Mat& position,
134  float sqRange,
135  float cosThreshold,
136  int maxNbNeighbors) const;
137 
138  void clearTreeDb() const;
139 
144  void clearDNNTraining(const QString& context = QString());
145  void clearDNNTraining(const QList<int>& identities, const QString& context = QString());
146 
147 private:
148 
149  void updateRangeTreeDb(int nodeId,
150  cv::Mat& minRange,
151  cv::Mat& maxRange,
152  const cv::Mat& position) const;
153  int findParentTreeDb(const cv::Mat& nodePos,
154  bool& leftChild,
155  int& parentSplitAxis) const;
156 
157  class DataNode;
158 
170  double getClosestNeighborsTreeDb(const DataNode& subTree,
171  QMap<double, QVector<int> >& neighborList,
172  const cv::Mat& position,
173  float sqRange,
174  float cosThreshold,
175  int maxNbNeighbors) const;
176 
177 private:
178 
179  // Disable
180  FaceDb(const FaceDb&) = delete;
181  FaceDb& operator=(const FaceDb&) = delete;
182 
183 private:
184 
185  class Private;
186  Private* const d;
187 };
188 
189 } // namespace Digikam
190 
191 #endif // DIGIKAM_FACE_DB_H
Definition: dbenginebackend.h:157
Definition: facedbbackend.h:40
Definition: facedb_p.h:44
Definition: facedb.h:52
void vacuum()
Definition: facedb.cpp:134
void clearDNNTraining(const QString &context=QString())
clearDNNTraining : clear all trained data in the database
Definition: facedb_dnn.cpp:102
bool insertToTreeDb(const int nodeID, const cv::Mat &faceEmbedding) const
insertToTreeDb : insert a new node to spatial database
Definition: facedb_dnn_spatial.cpp:61
QMap< double, QVector< int > > getClosestNeighborsTreeDb(const cv::Mat &position, float sqRange, float cosThreshold, int maxNbNeighbors) const
getClosestNeighbors : return a list of closest neighbor, limited by maxNbNeighbors and sqRange
Definition: facedb_dnn_spatial.cpp:116
void deleteIdentity(int id)
Definition: facedb_identity.cpp:54
FaceDb(FaceDbBackend *const db)
Definition: facedb.cpp:30
void updateIdentity(const Identity &p)
Definition: facedb_identity.cpp:39
KDTree * reconstructTree() const
reconstructTree: reconstruct KD-Tree from data in the database
Definition: facedb_dnn.cpp:62
int getNumberOfIdentities() const
Definition: facedb_identity.cpp:133
BdEngineBackend::QueryState setSetting(const QString &keyword, const QString &value)
Definition: facedb.cpp:41
void clearIdentities()
Definition: facedb_identity.cpp:81
void clearTreeDb() const
Definition: facedb_dnn_spatial.cpp:156
int insertFaceVector(const cv::Mat &faceEmbedding, const int label, const QString &context) const
insertFaceVector : insert a new face embedding to database
Definition: facedb_dnn.cpp:31
QString setting(const QString &keyword) const
Definition: facedb.cpp:50
~FaceDb()
Definition: facedb.cpp:36
cv::Ptr< cv::ml::TrainData > trainData() const
trainData: extract train data from database
Definition: facedb_dnn.cpp:87
int addIdentity() const
Definition: facedb_identity.cpp:30
bool integrityCheck()
Definition: facedb.cpp:71
QList< Identity > identities() const
Definition: facedb_identity.cpp:86
QList< int > identityIds() const
Definition: facedb_identity.cpp:117
Definition: identity.h:41
Definition: kd_tree.h:34
Definition: piwigotalker.h:48
qulonglong value
Definition: itemviewutilities.cpp:592
Definition: datefolderview.cpp:43
Definition: scan.h:26