Skip to content

Enable FaceRecognizer on OpenCV3 #379

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 25, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 32 additions & 8 deletions src/FaceRecognizer.cc
Original file line number Diff line number Diff line change
@@ -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 <nan.h>

#if CV_MAJOR_VERSION >= 3
namespace cv {
using std::vector;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Little hacks to make the OpenCV3 versions look more like the CV2 versions, to avoid ifdefing all over the place

using cv::face::createEigenFaceRecognizer;
using cv::face::createFisherFaceRecognizer;
using cv::face::createLBPHFaceRecognizer;
}
#endif

#define EIGEN 0
#define LBPH 1
#define FISHER 2
Expand Down Expand Up @@ -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<cv::face::BasicFaceRecognizer*>(self->rec.get());
if (bfr == NULL) {
Nan::ThrowTypeError("getMat not supported");
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LBPHFaceRecognizer doesn't support this method in CV3

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<Object> im = Nan::New(Matrix::constructor)->GetFunction()->NewInstance();
Matrix *img = Nan::ObjectWrap::Unwrap<Matrix>(im);
Expand Down
11 changes: 9 additions & 2 deletions src/FaceRecognizer.h
Original file line number Diff line number Diff line change
@@ -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 <opencv2/face.hpp>
namespace cv {
using cv::face::FaceRecognizer;
}
#else
#include "opencv2/contrib/contrib.hpp"
#endif

class FaceRecognizerWrap: public Nan::ObjectWrap {
public:
Expand Down
5 changes: 5 additions & 0 deletions src/OpenCV.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/opencv_modules.hpp>
#endif
#if ((CV_MAJOR_VERSION == 2) && (CV_MINOR_VERSION >=4) && (CV_SUBMINOR_VERSION>=4))
#define HAVE_OPENCV_FACE
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In CV3, HAVE_OPENCV_FACE is only defined if the face module from opencv_contrib is installed.

#endif

#include <string.h>
#include <nan.h>

Expand Down
4 changes: 2 additions & 2 deletions src/init.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ extern "C" void init(Local<Object> target) {
BackgroundSubtractorWrap::Init(target);
Features::Init(target);
LDAWrap::Init(target);
#if CV_SUBMINOR_VERSION>=4
FaceRecognizerWrap::Init(target);
#endif
#endif
#ifdef HAVE_OPENCV_FACE
FaceRecognizerWrap::Init(target);
#endif
};

Expand Down