summaryrefslogtreecommitdiff
blob: bc242b571a8176efc534f968716671f94081d11e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
diff --git a/libkface/recognition-opencv-lbph/facerec_borrowed.h b/libkface/recognition-opencv-lbph/facerec_borrowed.h
index 27ad77a..f197d22 100644
--- a/libkface/recognition-opencv-lbph/facerec_borrowed.h
+++ b/libkface/recognition-opencv-lbph/facerec_borrowed.h
@@ -125,6 +125,8 @@ public:
      */
     void update(cv::InputArrayOfArrays src, cv::InputArray labels);
 
+
+#if OPENCV_TEST_VERSION(3,1,0)
     /**
      * Predicts the label of a query image in src.
      */
@@ -134,6 +136,13 @@ public:
      * Predicts the label and confidence for a given sample.
      */
     void predict(cv::InputArray _src, int &label, double &dist) const;
+#else
+    using cv::face::FaceRecognizer::predict;
+    /*
+     * Predict
+     */
+    void predict(cv::InputArray src, cv::Ptr<cv::face::PredictCollector> collector, const int state = 0) const override;
+#endif
 
     /**
      * See FaceRecognizer::load().
diff --git a/libkface/recognition-opencv-lbph/facerec_borrowed.cpp b/libkface/recognition-opencv-lbph/facerec_borrowed.cpp
index 748691e..3c37ce2 100644
--- a/libkface/recognition-opencv-lbph/facerec_borrowed.cpp
+++ b/libkface/recognition-opencv-lbph/facerec_borrowed.cpp
@@ -36,6 +36,8 @@
  *
  * ============================================================ */
 
+#define QT_NO_EMIT
+
 #include "facerec_borrowed.h"
 
 // C++ includes
@@ -375,7 +377,11 @@ void LBPHFaceRecognizer::train(InputArrayOfArrays _in_src, InputArray _inm_label
     }
 }
 
+#if OPENCV_TEST_VERSION(3,1,0)
 void LBPHFaceRecognizer::predict(InputArray _src, int &minClass, double &minDist) const
+#else
+void LBPHFaceRecognizer::predict(cv::InputArray _src, cv::Ptr<cv::face::PredictCollector> collector, const int state) const
+#endif
 {
     if(m_histograms.empty())
     {
@@ -394,8 +400,12 @@ void LBPHFaceRecognizer::predict(InputArray _src, int &minClass, double &minDist
                                       m_grid_y,                                                          /* grid size y                 */
                                       true                                                               /* normed histograms           */
                                      );
+#if OPENCV_TEST_VERSION(3,1,0)
     minDist      = DBL_MAX;
     minClass     = -1;
+#else
+    collector->init((int)m_histograms.size(), state);
+#endif
 
     // This is the standard method
 
@@ -406,11 +416,19 @@ void LBPHFaceRecognizer::predict(InputArray _src, int &minClass, double &minDist
         {
             double dist = compareHist(m_histograms[sampleIdx], query, CV_COMP_CHISQR);
 
+#if OPENCV_TEST_VERSION(3,1,0)
             if((dist < minDist) && (dist < m_threshold))
             {
                 minDist  = dist;
                 minClass = m_labels.at<int>((int) sampleIdx);
             }
+#else
+            int label = m_labels.at<int>((int) sampleIdx);
+            if (!collector->emit(label, dist, state))
+            {
+                return;
+            }
+#endif
         }
     }
 
@@ -422,7 +440,7 @@ void LBPHFaceRecognizer::predict(InputArray _src, int &minClass, double &minDist
         // Create map "label -> vector of distances to all histograms for this label"
         std::map<int, std::vector<int> > distancesMap;
 
-        for(size_t sampleIdx = 0; sampleIdx < m_histograms.size(); sampleIdx++) 
+        for(size_t sampleIdx = 0; sampleIdx < m_histograms.size(); sampleIdx++)
         {
             double dist                 = compareHist(m_histograms[sampleIdx], query, CV_COMP_CHISQR);
             std::vector<int>& distances = distancesMap[m_labels.at<int>((int) sampleIdx)];
@@ -445,11 +463,18 @@ void LBPHFaceRecognizer::predict(InputArray _src, int &minClass, double &minDist
             double mean = sum / it->second.size();
             s          += QString::fromLatin1("%1: %2 - ").arg(it->first).arg(mean);
 
+#if OPENCV_TEST_VERSION(3,1,0)
             if((mean < minDist) && (mean < m_threshold))
             {
                 minDist = mean;
                 minClass = it->first;
             }
+#else
+            if (!collector->emit(it->first, mean, state))
+            {
+                return;
+            }
+#endif
         }
 
         qCDebug(LIBKFACE_LOG) << s;
@@ -462,7 +487,7 @@ void LBPHFaceRecognizer::predict(InputArray _src, int &minClass, double &minDist
         // map "label -> number of histograms"
         std::map<int, int> countMap;
 
-        for(size_t sampleIdx = 0; sampleIdx < m_histograms.size(); sampleIdx++) 
+        for(size_t sampleIdx = 0; sampleIdx < m_histograms.size(); sampleIdx++)
         {
             int label   = m_labels.at<int>((int) sampleIdx);
             double dist = compareHist(m_histograms[sampleIdx], query, CV_COMP_CHISQR);
@@ -480,7 +505,9 @@ void LBPHFaceRecognizer::predict(InputArray _src, int &minClass, double &minDist
             scoreMap[it->second]++;
         }
 
+#if OPENCV_TEST_VERSION(3,1,0)
         minDist = 0;
+#endif
         QString s("Nearest Neighbor score: ");
 
         for (std::map<int,int>::iterator it = scoreMap.begin(); it != scoreMap.end(); ++it)
@@ -488,17 +515,26 @@ void LBPHFaceRecognizer::predict(InputArray _src, int &minClass, double &minDist
             double score = double(it->second) / countMap.at(it->first);
             s           += QString("%1/%2 %3  ").arg(it->second).arg(countMap.at(it->first)).arg(score);
 
+#if OPENCV_TEST_VERSION(3,1,0)
             if (score > minDist)
             {
                 minDist  = score;
                 minClass = it->first;
             }
+#else
+            // large is better thus it is -score.
+            if (!collector->emit(it->first, -score, state))
+            {
+                return;
+            }
+#endif
         }
 
         kDebug() << s;
     }
 }
 
+#if OPENCV_TEST_VERSION(3,1,0)
 int LBPHFaceRecognizer::predict(InputArray _src) const
 {
     int    label;
@@ -506,6 +542,7 @@ int LBPHFaceRecognizer::predict(InputArray _src) const
     predict(_src, label, dummy);
     return label;
 }
+#endif
 
 // Static method ----------------------------------------------------