1- #include " FaceRecognizer.h"
21#include " OpenCV.h"
32
4- #if CV_MAJOR_VERSION >= 3
5- #warning TODO: port me to OpenCV 3
6- #endif
7-
8- #if ((CV_MAJOR_VERSION == 2) && (CV_MINOR_VERSION >=4) && (CV_SUBMINOR_VERSION>=4))
9-
3+ #ifdef HAVE_OPENCV_FACE
4+ #include " FaceRecognizer.h"
105#include " Matrix.h"
116#include < nan.h>
127
8+ #if CV_MAJOR_VERSION >= 3
9+ namespace cv {
10+ using std::vector;
11+ using cv::face::createEigenFaceRecognizer;
12+ using cv::face::createFisherFaceRecognizer;
13+ using cv::face::createLBPHFaceRecognizer;
14+ }
15+ #endif
16+
1317#define EIGEN 0
1418#define LBPH 1
1519#define FISHER 2
@@ -278,6 +282,17 @@ NAN_METHOD(FaceRecognizerWrap::PredictSync) {
278282 double confidence = 0.0 ;
279283 self->rec ->predict (im, predictedLabel, confidence);
280284
285+ #if CV_MAJOR_VERSION >= 3
286+ // Older versions of OpenCV3 incorrectly returned label=0 at
287+ // confidence=DBL_MAX instead of label=-1 on failure. This can be removed
288+ // once the fix* becomes more widespread.
289+ //
290+ // * https://github.com/Itseez/opencv_contrib/commit/0aa58ae9b30a017b356a86d29453c0b56ed9e625#diff-d9c561bf45c255c5951ff1ab55e80473
291+ if (predictedLabel == 0 && confidence == DBL_MAX) {
292+ predictedLabel = -1 ;
293+ }
294+ #endif
295+
281296 v8::Local<v8::Object> res = Nan::New<Object>();
282297 res->Set (Nan::New (" id" ).ToLocalChecked (), Nan::New<Number>(predictedLabel));
283298 res->Set (Nan::New (" confidence" ).ToLocalChecked (), Nan::New<Number>(confidence));
@@ -300,6 +315,16 @@ class PredictASyncWorker: public Nan::AsyncWorker {
300315
301316 void Execute () {
302317 this ->rec ->predict (this ->im , this ->predictedLabel , this ->confidence );
318+ #if CV_MAJOR_VERSION >= 3
319+ // Older versions of OpenCV3 incorrectly returned label=0 at
320+ // confidence=DBL_MAX instead of label=-1 on failure. This can be removed
321+ // once the fix* becomes more widespread.
322+ //
323+ // * https://github.com/Itseez/opencv_contrib/commit/0aa58ae9b30a017b356a86d29453c0b56ed9e625#diff-d9c561bf45c255c5951ff1ab55e80473
324+ if (this ->predictedLabel == 0 && this ->confidence == DBL_MAX) {
325+ this ->predictedLabel = -1 ;
326+ }
327+ #endif
303328 }
304329
305330 void HandleOKCallback () {
@@ -373,7 +398,27 @@ NAN_METHOD(FaceRecognizerWrap::GetMat) {
373398 JSTHROW (" getMat takes a key" )
374399 }
375400 std::string key = std::string (*Nan::Utf8String (info[0 ]->ToString ()));
376- cv::Mat m = self->rec ->getMat (key);
401+ cv::Mat m;
402+ #if CV_MAJOR_VERSION >= 3
403+ cv::face::BasicFaceRecognizer *bfr =
404+ dynamic_cast <cv::face::BasicFaceRecognizer*>(self->rec .get ());
405+ if (bfr == NULL ) {
406+ Nan::ThrowTypeError (" getMat not supported" );
407+ return ;
408+ }
409+ if (key.compare (" mean" ) == 0 ) {
410+ m = bfr->getMean ();
411+ } else if (key.compare (" eigenvectors" ) == 0 ) {
412+ m = bfr->getEigenVectors ();
413+ } else if (key.compare (" eigenvalues" ) == 0 ) {
414+ m = bfr->getEigenValues ();
415+ } else {
416+ Nan::ThrowTypeError (" Unknown getMat keyname" );
417+ return ;
418+ }
419+ #else
420+ m = self->rec ->getMat (key);
421+ #endif
377422
378423 Local<Object> im = Nan::New (Matrix::constructor)->GetFunction ()->NewInstance ();
379424 Matrix *img = Nan::ObjectWrap::Unwrap<Matrix>(im);
0 commit comments