From a237f33dc33bdcd63e76ca5a769ebdcfe0747b57 Mon Sep 17 00:00:00 2001 From: mayong Date: Wed, 24 Feb 2021 17:53:30 +0800 Subject: [PATCH] Determine parameters automatically. --- CMakeLists.txt | 1 + .../src/main/cpp/include/OcrUtils.h | 3 +- api4cpp/rapidocr_api.cpp | 75 ++++++++++++------- api4cpp/rapidocrtest.cpp | 4 +- include/precomp.h | 3 +- 5 files changed, 52 insertions(+), 34 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a68f51b..ccb7856 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -48,6 +48,7 @@ endif() # for onnxruntime link_libraries(onnxruntime) + FIND_PACKAGE(OpenCV REQUIRED) if(OPENCV_FOUND) message("OpenCV FOUND") diff --git a/android/BaiPiaoOcrAndroidOnnx/OcrLibrary/src/main/cpp/include/OcrUtils.h b/android/BaiPiaoOcrAndroidOnnx/OcrLibrary/src/main/cpp/include/OcrUtils.h index b4f5b5f..ec0f536 100755 --- a/android/BaiPiaoOcrAndroidOnnx/OcrLibrary/src/main/cpp/include/OcrUtils.h +++ b/android/BaiPiaoOcrAndroidOnnx/OcrLibrary/src/main/cpp/include/OcrUtils.h @@ -83,8 +83,7 @@ float boxScoreFast(const std::vector &boxes, const cv::Mat &pred); cv::RotatedRect unClip(std::vector box, float unClipRatio); -std::vector -substractMeanNormalize(cv::Mat &src, const float *meanVals, const float *normVals); +std::vector substractMeanNormalize(cv::Mat &src, const float *meanVals, const float *normVals); std::vector getAngleIndexes(std::vector &angles); diff --git a/api4cpp/rapidocr_api.cpp b/api4cpp/rapidocr_api.cpp index dc13309..9355d52 100755 --- a/api4cpp/rapidocr_api.cpp +++ b/api4cpp/rapidocr_api.cpp @@ -31,7 +31,7 @@ extern "C" } - _QM_OCR_API BOOL BPOcrDoOcr(BPHANDLE handle, const char* szImgPath, BOOL bAutoParam,BOOL bLongPic,RAPIDOCR_PARAM *pParam) + _QM_OCR_API BOOL BPOcrDoOcr(BPHANDLE handle, const char* szImgPath, BOOL bAutoParam, BOOL bLongPic,RAPIDOCR_PARAM *pParam) { OCR_OBJ* pOcrObj=(OCR_OBJ*)handle; @@ -39,13 +39,48 @@ extern "C" return FALSE; RAPIDOCR_PARAM Param = *pParam; - if(bAutoParam) + + if (Param.boxScoreThresh == 0) + Param.boxScoreThresh = 0.5; + if (Param.boxThresh == 0) + Param.boxThresh = 0.3; + if (Param.flagDoAngle == 0) + Param.flagDoAngle = 1; + if (Param.flagMostAngle == 0) + Param.flagMostAngle = 0; + + + + if (Param.maxSideLen == 0) + Param.maxSideLen = 1024; + if (Param.unClipRatio == 0) + Param.unClipRatio = 1.5; + + + + if (bAutoParam) { + cv::Mat oriImg = cv::imread(szImgPath); + + int nMax = oriImg.rows > oriImg.cols ? oriImg.rows : oriImg.cols; + int nMin = oriImg.rows > oriImg.cols ? oriImg.cols: oriImg.rows ; + + if (nMax > nMin * 5) // 5 times + bLongPic = true; } - else - { + + if (bLongPic) + { + + Param.maxSideLen = 0; + + Param.unClipRatio = 3.0; + } + + + // -numThread %NUMBER_OF_PROCESSORS% ^ // --padding 0 ^ // --maxSideLen %MAX_SIDE_LEN% ^ @@ -54,34 +89,16 @@ extern "C" // --unClipRatio %CLIP_RATE% ^ // --doAngle 1 ^ // --mostAngle 0 - if(Param.boxScoreThresh ==0) - Param.boxScoreThresh = 0.5; - if(Param.boxThresh ==0) - Param.boxThresh =0.3; - if(Param.flagDoAngle ==0) - Param.flagDoAngle =1; - if(Param.flagMostAngle ==0) - Param.flagMostAngle =0; + +// ²ÎÊýÉèÖÃËã·¨£º https://github.com/znsoftm/RapidOCR/blob/219bf6295a9c3c5afa7f47bf5173ca02f1003521/python/ch_ppocr_mobile_v2_det/utils.py#L118 + - if( bLongPic) - { - if(Param.maxSideLen ==0) - Param.maxSideLen =0; - if(Param.unClipRatio ==0) - Param.unClipRatio = 3.0; - } - else - { - if(Param.maxSideLen ==0) - Param.maxSideLen =1024; - if(Param.unClipRatio ==0) - Param.unClipRatio = 1.5; - } - } + + std::string imgPath,imgDir,imgName; imgPath=szImgPath; - imgDir.assign(imgPath.substr(0, imgPath.find_last_of('\\') + 1)); - imgName.assign(imgPath.substr(imgPath.find_last_of('\\') + 1)); + imgDir=imgPath.substr(0, imgPath.find_last_of('\\') + 1); + imgName =imgPath.substr(imgPath.find_last_of('\\') + 1); OcrResult result=pOcrObj->OcrObj.detect(imgDir.c_str(),imgName.c_str(),Param.padding,Param.maxSideLen,Param.boxScoreThresh,Param.boxThresh,Param.unClipRatio,Param.flagDoAngle?true:false,Param.flagMostAngle?true:false); if (result.strRes.length() > 0) { diff --git a/api4cpp/rapidocrtest.cpp b/api4cpp/rapidocrtest.cpp index 5ca2f13..e0b24b7 100755 --- a/api4cpp/rapidocrtest.cpp +++ b/api4cpp/rapidocrtest.cpp @@ -16,7 +16,7 @@ const char* DEFAULT_MODEL_DIR = "E:\\bai-piao-ocr\\cpp\\BaiPiaoOcrOnnx\\models\\"; -const char* DEFAULT_TEST_IMG = "E:\\bai-piao-ocr\\cpp\\BaiPiaoOcrOnnx\\images\\1.jpg"; +const char* DEFAULT_TEST_IMG = "E:\\bai-piao-ocr\\cpp\\BaiPiaoOcrOnnx\\images\\long1.jpg"; #else const char * DEFAULT_MODEL_DIR ="/data/workprj/RapidOCR/models/"; @@ -69,7 +69,7 @@ const char *szImagePath=NULL; } RAPIDOCR_PARAM Param={0}; - BOOL bRet =BPOcrDoOcr(Handle,szImagePath,false,false,&Param); + BOOL bRet =BPOcrDoOcr(Handle,szImagePath,true,false,&Param); if (bRet) { int nLen = BPOcrGetLen(Handle); diff --git a/include/precomp.h b/include/precomp.h index 335ee26..6c332ed 100755 --- a/include/precomp.h +++ b/include/precomp.h @@ -2,4 +2,5 @@ #include "OcrUtils.h" #include "rapidocr_api.h" #include "omp.h" -#include "OcrLite.h" \ No newline at end of file +#include "OcrLite.h" +#include "opencv2/imgcodecs.hpp" \ No newline at end of file