文字识别(OCR)专题——基于NCNN轻量级PaddleOCRv4模型C++推理

前言

PaddleOCR 提供了基于深度学习的文本检测、识别和方向检测等功能。其主要推荐的 PP-OCR 算法在国内外的企业开发者中得到广泛应用。在短短的几年时间里,PP-OCR 的累计 Star 数已经超过了32.2k,常常出现在 GitHub Trending 和 Paperswithcode 的日榜和月榜第一位,被认为是当前OCR领域最热门的仓库之一。

PaddleOCR 最初主打的 PP-OCR 系列模型在去年五月份推出了 v3 版本。最近,飞桨 AI 套件团队对 PP-OCRv3 进行了全面改进,推出了重大更新版本 PP-OCRv4。这个新版本预计带来了更先进的技术、更高的性能和更广泛的适用性,将进一步推动OCR技术在各个领域的应用。

PP-OCRv4在速度可比情况下,中文场景端到端 Hmean 指标相比于 PP-OCRv3提升4.25%,效果大幅提升。具体指标如下表所示:
在这里插入图片描述
测试环境:CPU 型号为 Intel Gold 6148,CPU 预测时使用 OpenVINO。

除了更新中文模型,本次升级也优化了英文数字模型,在自有评估集上文本识别准确率提升6%,如下表所示:
在这里插入图片描述
同时,也对已支持的80余种语言识别模型进行了升级更新,在有评估集的四种语系识别准确率平均提升5%以上,如下表所示:
在这里插入图片描述

一、模型转换

1.模型下载

从https://github.com/PaddlePaddle/PaddleOCR/tree/release/2.7 下载要用到的模型,要下载的模型有文本检测模型、文字方向模型、文字识别模型,我这里只下下载了文本检测与文字识别的模型。
在这里插入图片描述
下载好的模型nference.pdiparams为模型参数文件,inference.pdmodel为模型结构文件,这两个文件在转换onnx的时候都要用到。

2.模型转成onnx

使用paddle2ONNX进行模型转换,git地址:https://github.com/paddlepaddle/paddle2onnx, 下载源码然后编译转换,也可以使用在线转换的方法,如果嫌麻烦,最好使用在线的转换方法,在线地址:https://www.paddlepaddle.org.cn/paddle/visualdl/modelconverter/x2paddle
在这里插入图片描述

3. onnx转ncnn模型

这里为了之后在移动部署做准备,选择使用NCNN做最终的模型推理,NCNN封装了很高效的API接品,可以方便地在移动设备和嵌入式系统上进行神经网络的部署和推理。适用于移动设备和嵌入式设备。它被设计用于在各种硬件平台上高效地运行神经网络推断(inference)。NCNN主要特点包括:

  1. 轻量级和高效性: NCNN被设计为一个轻量级框架,具有高度优化的推断性能。它的设计目标是在移动设备和嵌入式设备上实现高效的神经网络推理。

  2. 跨平台支持: NCNN支持多种硬件平台,包括CPU、GPU、DSP等,并且可以在各种操作系统上运行,如Windows、Android、iOS、Linux等。

  3. 优化和硬件加速: NCNN对各种硬件进行了优化,并利用硬件加速特性提高了神经网络推断的性能。

  4. 丰富的模型支持: NCNN支持各种常见的深度学习模型,如AlexNet、VGG、ResNet、MobileNet等,并且兼容一些深度学习框架导出的模型,Caffe、TensorFlow、ONNX等。

可以从https://github.com/Tencent/ncnn 获取源码进行编译,也可以下载官方编译好的lib进行转换,还可以使用在线接口进行转换。在线接地址:https://convertmodel.com/。
在这里插入图片描述

转出来的模型后缀是.param和.bin文件。

二、文本检测

文本检测是旨在从图像或视频中准确地检测和定位文本的位置和边界框,OCR系统中的一个重要组成部分,它为后续的文本识别提供了定位和定界的信息。

  1. 预处理:对输入的图像进行预处理,可能包括图像增强、去噪、尺寸标准化等操作,以便更好地适应文本检测算法。

  2. 文本区域检测:使用特定的算法或模型来检测图像中可能包含文本的区域。常见的方法包括基于区域的方法(如基于区域的CNN(R-CNN)系列)、基于锚点的方法(如SSD和YOLO)、以及基于注意力机制的方法(如EAST、TextBoxes++等)。

  3. 后处理:在获取文本区域的初始预测结果后,可以进行后处理步骤来提高检测的准确性和稳定性。这可能包括非极大值抑制(NMS)来消除重叠的边界框、边框回归以精细调整边界框的位置等。

