前言
大家有使用过.NET开发过OCR工具吗?今天给大家推荐一个基于百度飞桨封装的.NET版本OCR工具类库:PaddleOCRSharp。
OCR工具有什么用?
OCR(Optical Character Recognition)工具可以将图像或扫描文件中的文本内容转换成可编辑的文本格式。这项技术可以帮助人们快速准确地将扫描文件、图片中的文字提取出来,从而进行编辑、存储和分析。
百度飞桨PaddleOCR介绍
PaddleOCR旨在打造一套丰富、领先、且实用的OCR工具库,助力开发者训练出更好的模型,并应用落地。
项目支持的.NET版本
net35;net40;net45;net451;net452;net46;net461;net462;net47;net471;net472;net48;net481; netstandard2.0;netcoreapp3.1; net5.0;net6.0;net7.0;net8.0
项目源码
示例项目运行
PaddleOCRSharpDemo
设置启动项目
注意该示例项目只支持X64位程序。
.NET示例项目编译
注意:如果因框架编译问题无法编译,请修改PaddleOCRSharp\PaddleOCRSharp.csproj文件【或双击项目】,删除当前电脑环境没有的框架,只保留你想要的.NET框架。
<TargetFrameworks> net35;net40;net45;net451;net452;net46;net461;net462;net47;net471;net472;net48;net481; netstandard2.0;netcoreapp3.1; net5.0;net6.0;net7.0;net8.0 </TargetFrameworks>
如我的本地环境没有安装.net8,那就删除对应版本即可。
下载对应模型
OCR识别模型库支持官方所有的模型,也支持自己训练的模型。完全按照飞桨OCR接口搭桥。本项目部署自带的一种轻量版8.6M模型库、服务器版模型库(更准确,需要自行下载),可以自行更改模型库适用实际需求。
或者复制源码对应位置paddle-ocrsharp-dev\paddle-ocrsharp-dev\PaddleOCRSharp\PaddleOCRLib\inference
下面的模型到项目输出目录中:
将下载的模型放到对应文件项目目录下
/// <summary> /// PaddleOCR识别引擎对象初始化 /// </summary> /// <param name="config">模型配置对象,如果为空则按默认值</param> /// <param name="parameter">识别参数,为空均按缺省值</param> public PaddleOCREngine(OCRModelConfig config, OCRParameter parameter = null) : base() { CheckEnvironment(); if (parameter == null) parameter = new OCRParameter(); if (config == null) { string root= GetRootDirectory(); config = new OCRModelConfig(); string modelPathroot = root + @"\inference"; config.det_infer = modelPathroot + @"\ch_PP-OCRv4_det_infer"; config.cls_infer = modelPathroot + @"\ch_ppocr_mobile_v2.0_cls_infer"; config.rec_infer = modelPathroot + @"\ch_PP-OCRv4_rec_infer"; config.keys = modelPathroot + @"\ppocr_keys.txt"; } if (!Directory.Exists(config.det_infer)) throw new DirectoryNotFoundException(config.det_infer); if (!Directory.Exists(config.cls_infer)) throw new DirectoryNotFoundException(config.cls_infer); if (!Directory.Exists(config.rec_infer)) throw new DirectoryNotFoundException(config.rec_infer); if (!File.Exists(config.keys)) throw new FileNotFoundException(config.keys); Initialize(config.det_infer, config.cls_infer, config.rec_infer, config.keys, parameter); }
无法加载 DLL“PaddleOCR.dll”: 找不到指定的模块
将下面的dll复制到对应的输出目录中:
|--libiomp5md.dll //第三方引用库 |--mkldnn.dll //第三方引用库 |--mklml.dll //第三方引用库 |--opencv_world470.dll //第三方引用库 |--paddle_inference.dll //飞桨库 |--PaddleOCR.dll 本项目依赖VC++2017X64运行库,请检查机器上是否安装VC++依赖库。2.0.4及以上版本,免安装VC++2017X64运行库
项目运行截图
文件识别
截图识别
剪切板表格
项目源码地址
更多项目实用功能和特性欢迎前往项目开源地址查看👀,别忘了给项目一个Star支持💖。
优秀项目和框架精选
该项目已收录到C#/.NET/.NET Core优秀项目和框架精选中,关注优秀项目和框架精选能让你及时了解C#、.NET和.NET Core领域的最新动态和最佳实践,提高开发工作效率和质量。坑已挖,欢迎大家踊跃提交PR推荐或自荐(让优秀的项目和框架不被埋没🤞
)。
https://github.com/YSGStudyHards/DotNetGuide/blob/main/docs/DotNet/DotNetProjectPicks.md