From 2405bc5a59ced1567a9f68dc78732fb229da6040 Mon Sep 17 00:00:00 2001 From: Michael Vines Date: Wed, 24 Feb 2016 20:12:23 -0800 Subject: [PATCH] Enable FaceRecognizer on OpenCV3 --- src/FaceRecognizer.cc | 40 ++++++++++++++++++++++++++++++++-------- src/FaceRecognizer.h | 9 ++++++++- src/OpenCV.h | 5 +++++ src/init.cc | 6 +++--- 4 files changed, 48 insertions(+), 12 deletions(-) diff --git a/src/FaceRecognizer.cc b/src/FaceRecognizer.cc index 47823c5..c23e6a3 100644 --- a/src/FaceRecognizer.cc +++ b/src/FaceRecognizer.cc @@ -1,15 +1,19 @@ -#include "FaceRecognizer.h" #include "OpenCV.h" -#if CV_MAJOR_VERSION >= 3 -#warning TODO: port me to OpenCV 3 -#endif - -#if ((CV_MAJOR_VERSION == 2) && (CV_MINOR_VERSION >=4) && (CV_SUBMINOR_VERSION>=4)) - +#ifdef HAVE_OPENCV_FACE +#include "FaceRecognizer.h" #include "Matrix.h" #include +#if CV_MAJOR_VERSION >= 3 +namespace cv { + using std::vector; + using cv::face::createEigenFaceRecognizer; + using cv::face::createFisherFaceRecognizer; + using cv::face::createLBPHFaceRecognizer; +} +#endif + #define EIGEN 0 #define LBPH 1 #define FISHER 2 @@ -373,7 +377,27 @@ NAN_METHOD(FaceRecognizerWrap::GetMat) { JSTHROW("getMat takes a key") } std::string key = std::string(*Nan::Utf8String(info[0]->ToString())); - cv::Mat m = self->rec->getMat(key); + cv::Mat m; +#if CV_MAJOR_VERSION >= 3 + cv::face::BasicFaceRecognizer *bfr = + dynamic_cast(self->rec.get()); + if (bfr == NULL) { + Nan::ThrowTypeError("getMat not supported"); + return; + } + if (key.compare("mean") == 0) { + m = bfr->getMean(); + } else if (key.compare("eigenvectors") == 0) { + m = bfr->getEigenVectors(); + } else if (key.compare("eigenvalues") == 0) { + m = bfr->getEigenValues(); + } else { + Nan::ThrowTypeError("Unknown getMat keyname"); + return; + } +#else + m = self->rec->getMat(key); +#endif Local im = Nan::New(Matrix::constructor)->GetFunction()->NewInstance(); Matrix *img = Nan::ObjectWrap::Unwrap(im); diff --git a/src/FaceRecognizer.h b/src/FaceRecognizer.h index 0224c28..a147229 100644 --- a/src/FaceRecognizer.h +++ b/src/FaceRecognizer.h @@ -1,8 +1,15 @@ #include "OpenCV.h" -#if ((CV_MAJOR_VERSION == 2) && (CV_MINOR_VERSION >=4) && (CV_SUBMINOR_VERSION>=4)) +#ifdef HAVE_OPENCV_FACE +#if CV_MAJOR_VERSION >= 3 +#include +namespace cv { + using cv::face::FaceRecognizer; +} +#else #include "opencv2/contrib/contrib.hpp" +#endif class FaceRecognizerWrap: public Nan::ObjectWrap { public: diff --git a/src/OpenCV.h b/src/OpenCV.h index 9eab257..f0ff9d3 100755 --- a/src/OpenCV.h +++ b/src/OpenCV.h @@ -22,7 +22,12 @@ #include #include #include +#include #endif +#if ((CV_MAJOR_VERSION == 2) && (CV_MINOR_VERSION >=4) && (CV_SUBMINOR_VERSION>=4)) +#define HAVE_OPENCV_FACE +#endif + #include #include diff --git a/src/init.cc b/src/init.cc index 75cb397..7d91bbe 100755 --- a/src/init.cc +++ b/src/init.cc @@ -38,11 +38,11 @@ extern "C" void init(Local target) { BackgroundSubtractorWrap::Init(target); Features::Init(target); LDAWrap::Init(target); -#if CV_SUBMINOR_VERSION>=4 +#endif +#endif +#ifdef HAVE_OPENCV_FACE FaceRecognizerWrap::Init(target); #endif -#endif -#endif }; NODE_MODULE(opencv, init)