文本检测类:

#ifndef __OCR_DBNET_H__
#define __OCR_DBNET_H__

#include "base_struct.h"
#include <ncnn/net.h>
#include <vector>
#include <ncnn/cpu.h>

namespace NCNNOCR
{
    class DbNet
    {
    public:
        DbNet();

        ~DbNet() {};

        int read_model(std::string param_path = "data/det.param",
            std::string bin_path = "data/det.bin", bool use_gpu = true);

        bool detect(cv::Mat& src, std::vector<TextBox>& results, int _target_size = 1024);

    private:
        ncnn::Net net;
        const float meanValues[3] = { 0.485 * 255, 0.456 * 255, 0.406 * 255 };
        const float normValues[3] = { 1.0 / 0.229 / 255.0, 1.0 / 0.224 / 255.0,
                                     1.0 / 0.225 / 255.0 };
        float boxThresh = 0.3f;
        float boxScoreThresh = 0.5f;
        float unClipRatio = 2.0f;
        int target_size;

    };
}

#endif //__OCR_DBNET_H__

类实现:

#include "db_net.h"
#include "tools.h"

namespace NCNNOCR
{
    int DbNet::read_model(std::string param_path, std::string bin_path, bool use_gpu)
    {
        ncnn::set_cpu_powersave(2);
        ncnn::set_omp_num_threads(ncnn::get_big_cpu_count());

        net.opt = ncnn::Option();

#if NCNN_VULKAN
        net.opt.use_vulkan_compute = use_gpu;
#endif
        net.opt.lightmode = true;
        net.opt.num_threads = ncnn::get_big_cpu_count();

        int rp = net.load_param(param_path.c_str());
        int rb = net.load_model(bin_path.c_str());

        if (rp == 0 || rb == 0)
        {
            return false;
        }

        return true;
    }

    std::vector<TextBox> inline findRsBoxes(const cv::Mat& fMapMat,
        const cv::Mat& norfMapMat,
        const float boxScoreThresh,
        const float unClipRatio)
    {
        const float minArea = 3;
        std::vector<TextBox> rsBoxes;
        rsBoxes.clear();

        std::vector<std::vector<cv::Point>> contours;
        cv::findContours(norfMapMat, contours, cv::RETR_LIST,
            cv::CHAIN_APPROX_SIMPLE);
        for (int i = 0; i < contours.size(); ++i) {
            float minSideLen, perimeter;
            std::vector<cv::Point> minBox =
                getMinBoxes(contours[i], minSideLen, perimeter);
            if (minSideLen < minArea)
                continue;
            float score = boxScoreFast(fMapMat, contours[i]);
            if (score < boxScoreThresh)
                continue;
            //---use clipper start---
            std::vector<cv::Point> clipBox = unClip(minBox, perimeter, unClipRatio);
            std::vector<cv::Point> clipMinBox =
                getMinBoxes(clipBox, minSideLen, perimeter);
            //---use clipper end---

            if (minSideLen < minArea + 2)
                continue;

            for (int j = 0; j < clipMinBox.size(); ++j) {
                clipMinBox[j].x = (clipMinBox[j].x / 1.0);
                clipMinBox[j].x =
                    (std::min)((std::max)(clipMinBox[j].x, 0), norfMapMat.cols);

                clipMinBox[j].y = (clipMinBox[j].y / 1.0);
                clipMinBox[j].y =
                    (std::min)((std::max)(clipMinBox[j].y, 0), norfMapMat.rows);
            }

            rsBoxes.emplace_back(TextBox{ clipMinBox, score });
        }
        reverse(rsBoxes.begin(), rsBoxes.end());

        return rsBoxes;
    }

