Skip to content

Commit 2405bc5

Browse files
author
Michael Vines
committed
Enable FaceRecognizer on OpenCV3
1 parent d7b7161 commit 2405bc5

File tree

4 files changed

+48
-12
lines changed

4 files changed

+48
-12
lines changed

src/FaceRecognizer.cc

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
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
@@ -373,7 +377,27 @@ NAN_METHOD(FaceRecognizerWrap::GetMat) {
373377
JSTHROW("getMat takes a key")
374378
}
375379
std::string key = std::string(*Nan::Utf8String(info[0]->ToString()));
376-
cv::Mat m = self->rec->getMat(key);
380+
cv::Mat m;
381+
#if CV_MAJOR_VERSION >= 3
382+
cv::face::BasicFaceRecognizer *bfr =
383+
dynamic_cast<cv::face::BasicFaceRecognizer*>(self->rec.get());
384+
if (bfr == NULL) {
385+
Nan::ThrowTypeError("getMat not supported");
386+
return;
387+
}
388+
if (key.compare("mean") == 0) {
389+
m = bfr->getMean();
390+
} else if (key.compare("eigenvectors") == 0) {
391+
m = bfr->getEigenVectors();
392+
} else if (key.compare("eigenvalues") == 0) {
393+
m = bfr->getEigenValues();
394+
} else {
395+
Nan::ThrowTypeError("Unknown getMat keyname");
396+
return;
397+
}
398+
#else
399+
m = self->rec->getMat(key);
400+
#endif
377401

378402
Local<Object> im = Nan::New(Matrix::constructor)->GetFunction()->NewInstance();
379403
Matrix *img = Nan::ObjectWrap::Unwrap<Matrix>(im);

src/FaceRecognizer.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
#include "OpenCV.h"
22

3-
#if ((CV_MAJOR_VERSION == 2) && (CV_MINOR_VERSION >=4) && (CV_SUBMINOR_VERSION>=4))
4-
3+
#ifdef HAVE_OPENCV_FACE
4+
5+
#if CV_MAJOR_VERSION >= 3
6+
#include <opencv2/face.hpp>
7+
namespace cv {
8+
using cv::face::FaceRecognizer;
9+
}
10+
#else
511
#include "opencv2/contrib/contrib.hpp"
12+
#endif
613

714
class FaceRecognizerWrap: public Nan::ObjectWrap {
815
public:

src/OpenCV.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@
2222
#include <opencv2/highgui.hpp>
2323
#include <opencv2/imgproc.hpp>
2424
#include <opencv2/videoio.hpp>
25+
#include <opencv2/opencv_modules.hpp>
2526
#endif
27+
#if ((CV_MAJOR_VERSION == 2) && (CV_MINOR_VERSION >=4) && (CV_SUBMINOR_VERSION>=4))
28+
#define HAVE_OPENCV_FACE
29+
#endif
30+
2631
#include <string.h>
2732
#include <nan.h>
2833

src/init.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ extern "C" void init(Local<Object> target) {
3838
BackgroundSubtractorWrap::Init(target);
3939
Features::Init(target);
4040
LDAWrap::Init(target);
41-
#if CV_SUBMINOR_VERSION>=4
42-
FaceRecognizerWrap::Init(target);
4341
#endif
4442
#endif
43+
#ifdef HAVE_OPENCV_FACE
44+
FaceRecognizerWrap::Init(target);
4545
#endif
4646
};
4747

0 commit comments

Comments
 (0)