PaddleOCRSharp,2022年,你来的晚了些,一款.NET离线使用的高精度OCR
一款免费且离线的.NET使用的OCR,爱你又恨你!恨你来的太晚了。
PaddleOCRSharp
本项目是一个基于百度飞桨[PaddleOCR](https://github.com/paddlepaddle/PaddleOCR)的C++代码修改并封装的.NET的工具类库。包含文本识别、文本检测、基于文本检测结果的统计分析的表格识别功能,同时针对小图识别不准的情况下,做了优化,提高识别准确率。包含总模型仅8.6M的超轻量级中文OCR,单模型支持中英文数字组合识别、竖排文本识别、长文本识别。同时支持多种文本检测。
项目封装极其简化,实际调用仅几行代码,极大的方便了中下游开发者的使用和降低了PaddleOCR的使用入门级别,同时提供不同的.NET框架使用,方便各个行业应用开发与部署。Nuget包即装即用,可以离线部署,不需要网络就可以识别的高精度中英文OCR。
本项目中PaddleOCR.dll文件是基于开源项目[PaddleOCR](https://github.com/paddlepaddle/PaddleOCR)的C++代码修改而成的C++动态库,基于opencv的x64编译而成的。
**本项目已经适配[PaddleOCR](https://github.com/paddlepaddle/PaddleOCR)最新版release2.5,并支持PP-OCRv3模型。**
**超轻量OCR系统PP-OCRv3:中英文、纯英文以及多语言场景精度再提升5% - 11%!**
如果使用v3模型,请设置OCR识别参数OCRParameter对象的属性rec_img_h:
rec_img_h=48
本项目只能在X64的CPU上编译和使用,因此不支持32位,暂不支持Linux平台,只能在avx指令集上的CPU上使用。
本项目目前支持以下NET框架:
net35;net40;net45;net451;net452;net46;net461;net462;net47;net471;net472;net48; netstandard2.0;netcoreapp3.1; net5.0;net6.0;
方便各个行业应用开发与部署。
C++示例代码
#include <iostream>
#include <Windows.h>
#include <tchar.h>
#include "string"
#include <include/Parameter.h>
#include <string.h>
using namespace std;
#pragma comment (lib,"PaddleOCR.lib")
extern "C" {
/// <summary>
/// PaddleOCREngine引擎初始化
/// </summary>
/// <param name="det_infer"></param>
/// <param name="cls_infer"></param>
/// <param name="rec_infer"></param>
/// <param name="keys"></param>
/// <param name="parameter"></param>
/// <returns></returns>
__declspec(dllimport) int* Initialize(char* det_infer, char* cls_infer, char* rec_infer, char* keys, OCRParameter parameter);
/// <summary>
/// 文本检测
/// </summary>
/// <param name="engine"></param>
/// <param name="imagefile"></param>
/// <param name="pOCRResult">返回结果</param>
/// <returns></returns>
__declspec(dllimport) int Detect(int* engine, char* imagefile, LpOCRResult* pOCRResult);
/// <summary>
/// 释放引擎对象
/// </summary>
/// <param name="engine"></param>
__declspec(dllimport) void FreeEngine(int* engine);
/// <summary>
/// 释放文本识别结果对象
/// </summary>
/// <param name="pOCRResult"></param>
__declspec(dllimport) void FreeDetectResult(LpOCRResult pOCRResult);
};
std::wstring string2wstring(const std::string& s)
{
int len;
int slength = (int)s.length() + 1;
len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0);
wchar_t* buf = new wchar_t[len];
MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len);
std::wstring r(buf);
delete[] buf;
return r;
}
int main()
{
LpOCRResult lpocrreult;
OCRParameter parameter;
/*parameter.enable_mkldnn = false;*/
char path[MAX_PATH];
GetCurrentDirectoryA(MAX_PATH, path);
string cls_infer(path);
cls_infer += "\\inference\\ch_ppocr_mobile_v2.0_cls_infer";
string rec_infer(path);
rec_infer += "\\inference\\ch_PP-OCRv2_rec_infer";
string det_infer(path);
det_infer += "\\inference\\ch_PP-OCRv2_det_infer";
string ocrkeys(path);
ocrkeys += "\\inference\\ppocr_keys.txt";
string imagefile(path);
imagefile += "\\test.jpg";
int* pEngine = Initialize(const_cast<char*>(det_infer.c_str()),
const_cast<char*>(cls_infer.c_str()),
const_cast<char*>(rec_infer.c_str()),
const_cast<char*>(ocrkeys.c_str()),
parameter);
int cout = Detect(pEngine, const_cast<char*>(imagefile.c_str()), &lpocrreult);
std::wcout.imbue(std::locale("chs"));
for (size_t i = 0; i < cout; i++)
{
wstring ss = (WCHAR*)(lpocrreult->pOCRText[i].ptext);
std::wcout << ss;
}
FreeDetectResult(lpocrreult);
FreeEngine(pEngine);
std::cin.get();
}
.NET示例代码
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
if (ofd.ShowDialog() != DialogResult.OK) return;
var imagebyte = File.ReadAllBytes(ofd.FileName);
Bitmap bitmap = new Bitmap(new MemoryStream(imagebyte));
OCRModelConfig config = null;
OCRParameter oCRParameter = null;
OCRResult ocrResult = new OCRResult();
using (PaddleOCREngine engine = new PaddleOCREngine(config, oCRParameter))
{
ocrResult = engine.DetectText(bmp);
}
if (ocrResult != null)
{
MessageBox.Show(ocrResult.Text,"识别结果");
}
微信公众号
PaddleOCRSharp项目地址:
码云:https://gitee.com/raoyutian/paddle-ocrsharp
github:https://github.com/raoyutian/PaddleOCRSharp
QQ群:318860399
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!