    bool DbNet::detect(cv::Mat& src, std::vector<TextBox>& results_, int _target_size)
    {
        target_size = _target_size;
        int width = src.cols;
        int height = src.rows;
        
        int w = width;
        int h = height;
        float scale = 1.f;
        const int resizeMode = 0; // min = 0, max = 1

        if (resizeMode == 1) {
            if (w < h) {
                scale = (float)target_size / w;
                w = target_size;
                h = h * scale;
            }
            else {
                scale = (float)target_size / h;
                h = target_size;
                w = w * scale;
            }
        }
        else if (resizeMode == 0) {
            if (w > h) {
                scale = (float)target_size / w;
                w = target_size;
                h = h * scale;
            }
            else {
                scale = (float)target_size / h;
                w = w * scale;
                h = target_size;
            }
        }
        ncnn::Extractor extractor = net.create_extractor();

        ncnn::Mat out;
        cv::Size in_pad_size;
        int wpad = (w + 31) / 32 * 32 - w;
        int hpad = (h + 31) / 32 * 32 - h;

        ncnn::Mat in_pad_;
        ncnn::Mat input = ncnn::Mat::from_pixels_resize(
            src.data, ncnn::Mat::PIXEL_RGB, width, height, w, h);

        // pad to target_size rectangle

        ncnn::copy_make_border(input, in_pad_, hpad / 2, hpad - hpad / 2, wpad / 2,
            wpad - wpad / 2, ncnn::BORDER_CONSTANT, 0.f);
        in_pad_.substract_mean_normalize(meanValues, normValues);
        in_pad_size = cv::Size(in_pad_.w, in_pad_.h);
        extractor.input("x", in_pad_);
        extractor.extract("sigmoid_0.tmp_0", out);

        //    ncnn::Mat flattened_out = out.reshape(out.w * out.h * out.c);
            //-----boxThresh-----
        cv::Mat fMapMat(in_pad_size.height, in_pad_size.width, CV_32FC1, (float*)out.data);
        cv::Mat norfMapMat;
        norfMapMat = fMapMat > boxThresh;

        cv::Mat element = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(2, 2));
        cv::dilate(norfMapMat, norfMapMat, element, cv::Point(-1, -1), 1);

        std::vector<TextBox> results =
            findRsBoxes(fMapMat, norfMapMat, boxScoreThresh,
                unClipRatio);
        for (int i = 0; i < results.size(); i++) {
            for (int j = 0; j < results[i].boxPoint.size(); j++) {
                float x = float(results[i].boxPoint[j].x - (wpad / 2)) / scale;
                float y = float(results[i].boxPoint[j].y - (hpad / 2)) / scale;
                x = std::max(std::min(x, (float)(width - 1)), 0.f);
                y = std::max(std::min(y, (float)(height - 1)), 0.f);
                results[i].boxPoint[j].x = (int)x;
                results[i].boxPoint[j].y = (int)y;
            }
            if (abs(results[i].boxPoint[0].x - results[i].boxPoint[1].x) <= 3) {
                continue;
            }
            if (abs(results[i].boxPoint[0].y - results[i].boxPoint[3].y) <= 3) {
                continue;
            }
            results_.push_back(results[i]);
        }

        return true;
    }

    DbNet::DbNet()
    {

    }
}

检测结果:
在这里插入图片描述
在这里插入图片描述

三、文字识别

1. OCR

文字识别是将印刷或手写文本转换为可文本,被广泛应用于各种领域,包括数字化档案管理、自动化数据录入、图像搜索、身份验证、自动车牌识别、票据处理、手写文字识别。

类声明:

#ifndef __OCR_CRNNNET_H__
#define __OCR_CRNNNET_H__

#include "base_struct.h"
#include <ncnn/net.h>
#include <opencv2/opencv.hpp>
#include <vector>
#include <ncnn/cpu.h>
#include <fstream>

namespace NCNNOCR
{
    class CrnnNet
    {
    public:
        CrnnNet();

        ~CrnnNet() {};

        int read_model(std::string param_path = "data/ch_recv4.ncnn.param",
            std::string bin_path = "data/ch_recv4.ncnn.bin",
            std::string key_path = "data/dict_chi_sim.txt", bool use_gpu = false);

        int read_keys(std::string key_path);


        bool detect(cv::Mat& src, TextLine& result);

        bool detect(std::vector<cv::Mat>& src, std::vector<TextLine>& results);

    private:
        TextLine scoreToTextLine(const std::vector<float>& outputData, int h, int w);

    private:
        ncnn::Net net;
        const int dstHeight = 48;
        const int dstWidth = 320;
        const float meanValues[3] = { 127.5, 127.5, 127.5 };
        const float normValues[3] = { 1.0 / 127.5, 1.0 / 127.5, 1.0 / 127.5 };

        std::vector<std::string> keys;
    };
}

#endif //__OCR_DBNET_H__

类实现:

#include "crnn_net.h"

namespace NCNNOCR
{

    template<class ForwardIterator>
    inline static size_t argmax(ForwardIterator first, ForwardIterator last)
    {
        return std::distance(first, std::max_element(first, last));
    }

