LOGO 首页 OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 技术文档 其他文档  
 
网站管理员

高性能百度OCR ONNX Runtime C#实现

freeflydom
2026年6月2日 9:2 本文热度 153
RapidOCRSharpOnnx 是一个基于OpenCV与ONNX Runtime 实现的PaddleOCR C#开源推理库,参考了RapidOCR 的实现,重新使用C#实现,并做了大量性能优化与架构重新设计,方便多种ONNX Runtime 执行提供程序部署

特性

  • 不依赖于具体的模型,直接导入需要的PP-OCR模型,识别指定的语言,PP-OCRv5模型支持识别106种语言,只需要将PP-OCRv5模型导出为onnx格式,就可以离线运行
  • 支持的ONNX Runtime 执行提供程序:Execution Provider CPU, CUDA / TensorRT, OpenVINO, CoreML, DirectML
  • 支持批量识别,并提供异步执行性能优化,大幅提高批量执行性能
  • 使用OpenCVSharp4 进行图像处理操作
  • 绘制识别结果图片使用SkiaSharp
  • PP-OCR 版本支持:PP-OCRv5, PP-OCRv4
  • 推理引擎:ONNX Runtime 是一个跨平台的机器学习模型加速器

OCR 示例

 

 

 

RapidOCRSharpOnnx使用

1 导出模型为onnx格式

如何转换PP-OCR模型为onnx格式,可以参考PP-OCR官网Obtaining ONNX Models, 或者直接从RapidOCR的魔塔社区下载Model List.

2 安装RapidOCRSharpOnnx组件并加载模型

Install Nuget packages RapidOCRSharpOnnx, OnnxRuntime, OpenCvSharp4.runtime

 

CPU 推理

dotnet add package RapidOCRSharpOnnx
dotnet add package OpenCvSharp4.runtime.win
dotnet add package Microsoft.ML.OnnxRuntime
using RapidOCRSharp ocr = new RapidOCRSharp(new ExecutionProviderCPU(new OcrConfig(detectPath, recPath, LangRec.CH, OCRVersion.PPOCRV5, clsMobilePath)));

CoreML 推理

dotnet add package RapidOCRSharpOnnx
dotnet add package OpenCvSharp4.runtime.osx.10.15-x64
dotnet add package Microsoft.ML.OnnxRuntime
using RapidOCRSharp ocr = new RapidOCRSharp(new ExecutionProviderCoreML(new OcrConfig(detectPath, recPath, LangRec.CH, OCRVersion.PPOCRV5, clsMobilePath)));

CUDA/TensorRT 推理

dotnet add package RapidOCRSharpOnnx
dotnet add package OpenCvSharp4.runtime.win
dotnet add package Microsoft.ML.OnnxRuntime.Gpu.Windows
using RapidOCRSharp ocr = new RapidOCRSharp(new ExecutionProviderCUDA(new OcrConfig(detectPath, recogPath, LangRec.CH, OCRVersion.PPOCRV5, clsPath), deviceId));
using RapidOCRSharp ocr = new RapidOCRSharp(new ExecutionProviderTensorRT(new OcrConfig(detectPath, recogPath, LangRec.CH, OCRVersion.PPOCRV5, clsPath), deviceId));

DirectML 推理

dotnet add package RapidOCRSharpOnnx
dotnet add package OpenCvSharp4.runtime.win
dotnet add package Microsoft.ML.OnnxRuntime.DirectML
using RapidOCRSharp ocr = new RapidOCRSharp(new ExecutionProviderDirectML(new OcrConfig(detectPath, recogPath, LangRec.EN, OCRVersion.PPOCRV5, clsPath), deviceId));

OpenVINO 推理

 
dotnet add package RapidOCRSharpOnnx
dotnet add package OpenCvSharp4.runtime.win
dotnet add package Intel.ML.OnnxRuntime.OpenVino
using RapidOCRSharp ocr = new RapidOCRSharp(new ExecutionProviderOpenVINO(new OcrConfig(detectPath, recogPath, LangRec.EN, OCRVersion.PPOCRV5, clsPath), IntelDeviceType.NPU));

 

基本的API使用

