修改OCR参数,尝试提高文字识别率

This commit is contained in:
MistEO
2021-07-29 23:01:23 +08:00
parent 5aa570040e
commit 79e80a61c8
3 changed files with 64 additions and 43 deletions

View File

@@ -8,50 +8,68 @@
#include "CrnnNet.h"
namespace ocr {
class OCRLITE_EXPORT OcrLite {
public:
OcrLite();
class OCRLITE_EXPORT OcrLite {
public:
OcrLite();
~OcrLite();
~OcrLite();
void setNumThread(int numOfThread);
void setNumThread(int numOfThread);
void initLogger(bool isConsole, bool isPartImg, bool isResultImg);
void initLogger(bool isConsole, bool isPartImg, bool isResultImg);
void enableResultTxt(const char* path, const char* imgName);
void enableResultTxt(const char* path, const char* imgName);
void setGpuIndex(int gpuIndex);
void setGpuIndex(int gpuIndex);
bool initModels(const std::string& detPath, const std::string& clsPath,
const std::string& recPath, const std::string& keysPath);
bool initModels(const std::string& detPath, const std::string& clsPath,
const std::string& recPath, const std::string& keysPath);
void Logger(const char* format, ...);
void Logger(const char* format, ...);
OcrResult detect(const char* path, const char* imgName,
int padding, int maxSideLen,
float boxScoreThresh, float boxThresh, float unClipRatio, bool doAngle, bool mostAngle);
/*
* padding图像预处理在图片外周添加白边用于提升识别率文字框没有正确框住所有文字时增加此值。
* maxSideLen 按图片最长边的长度此值为0代表不缩放1024如果图片长边大于1024则把图像整体缩小到1024再进行图像分割计算如果图片长边小于1024则不缩放如果图片长边小于32则缩放到32。
* boxScoreThresh文字框置信度门限文字框没有正确框住所有文字时减小此值。
* boxThresh请自行试验。
* unClipRatio单个文字框大小倍率越大时单个文字框越大。此项与图片的大小相关越大的图片此值应该越大。
* doAngle启用(1)/禁用(0) 文字方向检测,只有图片倒置的情况下(旋转90~270度的图片),才需要启用文字方向检测。
* mostAngle启用(1)/禁用(0) 角度投票(整张图片以最大可能文字方向来识别),当禁用文字方向检测时,此项也不起作用。
*/
OcrResult detect(const char* path, const char* imgName,
int padding, int maxSideLen,
float boxScoreThresh, float boxThresh, float unClipRatio, bool doAngle, bool mostAngle);
OcrResult detect(const cv::Mat & mat,
int padding, int maxSideLen,
float boxScoreThresh, float boxThresh, float unClipRatio, bool doAngle, bool mostAngle);
private:
bool isOutputConsole = false;
bool isOutputPartImg = false;
bool isOutputResultTxt = false;
bool isOutputResultImg = false;
FILE* resultTxt;
DbNet dbNet;
AngleNet angleNet;
CrnnNet crnnNet;
/*
* padding图像预处理在图片外周添加白边用于提升识别率文字框没有正确框住所有文字时增加此值。
* maxSideLen 按图片最长边的长度此值为0代表不缩放1024如果图片长边大于1024则把图像整体缩小到1024再进行图像分割计算如果图片长边小于1024则不缩放如果图片长边小于32则缩放到32。
* boxScoreThresh文字框置信度门限文字框没有正确框住所有文字时减小此值。
* boxThresh请自行试验。
* unClipRatio单个文字框大小倍率越大时单个文字框越大。此项与图片的大小相关越大的图片此值应该越大。
* doAngle启用(1)/禁用(0) 文字方向检测,只有图片倒置的情况下(旋转90~270度的图片),才需要启用文字方向检测。
* mostAngle启用(1)/禁用(0) 角度投票(整张图片以最大可能文字方向来识别),当禁用文字方向检测时,此项也不起作用。
*/
OcrResult detect(const cv::Mat& mat,
int padding, int maxSideLen,
float boxScoreThresh, float boxThresh, float unClipRatio, bool doAngle, bool mostAngle);
private:
bool isOutputConsole = false;
bool isOutputPartImg = false;
bool isOutputResultTxt = false;
bool isOutputResultImg = false;
FILE* resultTxt;
DbNet dbNet;
AngleNet angleNet;
CrnnNet crnnNet;
std::vector<cv::Mat> getPartImages(cv::Mat& src, std::vector<TextBox>& textBoxes,
const char* path, const char* imgName);
std::vector<cv::Mat> getPartImages(cv::Mat& src, std::vector<TextBox>& textBoxes,
const char* path, const char* imgName);
OcrResult detect(const char* path, const char* imgName,
cv::Mat& src, cv::Rect& originRect, ScaleParam& scale,
float boxScoreThresh = 0.6f, float boxThresh = 0.3f,
float unClipRatio = 2.0f, bool doAngle = true, bool mostAngle = true);
};
OcrResult detect(const char* path, const char* imgName,
cv::Mat& src, cv::Rect& originRect, ScaleParam& scale,
float boxScoreThresh = 0.6f, float boxThresh = 0.3f,
float unClipRatio = 2.0f, bool doAngle = true, bool mostAngle = true);
};
}
#endif //__OCR_LITE_H__