    int CrnnNet::read_model(std::string param_path, std::string bin_path,
        std::string key_path, bool use_gpu)
    {
        ncnn::set_cpu_powersave(2);
        ncnn::set_omp_num_threads(ncnn::get_big_cpu_count());

        net.opt = ncnn::Option();

#if NCNN_VULKAN
        net.opt.use_vulkan_compute = use_gpu;
#endif

        net.opt.num_threads = ncnn::get_big_cpu_count();

        int rp = net.load_param(param_path.c_str());
        int rb = net.load_model(bin_path.c_str());

        int rk = read_keys(key_path);

        if (rp == 0 || rb == 0 || rk == 0)
        {
            return false;
        }

        return true;
    }

    int CrnnNet::read_keys(std::string key_path)
    {
        std::ifstream in(key_path.c_str());
        std::string line;
        if (in)
        {
            while (getline(in, line))
            {// line中不包括每行的换行符
                keys.push_back(line);
            }
        }
        else
        {
            printf("The keys.txt file was not found\n");
        }
        keys.insert(keys.begin(), "#");
        keys.emplace_back(" ");

        return keys.size();
    };

    TextLine CrnnNet::scoreToTextLine(const std::vector<float>& outputData, int h, int w)
    {
        int keySize = keys.size();
        std::string strRes;
        std::vector<float> scores;
        int lastIndex = -1;
        int maxIndex;
        float maxValue;

        for (int i = 0; i < h; i++)
        {
            maxIndex = 0;
            maxValue = -1000.f;

            maxIndex = int(argmax(outputData.begin() + i * w, outputData.begin() + i * w + w));
            maxValue = float(*std::max_element(outputData.begin() + i * w, outputData.begin() + i * w + w)); // / partition;

            if (maxIndex > 0 && maxIndex < keySize && (!(maxIndex == lastIndex)))
            {
                /* std::cout << maxIndex << std::endl;*/
                scores.emplace_back(maxValue);
                //std::cout << keys[maxIndex] << std::endl;
                strRes.append(keys[maxIndex]);
            }
            lastIndex = maxIndex;
        }
        return { strRes, scores };
    }


    bool CrnnNet::detect(cv::Mat& src, TextLine& result)
    {
        int resized_w = 0;
        float ratio = src.cols / float(src.rows);


        resized_w = ceil(dstHeight * ratio);

        cv::Size tmp = cv::Size(resized_w, dstHeight);

        ncnn::Mat input = ncnn::Mat::from_pixels_resize(src.data, ncnn::Mat::PIXEL_BGR2RGB,
            src.cols, src.rows, tmp.width, tmp.height);

        input.substract_mean_normalize(meanValues, normValues);
        ncnn::Extractor extractor = net.create_extractor();
        extractor.input("in0", input);
        ncnn::Mat out;
        extractor.extract("out0", out);
        float* floatArray = (float*)out.data;
        std::vector<float> outputData(floatArray, floatArray + out.h * out.w);
        result = scoreToTextLine(outputData, out.h, out.w);

        return true;
    }

    bool CrnnNet::detect(std::vector<cv::Mat>& src,
        std::vector<TextLine>& results)
    {
        int sizeLen = src.size();
        // results.resize(sizeLen);
        for (size_t i = 0; i < sizeLen; i++)
        {
            TextLine textline;
            if (detect(src[i], textline))
            {
                results.emplace_back(textline);
            }
            else
            {
                return false;
            }
        }
        return true;
    }

    CrnnNet::CrnnNet()
    {

    }
}

2.在图像画中文

识别后,想要比对识别的结果,可以把文字画到当前图像,但OpenCV没有提供画中文的方法,甩以要自己写一个画中文的方法:

#include "put_text.h"

void get_string_size(HDC hDC, const char* str, int* w, int* h)
{
	SIZE size;
	GetTextExtentPoint32A(hDC, str, strlen(str), &size);
	if (w != 0) *w = size.cx;
	if (h != 0) *h = size.cy;
}

