RapidOcrNet文字识别OCR
nuget: RapidOcrNet NuGet Gallery | RapidOcrNet 3.0.0
测试v6(6秒) 版本的没有v5版本(1秒)的快。
另外:中文的ppocrv5_dict 或者ppocrv6_dict 需要费劲找到。这里找到:ppocrv6_dict.txt 全部: PaddleOCR/ppocr/utils/dict
模型: RapidOCR · 模型库
using RapidOcrNet;
using System;
using System.IO;
using System.Linq;
using SkiaSharp;
using System.Diagnostics;
namespace OcrTest
{
public class OCRService
{
private static readonly RapidOcr _rapidOcr = new RapidOcr();
public string RecognizeText(string imagePath)
{
if (!File.Exists(imagePath))
{
return "[错误] 图片不存在";
}
using var ocr = new RapidOcr();
using var sessionOptions = RapidOcr.GetDefaultSessionOptions(); // sensible defaults: ORT_ENABLE_EXTENDED
try
{
sessionOptions.AppendExecutionProvider_CUDA();
}
// NVIDIA GPU
catch
{
sessionOptions.AppendExecutionProvider_CPU();
}
//ocr.InitModels(RapidOcrModelSet.PPOCRv6Small); // PP-OCRv6, balanced size/accuracy
//ocr.InitModels(RapidOcrModelSet.PPOCRv6Medium,sessionOptions);
//ocr.InitModels(); // loads bundled PP-OCRv5 latin models
ocr.InitModels(
detPath: "models/v5/ch_PP-OCRv5_mobile_det.onnx",
clsPath: "models/v5/ch_PP-LCNet_x0_25_textline_ori_cls_mobile.onnx",
recPath: "models/v5/ch_PP-OCRv5_rec_mobile.onnx",
keysPath: "models/v5/ppocrv5_ch_dict.txt");
//OcrResult result = ocr.Detect(imagePath, RapidOcrOptions.PPOCRv6);
OcrResult result = ocr.Detect(imagePath, RapidOcrOptions.Default);
Debug.Print(result.StrRes); // full recognized text, one line per block
foreach (var block in result.TextBlocks)
{
Debug.Print($"{block.Text} (avg score: {block.CharScores!.Average():F2})");
// return block.Text;
}
if (result == null || result.TextBlocks == null || !result.TextBlocks.Any())
{
return "[未识别到文字]";
}
return string.Join(Environment.NewLine, result.TextBlocks.Select(b => b.Text));
}
}
}
fffffffffffffffff
test red font.

浙公网安备 33010602011771号