From 756234beb6b9a3af39c0f1672b675b46e8b33239 Mon Sep 17 00:00:00 2001 From: Matt Hill Date: Sat, 17 Oct 2015 08:56:18 -0400 Subject: [PATCH] Added try/catch to image loading. Resolves issue #196 --- src/openalpr/alpr_impl.cpp | 58 +++++++++++++++++++++++++++----------- 1 file changed, 42 insertions(+), 16 deletions(-) diff --git a/src/openalpr/alpr_impl.cpp b/src/openalpr/alpr_impl.cpp index f9eaefb..5690b2f 100644 --- a/src/openalpr/alpr_impl.cpp +++ b/src/openalpr/alpr_impl.cpp @@ -410,36 +410,62 @@ namespace alpr AlprResults AlprImpl::recognize( std::vector imageBytes) { - cv::Mat img = cv::imdecode(cv::Mat(imageBytes), 1); - - return this->recognize(img); + try + { + cv::Mat img = cv::imdecode(cv::Mat(imageBytes), 1); + return this->recognize(img); + } + catch (cv::Exception& e) + { + std::cerr << "Caught exception in OpenALPR recognize: " << e.msg << std::endl; + AlprResults emptyresults; + return emptyresults; + } } AlprResults AlprImpl::recognize(std::vector imageBytes, std::vector regionsOfInterest) { - cv::Mat img = cv::imdecode(cv::Mat(imageBytes), 1); + try + { + cv::Mat img = cv::imdecode(cv::Mat(imageBytes), 1); - std::vector rois = convertRects(regionsOfInterest); + std::vector rois = convertRects(regionsOfInterest); - AlprFullDetails fullDetails = recognizeFullDetails(img, rois); - return fullDetails.results; + AlprFullDetails fullDetails = recognizeFullDetails(img, rois); + return fullDetails.results; + } + catch (cv::Exception& e) + { + std::cerr << "Caught exception in OpenALPR recognize: " << e.msg << std::endl; + AlprResults emptyresults; + return emptyresults; + } } AlprResults AlprImpl::recognize( unsigned char* pixelData, int bytesPerPixel, int imgWidth, int imgHeight, std::vector regionsOfInterest) { - int arraySize = imgWidth * imgHeight * bytesPerPixel; - cv::Mat imgData = cv::Mat(arraySize, 1, CV_8U, pixelData); - cv::Mat img = imgData.reshape(bytesPerPixel, imgHeight); - - if (regionsOfInterest.size() == 0) + try { - AlprRegionOfInterest fullFrame(0,0, img.cols, img.rows); + int arraySize = imgWidth * imgHeight * bytesPerPixel; + cv::Mat imgData = cv::Mat(arraySize, 1, CV_8U, pixelData); + cv::Mat img = imgData.reshape(bytesPerPixel, imgHeight); - regionsOfInterest.push_back(fullFrame); + if (regionsOfInterest.size() == 0) + { + AlprRegionOfInterest fullFrame(0,0, img.cols, img.rows); + + regionsOfInterest.push_back(fullFrame); + } + + return this->recognize(img, this->convertRects(regionsOfInterest)); + } + catch (cv::Exception& e) + { + std::cerr << "Caught exception in OpenALPR recognize: " << e.msg << std::endl; + AlprResults emptyresults; + return emptyresults; } - - return this->recognize(img, this->convertRects(regionsOfInterest)); } AlprResults AlprImpl::recognize(cv::Mat img)