void put_text_ch(Mat &dst, const char* str, Point org, Scalar color, int fontSize, const char* fn, bool italic, bool underline)
{
	CV_Assert(dst.data != 0 && (dst.channels() == 1 || dst.channels() == 3));

	int x, y, r, b;
	if (org.x > dst.cols || org.y > dst.rows) return;
	x = org.x < 0 ? -org.x : 0;
	y = org.y < 0 ? -org.y : 0;

	LOGFONTA lf;
	lf.lfHeight = -fontSize;
	lf.lfWidth = 0;
	lf.lfEscapement = 0;
	lf.lfOrientation = 0;
	lf.lfWeight = 5;
	lf.lfItalic = italic;   //斜体
	lf.lfUnderline = underline; //下划线
	lf.lfStrikeOut = 0;
	lf.lfCharSet = DEFAULT_CHARSET;
	lf.lfOutPrecision = 0;
	lf.lfClipPrecision = 0;
	lf.lfQuality = PROOF_QUALITY;
	lf.lfPitchAndFamily = 0;
	strcpy_s(lf.lfFaceName, fn);

	HFONT hf = CreateFontIndirectA(&lf);
	HDC hDC = CreateCompatibleDC(0);
	HFONT hOldFont = (HFONT)SelectObject(hDC, hf);

	int strBaseW = 0, strBaseH = 0;
	int singleRow = 0;
	char buf[1 << 12];
	strcpy_s(buf, str);
	char *bufT[1 << 12];  // 这个用于分隔字符串后剩余的字符,可能会超出。
						  //处理多行
	{
		int nnh = 0;
		int cw, ch;

		const char* ln = strtok_s(buf, "\n", bufT);
		while (ln != 0)
		{
			get_string_size(hDC, ln, &cw, &ch);
			strBaseW = max(strBaseW, cw);
			strBaseH = max(strBaseH, ch);

			ln = strtok_s(0, "\n", bufT);
			nnh++;
		}
		singleRow = strBaseH;
		strBaseH *= nnh;
	}

	if (org.x + strBaseW < 0 || org.y + strBaseH < 0)
	{
		SelectObject(hDC, hOldFont);
		DeleteObject(hf);
		DeleteObject(hDC);
		return;
	}

	r = org.x + strBaseW > dst.cols ? dst.cols - org.x - 1 : strBaseW - 1;
	b = org.y + strBaseH > dst.rows ? dst.rows - org.y - 1 : strBaseH - 1;
	org.x = org.x < 0 ? 0 : org.x;
	org.y = org.y < 0 ? 0 : org.y;

	BITMAPINFO bmp = { 0 };
	BITMAPINFOHEADER& bih = bmp.bmiHeader;
	int strDrawLineStep = strBaseW * 3 % 4 == 0 ? strBaseW * 3 : (strBaseW * 3 + 4 - ((strBaseW * 3) % 4));

	bih.biSize = sizeof(BITMAPINFOHEADER);
	bih.biWidth = strBaseW;
	bih.biHeight = strBaseH;
	bih.biPlanes = 1;
	bih.biBitCount = 24;
	bih.biCompression = BI_RGB;
	bih.biSizeImage = strBaseH * strDrawLineStep;
	bih.biClrUsed = 0;
	bih.biClrImportant = 0;

	void* pDibData = 0;
	HBITMAP hBmp = CreateDIBSection(hDC, &bmp, DIB_RGB_COLORS, &pDibData, 0, 0);

	CV_Assert(pDibData != 0);
	HBITMAP hOldBmp = (HBITMAP)SelectObject(hDC, hBmp);

	//color.val[2], color.val[1], color.val[0]
	SetTextColor(hDC, RGB(255, 255, 255));
	SetBkColor(hDC, 0);
	//SetStretchBltMode(hDC, COLORONCOLOR);

	strcpy_s(buf, str);
	const char* ln = strtok_s(buf, "\n", bufT);
	int outTextY = 0;
	while (ln != 0)
	{
		TextOutA(hDC, 0, outTextY, ln, strlen(ln));
		outTextY += singleRow;
		ln = strtok_s(0, "\n", bufT);
	}
	uchar* dstData = (uchar*)dst.data;
	int dstStep = dst.step / sizeof(dstData[0]);
	unsigned char* pImg = (unsigned char*)dst.data + org.x * dst.channels() + org.y * dstStep;
	unsigned char* pStr = (unsigned char*)pDibData + x * 3;
	for (int tty = y; tty <= b; ++tty)
	{
		unsigned char* subImg = pImg + (tty - y) * dstStep;
		unsigned char* subStr = pStr + (strBaseH - tty - 1) * strDrawLineStep;
		for (int ttx = x; ttx <= r; ++ttx)
		{
			for (int n = 0; n < dst.channels(); ++n) {
				double vtxt = subStr[n] / 255.0;
				int cvv = vtxt * color.val[n] + (1 - vtxt) * subImg[n];
				subImg[n] = cvv > 255 ? 255 : (cvv < 0 ? 0 : cvv);
			}

			subStr += 3;
			subImg += dst.channels();
		}
	}

	SelectObject(hDC, hOldBmp);
	SelectObject(hDC, hOldFont);
	DeleteObject(hf);
	DeleteObject(hBmp);
	DeleteDC(hDC);
}

