OCR 示例RapidOCRSharpOnnx使用1 导出模型为onnx格式如何转换PP-OCR模型为onnx格式可以参考PP-OCR官网Obtaining ONNX Models, 或者直接从RapidOCR的魔塔社区下载Model List.2 安装RapidOCRSharpOnnx组件并加载模型Install Nuget packages RapidOCRSharpOnnx, OnnxRuntime, OpenCvSharp4.runtimeCPU 推理dotnet add package RapidOCRSharpOnnx dotnet add package OpenCvSharp4.runtime.win dotnet add package Microsoft.ML.OnnxRuntimeusing 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.OnnxRuntimeusing 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.Windowsusing 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.DirectMLusing 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.OpenVinousing 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 APIprivate 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 EnginePaddleSharp3.0.1Paddle Inference C API .NET bindingSdcb.PaddleInferencePaddleOCR3.5.0pythonpaddlepaddleRapidOCR3.8.1pythonopenvinoRapidOCRSharpOnnx1.0.0C#Intel.ML.OnnxRuntime.OpenVino测试电脑Windows 11 Pro OS Version 25H2CPU 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_mobilePaddleSharp 测试结果CPU 推理时间 : 48.1769278sPaddleOCR 测试结果CPU 推理时间 : 62.6685sRapidOCR 测试结果CPU 推理时间 : 17.9634sRapidOCRSharpOnnx 测试结果CPU 推理时间 : 9.2447s性能测试结果OCR libraryVersionlanguageInference EngineElapsed TimePaddleSharp3.0.1Paddle Inference C API .NET bindingSdcb.PaddleInference.runtime.win64.mkl version 3.1.0.54 CPU48.1769sPaddleOCR3.5.0pythonpaddlepaddle version 3.2.0 CPU62.6685sRapidOCR3.8.1pythonopenvino version 2026.1.0 21367 CPU