View File

@@ -57,12 +57,12 @@ double Identify::image_hist_comp(const cv::Mat& src, const cv::MatND& hist)
std::vector<TextArea> asst::Identify::ocr_detect(const cv::Mat& mat)
{
ocr::OcrResult ocr_results = m_ocr_lite.detect(mat,
50, 1024,
50, 0,
0.6f, 0.3f,
2.0f, true, true);
2.0f, false, false);
std::vector<TextArea> result;
for (const ocr::TextBlock & text_block : ocr_results.textBlocks) {
for (ocr::TextBlock & text_block : ocr_results.textBlocks) {
if (text_block.boxPoint.size() != 4) {
continue;
}
@@ -73,7 +73,7 @@ std::vector<TextArea> asst::Identify::ocr_detect(const cv::Mat& mat)
int y = text_block.boxPoint.at(0).y;
int width = text_block.boxPoint.at(1).x - x;
int height = text_block.boxPoint.at(3).y - y;
result.emplace_back(text_block.text, x, y, width, height);
result.emplace_back(std::move(text_block.text), x, y, width, height);
}
return result;
}
@@ -155,10 +155,10 @@ std::vector<TextArea> asst::Identify::find_text(const cv::Mat& mat, const std::v
{
std::vector<TextArea> dst;
std::vector<TextArea> detect_result = ocr_detect(mat);
for (const TextArea& res : detect_result) {
for (TextArea& res : detect_result) {
for (const std::string& t : texts) {
if (res.text == t) {
dst.emplace_back(res);
dst.emplace_back(std::move(res));
}
}
}
@@ -169,11 +169,11 @@ std::vector<TextArea> asst::Identify::find_text(const cv::Mat& mat, const std::u
{
std::vector<TextArea> dst;
std::vector<TextArea> detect_result = ocr_detect(mat);
for (const TextArea& res : detect_result) {
for (TextArea& res : detect_result) {
DebugTrace("detect", Utf8ToGbk(res.text));
for (const std::string& t : texts) {
if (res.text == t) {
dst.emplace_back(res);
dst.emplace_back(std::move(res));
}
}
}

View File

@@ -1,2 +1,5 @@
xcopy /e /y /i resource x64\Debug\resource
xcopy /e /y /i resource x64\Release\resource
xcopy /e /y /i resource x64\RelWithDebInfo\resource
xcopy /e /y /i resource x64\Release\resource
xcopy /e /y /i 3rdPart\resource x64\RelWithDebInfo\resource
xcopy /e /y /i 3rdPart\resource x64\Release\resource