3.字符转换

识别的字符属于UTF8,在windows下,要转成ASCII才能正常显示不乱码,在C++中,可以使用标准库中的一些函数来处理字符编码的转换,但需要注意UTF-8和ASCII字符编码之间的差异。因为UTF-8是一种更广泛支持字符的编码方式,所以在进行转换时,需要确保要转换的文本仅包含ASCII字符。

#include "EncodeConversion.h"
#include <Windows.h>

//utf8 转 Unicode
extern std::wstring Utf8ToUnicode(const std::string& utf8string)
{
	int widesize = ::MultiByteToWideChar(CP_UTF8, 0, utf8string.c_str(), -1, NULL, 0);
	if (widesize == ERROR_NO_UNICODE_TRANSLATION)
	{
		throw std::exception("Invalid UTF-8 sequence.");
	}
	if (widesize == 0)
	{
		throw std::exception("Error in conversion.");
	}

	std::vector<wchar_t> resultstring(widesize);

	int convresult = ::MultiByteToWideChar(CP_UTF8, 0, utf8string.c_str(), -1, &resultstring[0], widesize);

	if (convresult != widesize)
	{
		throw std::exception("La falla!");
	}

	return std::wstring(&resultstring[0]);
}

//unicode 转为 ascii
extern std::string WideByteToAcsi(std::wstring& wstrcode)
{
	int asciisize = ::WideCharToMultiByte(CP_OEMCP, 0, wstrcode.c_str(), -1, NULL, 0, NULL, NULL);
	if (asciisize == ERROR_NO_UNICODE_TRANSLATION)
	{
		throw std::exception("Invalid UTF-8 sequence.");
	}
	if (asciisize == 0)
	{
		throw std::exception("Error in conversion.");
	}
	std::vector<char> resultstring(asciisize);
	int convresult = ::WideCharToMultiByte(CP_OEMCP, 0, wstrcode.c_str(), -1, &resultstring[0], asciisize, NULL, NULL);

	if (convresult != asciisize)
	{
		throw std::exception("La falla!");
	}

	return std::string(&resultstring[0]);
}

//utf-8 转 ascii
extern std::string UTF8ToASCII(std::string& strUtf8Code)
{
	std::string strRet("");
	//先把 utf8 转为 unicode
	std::wstring wstr = Utf8ToUnicode(strUtf8Code);
	//最后把 unicode 转为 ascii
	strRet = WideByteToAcsi(wstr);
	return strRet;
}

//ascii 转 Unicode
extern std::wstring AcsiToWideByte(std::string& strascii)
{
	int widesize = MultiByteToWideChar(CP_ACP, 0, (char*)strascii.c_str(), -1, NULL, 0);
	if (widesize == ERROR_NO_UNICODE_TRANSLATION)
	{
		throw std::exception("Invalid UTF-8 sequence.");
	}
	if (widesize == 0)
	{
		throw std::exception("Error in conversion.");
	}
	std::vector<wchar_t> resultstring(widesize);
	int convresult = MultiByteToWideChar(CP_ACP, 0, (char*)strascii.c_str(), -1, &resultstring[0], widesize);


	if (convresult != widesize)
	{
		throw std::exception("La falla!");
	}

	return std::wstring(&resultstring[0]);
}

//Unicode 转 Utf8
extern std::string UnicodeToUtf8(const std::wstring& widestring)
{
	int utf8size = ::WideCharToMultiByte(CP_UTF8, 0, widestring.c_str(), -1, NULL, 0, NULL, NULL);
	if (utf8size == 0)
	{
		throw std::exception("Error in conversion.");
	}

	std::vector<char> resultstring(utf8size);

	int convresult = ::WideCharToMultiByte(CP_UTF8, 0, widestring.c_str(), -1, &resultstring[0], utf8size, NULL, NULL);

	if (convresult != utf8size)
	{
		throw std::exception("La falla!");
	}

	return std::string(&resultstring[0]);
}

//ascii 转 Utf8
extern std::string ASCIIToUTF8(std::string& strAsciiCode)
{
	std::string strRet("");
	//先把 ascii 转为 unicode
	std::wstring wstr = AcsiToWideByte(strAsciiCode);
	//最后把 unicode 转为 utf8
	strRet = UnicodeToUtf8(wstr);
	return strRet;
}

三、整体测试

#include <iostream>
#include "crnn_net.h"
#include "db_net.h"
#include "tools.h"
#include "put_text.h"
#include "EncodeConversion.h"

