PaddleOCRSharp是基于PaddleOCR的C++代码修改并封装的.NET工具类库,支持文本识别、文本检测、基于文本检测结果的统计分析的表格识别功能。
PaddleOCRSharp封装极其简化,实际调用仅几行代码,极大的方便了中下游开发者的使用和降低了PaddleOCR的使用入门级别,同时提供不同的.NET框架使用,方便各个行业应用开发与部署。Nuget包即装即用,可以离线部署,不需要网络就可以识别的高精度中英文OCR。
目录
跑通了例程代码并根据建议进行了些许优化,代码中注释详细,自行阅读。
该项目只支持x64cpu编译
一、准备环境
1、先创建一个窗体项目,添加一个按钮(我的是VS2017)
2、项目-属性-生成。配置和图中一样即可
二、nuget包安装:
三、代码如下
using PaddleOCRSharp;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace PaddleOCRSharp1._0
{
public partial class Form1 : Form
{
//程序全局初始化一次即可,不必每次识别都初始化,容易报错。
// 初始化OCR模型配置,默认中英文V3模型
OCRModelConfig config = null;
// 初始化OCR参数
OCRParameter oCRParameter = new OCRParameter();
// 创建一个OCR识别结果对象
OCRResult ocrResult = new OCRResult();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// 创建对象,设置文件过滤器
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 bitmap = new Bitmap(new MemoryStream(imagebyte));
// 创建PaddleOCR引擎,使用之前初始化的配置和参数
PaddleOCREngine engine = new PaddleOCREngine(config, oCRParameter);
// 使用PaddleOCR引擎对图像进行文字识别
// OCR识别结果会保存在ocrResult对象中
ocrResult = engine.DetectText(bitmap);
// 如果识别结果不为空,显示识别出的文字内容
if (ocrResult != null)
{
// 弹出一个消息框,显示识别出的文字内容
MessageBox.Show(ocrResult.Text, "识别结果");
}
}
}
}
PaddleOCRSharp的github: