25 #ifndef OPENCV_DNN_FACERECOGNIZER_P_H
26 #define OPENCV_DNN_FACERECOGNIZER_P_H
36 #include <QElapsedTimer>
60 for (
int i = 0 ; i < 1 ; ++i)
69 svm = cv::ml::SVM::create();
70 svm->setKernel(cv::ml::SVM::LINEAR);
76 knn = cv::ml::KNearest::create();
77 knn->setAlgorithmType(cv::ml::KNearest::BRUTE_FORCE);
78 knn->setIsClassifier(
true);
95 qFatal(
"Invalid classifier");
102 QVector<DNNFaceExtractor*>::iterator extractor = extractors.begin();
104 while (extractor != extractors.end())
107 extractor = extractors.erase(extractor);
118 int predictSVM(
const cv::Mat& faceEmbedding);
119 int predictKNN(
const cv::Mat& faceEmbedding);
121 int predictKDTree(
const cv::Mat& faceEmbedding)
const;
122 int predictDb(
const cv::Mat& faceEmbedding)
const;
124 bool insertData(
const cv::Mat&
position,
const int label,
const QString& context = QString());
132 cv::Ptr<cv::ml::KNearest>
knn;
157 ids.resize(images.size());
162 for(
int i = range.start ; i < range.end ; ++i)
196 qCWarning(DIGIKAM_FACEDB_LOG) <<
"Not recognized classifying method";
223 const QString& context)
233 for(
int i = range.start ; i < range.end ; ++i)
238 if (!d->
insertData(faceEmbedding,
id, context))
240 qCWarning(DIGIKAM_FACEDB_LOG) <<
"Fail to register a face of identity" << id;
249 const QString& context;
265 qCDebug(DIGIKAM_FACEDB_LOG) <<
"Support vector machine trains in" << timer.elapsed() <<
"ms";
267 return (
svm->isTrained());
277 qCDebug(DIGIKAM_FACEDB_LOG) <<
"KNN trains in" << timer.elapsed() <<
"ms";
279 return (knn->isTrained());
291 newDataAdded =
false;
294 return (
int(svm->predict(faceEmbedding)));
306 newDataAdded =
false;
310 knn->findNearest(faceEmbedding, kNeighbors, output);
312 return (
int(output.at<
float>(0)));
324 QMap<double, QVector<int> > closestNeighbors = tree->getClosestNeighbors(faceEmbedding, threshold, 0.8, kNeighbors);
326 QMap<int, QVector<double> > votingGroups;
328 for (QMap<
double, QVector<int> >::const_iterator iter = closestNeighbors.cbegin();
329 iter != closestNeighbors.cend();
332 for (QVector<int>::const_iterator node = iter.value().cbegin();
333 node != iter.value().cend();
338 votingGroups[label].append(iter.key());
342 double maxScore = 0.0;
345 for (QMap<
int, QVector<double> >::const_iterator group = votingGroups.cbegin();
346 group != votingGroups.cend();
351 for (
int i = 0 ; i < group.value().size() ; ++i)
353 score += (threshold - group.value()[i]);
356 if (score > maxScore)
359 prediction = group.key();
370 QMap<int, QVector<double> > votingGroups;
372 for (QMap<
double, QVector<int> >::const_iterator iter = closestNeighbors.cbegin();
373 iter != closestNeighbors.cend();
376 for (
int i = 0 ; i < iter.value().size() ; ++i)
378 votingGroups[iter.value()[i]].append(iter.key());
382 double maxScore = 0.0;
385 for (QMap<
int, QVector<double> >::const_iterator group = votingGroups.cbegin();
386 group != votingGroups.cend();
391 for (
int i = 0 ; i < group.value().size() ; ++i)
393 score += (threshold - group.value()[i]);
396 if (score > maxScore)
399 prediction = group.key();
412 qCWarning(DIGIKAM_FACEDB_LOG) <<
"error inserting face embedding to database";
417 if (!
FaceDbAccess().db()->insertToTreeDb(nodeId, nodePos))
419 qCWarning(DIGIKAM_FACEDB_LOG) <<
"Error insert face embedding";
424 else if (method ==
Tree)
426 KDNode*
const newNode = tree->add(nodePos, label);
434 qCWarning(DIGIKAM_FACEDB_LOG) <<
"Error insert new node" << nodeId;
Definition: facedbaccess.h:43
FaceDb * db() const
Definition: facedbaccess.cpp:141
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
KDTree * reconstructTree() const
reconstructTree: reconstruct KD-Tree from data in the database
Definition: facedb_dnn.cpp:62
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
void setNodeId(int id)
Definition: kd_node.cpp:170
Definition: opencvdnnfacerecognizer_p.h:147
void operator()(const cv::Range &range) const override
Definition: opencvdnnfacerecognizer_p.h:160
ParallelRecognizer(OpenCVDNNFaceRecognizer::Private *d, const QList< QImage * > &images, QVector< int > &ids)
Definition: opencvdnnfacerecognizer_p.h:150
Definition: opencvdnnfacerecognizer_p.h:217
ParallelTrainer(OpenCVDNNFaceRecognizer::Private *d, const QList< QImage * > &images, const int &id, const QString &context)
Definition: opencvdnnfacerecognizer_p.h:220
void operator()(const cv::Range &range) const override
Definition: opencvdnnfacerecognizer_p.h:231
Definition: opencvdnnfacerecognizer_p.h:50
int predictKDTree(const cv::Mat &faceEmbedding) const
Definition: opencvdnnfacerecognizer_p.h:315
Classifier method
Definition: opencvdnnfacerecognizer_p.h:128
QVector< DNNFaceExtractor * > extractors
Definition: opencvdnnfacerecognizer_p.h:130
KDTree * tree
Definition: opencvdnnfacerecognizer_p.h:134
bool newDataAdded
Definition: opencvdnnfacerecognizer_p.h:138
~Private()
Definition: opencvdnnfacerecognizer_p.h:100
cv::Ptr< cv::ml::SVM > svm
Definition: opencvdnnfacerecognizer_p.h:131
int predictDb(const cv::Mat &faceEmbedding) const
Definition: opencvdnnfacerecognizer_p.h:366
cv::Ptr< cv::ml::KNearest > knn
Definition: opencvdnnfacerecognizer_p.h:132
int predictKNN(const cv::Mat &faceEmbedding)
Definition: opencvdnnfacerecognizer_p.h:297
Private(Classifier method)
Definition: opencvdnnfacerecognizer_p.h:53
int predictSVM(const cv::Mat &faceEmbedding)
Definition: opencvdnnfacerecognizer_p.h:282
bool insertData(const cv::Mat &position, const int label, const QString &context=QString())
Definition: opencvdnnfacerecognizer_p.h:406
int kNeighbors
Definition: opencvdnnfacerecognizer_p.h:135
bool trainSVM()
Definition: opencvdnnfacerecognizer_p.h:258
bool trainKNN()
Definition: opencvdnnfacerecognizer_p.h:270
float threshold
Definition: opencvdnnfacerecognizer_p.h:136
Definition: opencvdnnfacerecognizer.h:42
static cv::Mat prepareForRecognition(QImage &inputImage)
Definition: opencvdnnfacerecognizer.cpp:50
Classifier
Definition: opencvdnnfacerecognizer.h:46
@ OpenCV_KNN
Definition: opencvdnnfacerecognizer.h:48
@ DB
Definition: opencvdnnfacerecognizer.h:50
@ Tree
Definition: opencvdnnfacerecognizer.h:49
@ SVM
Definition: opencvdnnfacerecognizer.h:47
Definition: datefolderview.cpp:43