1
- #include " FaceRecognizer.h"
2
1
#include " OpenCV.h"
3
2
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"
10
5
#include " Matrix.h"
11
6
#include < nan.h>
12
7
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
+
13
17
#define EIGEN 0
14
18
#define LBPH 1
15
19
#define FISHER 2
@@ -278,6 +282,17 @@ NAN_METHOD(FaceRecognizerWrap::PredictSync) {
278
282
double confidence = 0.0 ;
279
283
self->rec ->predict (im, predictedLabel, confidence);
280
284
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
+
281
296
v8::Local<v8::Object> res = Nan::New<Object>();
282
297
res->Set (Nan::New (" id" ).ToLocalChecked (), Nan::New<Number>(predictedLabel));
283
298
res->Set (Nan::New (" confidence" ).ToLocalChecked (), Nan::New<Number>(confidence));
@@ -300,6 +315,16 @@ class PredictASyncWorker: public Nan::AsyncWorker {
300
315
301
316
void Execute () {
302
317
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
303
328
}
304
329
305
330
void HandleOKCallback () {
@@ -373,7 +398,27 @@ NAN_METHOD(FaceRecognizerWrap::GetMat) {
373
398
JSTHROW (" getMat takes a key" )
374
399
}
375
400
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
377
422
378
423
Local<Object> im = Nan::New (Matrix::constructor)->GetFunction ()->NewInstance ();
379
424
Matrix *img = Nan::ObjectWrap::Unwrap<Matrix>(im);
0 commit comments