int main() 
{
    NCNNOCR::DbNet det_net;
    NCNNOCR::CrnnNet rec_net;
    
    rec_net.read_model();
    det_net.read_model();
    cv::Mat img = cv::imread("235.jpg");
    if (img.empty())
    {
        std::cout << "empty" << std::endl;
        return 0;
    }
    cv::Mat drawImg = img.clone();
    std::vector< NCNNOCR::TextBox> boxResult;
    std::vector< NCNNOCR::TextLine> recResult;
    det_net.detect(img, boxResult,2560);
    recResult.resize(boxResult.size());

    for (size_t i = 0; i < boxResult.size(); i++) 
    {
        cv::Mat partImg = NCNNOCR::getRotateCropImage(img, boxResult[i].boxPoint);
        rec_net.detect(partImg, recResult[i]);
        cv::polylines(drawImg, boxResult[i].boxPoint, true, cv::Scalar(0,0,255),4);

        std::string text = UTF8ToASCII(recResult.at(i).text);
        std::cout << text << std::endl;
        if (text.empty())
        {
            continue;
        }
        put_text_ch(drawImg, text.c_str(), boxResult[i].boxPoint[0], cv::Scalar(0, 0, 255), 80);
    }

    cv::namedWindow("result", 0);
    cv::imshow("result", drawImg);
    cv::waitKey();

    return 0;
}

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/208311.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

python简单进阶之web框架:fastapi使用教程

原文&#xff1a;python简单进阶之web框架&#xff1a;fastapi使用教程 - 知乎 这是简单进阶教程系列第四篇&#xff0c;本系列文章主要介绍那些可以很快上手的进阶库。 我其实学过一段时间Django框架&#xff0c;但是半途而废了&#xff0c;我觉得可能还是简单一点的框架比较适…

nc 传输目录

使用nc命令进行目录传输 接收方发送方 使用TCP连接进行文件传输&#xff0c;不提供数据加密或身份验证 接收方 -ip 192.168.1.200 nc -l -p 2222 | tar -xvf -发送方 发送目录 lotus tar -cf - lotus | nc 192.168.1.200 2222

Python+Requests对图片验证码的处理

Requests对图片验证码的处理 在web端的登录接口经常会有图片验证码的输入&#xff0c;而且每次登录时图片验证码都是随机的&#xff1b;当通过request做接口登录的时候要对图片验证码进行识别出图片中的字段&#xff0c;然后再登录接口中使用&#xff1b; 通过request对图片验…

LINUX 嵌入式C编程--信号编程

基本概念 信号是事件发生时对进程的通知机制&#xff0c;也可以把它称为软件中断。信号与硬件中断的相似之处在于能够打断程序当前执行的正常流程&#xff0c;其实是在软件层次上对中断机制的一种模拟。信号提供了一种处理异步事件的方法。 信号目的 **信号的目的是用来通信…

shell编程系列(9)-使用cut选择列

文章目录 前言使用cut选择列选择特定的列 结语 前言 前面的文章介绍了sed命令&#xff0c;sed可以帮我们处理文本列&#xff0c;这边文章介绍cut命令&#xff0c;cut命令可以帮我们选择想要的列&#xff0c;在文本处理时候结合sed命令&#xff0c;就可以精准定位了。 cut命令是…

HuggingFace学习笔记--Model的使用

1--Model介绍 Transformer的 model 一般可以分为&#xff1a;编码器类型&#xff08;自编码&#xff09;、解码器类型&#xff08;自回归&#xff09;和编码器解码器类型&#xff08;序列到序列&#xff09;&#xff1b; Model Head&#xff08;任务头&#xff09;是在base模型…

java正则表达式字母开头后面跟12位数字

字母开头后面跟12位数字 ^[A-Za-z]\d{12}$ 验证&#xff1a; 验证工具地址&#xff1a; Java正则表达式测试

学习感悟一己之言

学习感悟一己之言 学习上克服困难实际上是克服心理上或认识上的障碍的过程。所谓的理解&#xff0c;就是化陌生为熟悉。看不懂&#xff0c;一方面是因为接触的材料太陌生&#xff0c;即远离你当前的背景知识&#xff1b;另一方面是材料或讲述者的描述刻画不准确或晦涩不当。有了…

修改sublime配置让其显示文件编码格式

1、下载sublime并安装 2、点击菜单栏Preferences&#xff0c;然后在Preferences里面点击Setting 3、然后在跳出来的窗口添加&#xff1a; "show_enconding":true, 4、随便打开一个文件就可以在底部查看文件编码格式&#xff1a;

