Skip to content

Commit ead733b

Browse files
author
Michael Vines
committed
Merge pull request #379 from mvines/face
Enable FaceRecognizer on OpenCV3
2 parents d7b7161 + 85b4542 commit ead733b

File tree

4 files changed

+69
-12
lines changed

4 files changed

+69
-12
lines changed

src/FaceRecognizer.cc

+53-8
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
@@ -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);

src/FaceRecognizer.h

+9-2
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

+5
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

+2-2
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)