using RapidOCRSharp ocr = new RapidOCRSharp(new ExecutionProviderDirectML(new OcrConfig(detectPath, recogPath, LangRec.EN, OCRVersion.PPOCRV5, clsPath), _deviceId));
string savePath = $"res_{Path.GetFileName(imgPath)}";
var result = ocr.RecognizeText(imgPath, savePath);
Console.WriteLine($"result: {result.ToString()}");

批量识别图片

 using RapidOCRSharp ocr = new RapidOCRSharp(new ExecutionProviderDirectML(new OcrConfig(detectPath, recogPath, LangRec.CH, OCRVersion.PPOCRV5, clsPath), _deviceId));
 var list = Directory.GetFiles(@"C:\code\model\OCRTestImages");
 Stopwatch sw = new Stopwatch();
 sw.Start();
 var resPath = ocr.BatchParallelAsync(list.ToList(), saveDir, receiveAction: ReceiveResult);
 sw.Stop();
 Console.WriteLine($"BatchAsync Time: {sw.ElapsedMilliseconds} ms");
private static void ReceiveResult(OcrBatchResult batchResult)
{
    Console.WriteLine(batchResult.ToString());
    Console.WriteLine("------------------------------------------------------------");
}

批量识别使用Foreach API

        private static async Task TestBatchForeachAsync()
        {
            string detectPath = @"D:\code\RapidOCRSharpOnnx\RapidOCRSharpOnnx.TestCommon\Models\ch_PP-OCRv5_det_mobile.onnx";
            string recogPath = @"D:\code\RapidOCRSharpOnnx\RapidOCRSharpOnnx.TestCommon\Models\ch_PP-OCRv5_rec_mobile.onnx";
            string clsPath = @"D:\code\RapidOCRSharpOnnx\RapidOCRSharpOnnx.TestCommon\Models\ch_PP-LCNet_x0_25_textline_ori_cls_mobile.onnx";
            using RapidOCRSharp ocr = new RapidOCRSharp(new ExecutionProviderDirectML(new OcrConfig(detectPath, recogPath, LangRec.CH, OCRVersion.PPOCRV5, clsPath), _deviceId));
            var list = Directory.GetFiles(@"D:\code\model\OCRTestImages");
            var res = ocr.BatchForeachAsync(list.ToList(), @"D:\code\model\OCRTestImagesResults");
            await foreach (var item in res)
            {
                Console.WriteLine(item.TextBlocks);
            }
        }

 

性能测试

OCR组件库性能对比测试 CPU推理测试

OCR libraryVersionlanguageInference Engine
PaddleSharp3.0.1Paddle Inference C API .NET bindingSdcb.PaddleInference
PaddleOCR3.5.0pythonpaddlepaddle
RapidOCR3.8.1pythonopenvino
RapidOCRSharpOnnx1.0.0C#Intel.ML.OnnxRuntime.OpenVino

 

测试电脑

Windows 11 Pro OS Version 25H2

CPU: Intel Core Ultra 9 285k 3.7GHz

内存:DDR5 128GB speed 4400MT/s

硬盘:SSD 2TB

 

测试数据

图片: 60 张图片 (图片大小: 1180x92)

PP-OCR 模型: ch_PP-OCRv5_det_mobile, ch_PP-OCRv5_rec_mobile, ch_PP-LCNet_x0_25_textline_ori_cls_mobile

PaddleSharp 测试结果

CPU 推理时间 : 48.1769278s

 

PaddleOCR 测试结果

CPU 推理时间 : 62.6685s

 

RapidOCR 测试结果

CPU 推理时间 : 17.9634s

 

RapidOCRSharpOnnx 测试结果

CPU 推理时间 : 9.2447s

 

性能测试结果

OCR libraryVersionlanguageInference EngineElapsed Time
PaddleSharp3.0.1Paddle Inference C API .NET bindingSdcb.PaddleInference.runtime.win64.mkl version 3.1.0.54 CPU48.1769s
PaddleOCR3.5.0pythonpaddlepaddle version 3.2.0 CPU62.6685s
RapidOCR3.8.1pythonopenvino version 2026.1.0 21367 CPU17.9634s
RapidOCRSharpOnnx1.0.0C#Intel.ML.OnnxRuntime.OpenVino CPU version 1.24.19.2447s

转自https://www.cnblogs.com/luoht/p/20026520


该文章在 2026/6/2 9:03:04 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2026 ClickSun All Rights Reserved  粤ICP备13012886号-9  粤公网安备44030602007207号