openbabel 安装 生成指纹方法

今日踩坑小结&#xff1a; openbabel 安装&#xff1a; 可以装&#xff0c;但是得在 Linux 环境下&#xff0c;win 环境装会报错&#xff08;安装不会报错&#xff0c;但是生成指纹的时候会&#xff09; 指纹&#xff1a; 在下面这个链接里&#xff0c;官方给出了命令行调用 o…

一篇博客带你认识泛型

目录 泛型类&#xff08;Generic Class&#xff09;: 泛型方法&#xff08;Generic Method&#xff09;: Java 中的泛型是一种编程机制&#xff0c;允许你编写可以与多种数据类型一起工作的代码&#xff0c;同时提供编译时类型检查以确保类型的安全性。泛型的主要目的是提高代…

外贸获客的几种正确打开方式,还不快来GET!

做外贸还在愁没客户&#xff1f;作为外贸人&#xff0c;开发客户是我们的重要工作内容&#xff0c;想要高效地开发客户&#xff0c;首先就要知道外贸获客的方法有哪些&#xff0c;当下最主流的外贸获客渠道分为线下和线上两种方式&#xff0c;今天东哥就介绍几种获客渠道&#…

YOLOv5项目实战(5)— 算法模型优化和服务器部署

前言:Hello大家好,我是小哥谈。近期,作者所负责项目中的算法模型检测存在很多误报情况,为了减少这种误报情况,作者一直在不断优化算法模型。鉴于此,本节课就给大家详细介绍一下实际工作场景中如何去优化算法模型和进行部署,另外为了方便大家进行模型训练,作者在文章中提…

流量内存cpu使用率使用工具

类似360工具球的工具 我提供了夸克下载喜欢的朋友可以直接下载使用 我用夸克网盘分享了「TrafficMonitor」&#xff0c;点击链接即可保存。打开「夸克APP」&#xff0c;无需下载在线播放视频&#xff0c;畅享原画5倍速&#xff0c;支持电视投屏。 链接&#xff1a;https://pan…

wpf devexpress 使用IDataErrorInfo实现input验证

此处下载源码 当form初始化显示&#xff0c;Register按钮应该启动和没有输入错误应该显示。如果用户点击注册按钮在特定的输入无效数据&#xff0c;form将显示输入错误和禁用的注册按钮。实现逻辑在标准的IDataErrorInfo接口。请查阅IDataErrorInfo接口&#xff08;System.Com…

微机原理——定时器学习1

目录 定时类型 8253内部结构框图 8253命令字 六种工作方式及输出波形 计数初值的计算与装入 8253的初始化 定时类型 可编程定时器8253&#xff1a;&#xff08;内部采用的是16位 减法计数器&#xff09; 8253内部结构框图 8253命令字 8253有三个命令字&#xff1a;方式命…

【广州华锐视点】VR云端看车:一键穿越!VR技术让你在家就能试驾各种豪车!

随着科技的不断发展&#xff0c;虚拟现实&#xff08;VR&#xff09;技术已经逐渐走进我们的生活。在汽车行业&#xff0c;VR线上看车已经成为了一种全新的购车体验。通过这种创新的方式&#xff0c;消费者可以在不出门的情况下&#xff0c;全方位地了解汽车的外观、内饰和性能…

PGSQL(PostgreSQL)数据库安装教程

安装包下载 下载地址 下载后点击exe安装包 设置的data存储路径 设置密码 设置端口 安装完毕&#xff0c;配置PGSQL的ip远程连接&#xff0c;pg_hba.conf&#xff0c;postgresql.conf&#xff0c;需要更改这两个文件 pg_hba.conf 最后增加一行 host all all …

1+x网络系统建设与运维(中级)-练习题

一.给设备重命名 同理可得&#xff0c;所有交换机和路由器都用一下命令配置 <Huawei>sys [Huawei]sysn LSW1 二.配置VLAN LSW1&#xff1a; [LSW1]vlan batch 10 20 [LSW1]int e0/0/1 [LSW1-Ethernet0/0/1]port link-type access [LSW1-Ethernet0/0/1]port default vlan…

P1012 [NOIP1998 提高组] 拼数( 字典序 )

字典序&#xff1a; 在字典中&#xff0c;单词是按照首字母在字母表中的顺序进行排列的 比如 alpha 在 beta 之前。 1.而第一个字母相同时&#xff0c;会 去比较两个单词的第二个字母在字母表中的顺序&#xff0c;比如 account 在 advanced 之前&#xff0c;以此类推。 2. 若…