1.所用到的ncnn格式的模型文件
要问这些模型哪里来的,请看下面提示信息:
2.查看字典函数读取方法
char* readKeysFromAssets()
{
std::ifstream ifs("./model/paddleocr_keys.txt");
if (!ifs.is_open())
{
return 0;
}
ifs.seekg(0, std::ios_base::end);
int length = ifs.tellg();
ifs.seekg(std::ios_base::beg);
char* buff = new char[length + 1]();
ifs.read(buff, length + 1);
return buff;
}
这个paddleocr_keys.txt 的字符编码格式是可以修改,改为ANSI 格式,这样在cmd行中就不会中文乱码
3.模型加载函数,分为检测模型和识别模型
int ret = dbNet.load_param("./model/pdocrv2.0_det-op.param");
if (ret != 0)
{
return false;
}
ret = crnnNet.load_param("./model/pdocrv2.0_rec-op.param");
if (ret != 0)
{
return false;
}
}
// init bin
{
int ret = dbNet.load_model("./model/pdocrv2.0_det-op.bin");
if (ret != 0)
{
return false;
}
ret = crnnNet.load_model("./model/pdocrv2.0_rec-op.bin");
if (ret != 0)
{
return false;
}
}
4.模型推理主要函数
void PaddleOCRNcnn_Detect(bool use_gpu, cv::Mat& rgb)
{
std::vector<TextBox> objects;
objects = getTextBoxes(rgb, 0.4, 0.3, 2.0);
std::vector<cv::Mat> partImages = getPartImages(rgb, objects);
std::vector<TextLine> textLines = getTextLines(partImages);
if (textLines.size() > 0)
{
for (int i = 0; i < textLines.size(); i++)
objects[i].text = textLines[i].text;
}
if (objects.size() > 0)
{
for (size_t i = 0; i < objects.size(); i++)
{
float x0 = objects[i].boxPoint[0].x;
float y0 = objects[i].boxPoint[0].y;
float x1 = objects[i].boxPoint[1].x;
float y1 = objects[i].boxPoint[1].y;
float x2 = objects[i].boxPoint[2].x;
float y2 = objects[i].boxPoint[2].y;
float x3 = objects[i].boxPoint[3].x;
float y3 = objects[i].boxPoint[3].y;
cv::line(rgb, cv::Point(x0, y0), cv::Point(x1, y1), cv::Scalar(0, 0, 255), 2, 8);
cv::line(rgb, cv::Point(x1, y1), cv::Point(x2, y2), cv::Scalar(0, 0, 255), 2, 8);
cv::line(rgb, cv::Point(x2, y2), cv::Point(x3, y3), cv::Scalar(0, 0, 255), 2, 8);
cv::line(rgb, cv::Point(x3, y3), cv::Point(x0, y0), cv::Scalar(0, 0, 255), 2, 8);
cv::putTextZH(
rgb,
textLines[i].text.c_str(),
cv::Point(x1, y1),
CV_RGB(255, 255, 255),
20
);
}
}
}
5.OCR识别推理的结果
我只是个代码搬运工,需要下载代码的地址。
https://download.csdn.net/download/huzhifei/88016666