From 0905ab40350bd02be378d745e33881533994e0ae Mon Sep 17 00:00:00 2001 From: Emanuel Date: Tue, 21 Jul 2015 01:40:45 -0300 Subject: [PATCH] Fixes error trying to load an image that is not an image. Fixes error trying to detect faces in some images. --- src/CascadeClassifierWrap.cc | 22 ++++++++++-------- src/OpenCV.cc | 45 ++++++++++++++++++------------------ 2 files changed, 35 insertions(+), 32 deletions(-) diff --git a/src/CascadeClassifierWrap.cc b/src/CascadeClassifierWrap.cc index 2398103..0032c8f 100755 --- a/src/CascadeClassifierWrap.cc +++ b/src/CascadeClassifierWrap.cc @@ -54,17 +54,21 @@ class AsyncDetectMultiScale : public NanAsyncWorker { ~AsyncDetectMultiScale() {} void Execute () { - std::vector objects; + try { + std::vector objects; - cv::Mat gray; + cv::Mat gray; - if(this->im->mat.channels() != 1) - cvtColor(this->im->mat, gray, CV_BGR2GRAY); - - equalizeHist( gray, gray); - this->cc->cc.detectMultiScale(gray, objects, this->scale, this->neighbors, 0 | CV_HAAR_SCALE_IMAGE, cv::Size(this->minw, this->minh)); - - res = objects; + if(this->im->mat.channels() != 1) { + cvtColor(this->im->mat, gray, CV_BGR2GRAY); + } + equalizeHist( gray, gray); + this->cc->cc.detectMultiScale(gray, objects, this->scale, this->neighbors, 0 | CV_HAAR_SCALE_IMAGE, cv::Size(this->minw, this->minh)); + res = objects; + } + catch( cv::Exception& e ){ + SetErrorMessage(e.what()); + } } void HandleOKCallback () { diff --git a/src/OpenCV.cc b/src/OpenCV.cc index 4ccd4d2..e22a485 100755 --- a/src/OpenCV.cc +++ b/src/OpenCV.cc @@ -18,20 +18,20 @@ OpenCV::Init(Handle target) { NAN_METHOD(OpenCV::ReadImage) { NanEscapableScope(); + REQ_FUN_ARG(1, cb); + + Local argv[2]; + + argv[0] = NanNull(); + + Local im_h = NanNew(Matrix::constructor)->GetFunction()->NewInstance(); + Matrix *img = ObjectWrap::Unwrap(im_h); + argv[1] = im_h; + try{ - Local im_h = NanNew(Matrix::constructor)->GetFunction()->NewInstance(); - Matrix *img = ObjectWrap::Unwrap(im_h); - cv::Mat mat; - REQ_FUN_ARG(1, cb); - - Local argv[2]; - - argv[0] = NanNull(); - argv[1] = im_h; - if (args[0]->IsNumber() && args[1]->IsNumber()){ int width, height; @@ -58,19 +58,18 @@ NAN_METHOD(OpenCV::ReadImage) { img->mat = mat; - TryCatch try_catch; - - cb->Call(NanGetCurrentContext()->Global(), 2, argv); - - if (try_catch.HasCaught()) { - FatalException(try_catch); - } - - NanReturnUndefined(); - } catch( cv::Exception& e ){ - const char* err_msg = e.what(); - NanThrowError(err_msg); - NanReturnUndefined(); + argv[0] = NanError(e.what()); + argv[1] = NanNull(); } + + TryCatch try_catch; + + cb->Call(NanGetCurrentContext()->Global(), 2, argv); + + if (try_catch.HasCaught()) { + FatalException(try_catch); + } + + NanReturnUndefined(); };