Marking.Template/App_Code/OcrHelper.cs

66 lines
1.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using PaddleOCRSharp;
using System;
using System.Drawing;
public class OcrHelper
{
//private static TesseractEngine engine = new TesseractEngine("./ocr_models", "chi_sim");
//public static OCRResult GetOcrResult(Bitmap bitmap)
//{
// try
// {
// using var tempBitmap = new Bitmap(bitmap);
// var ocrResult = paddleOCREngine.DetectText(tempBitmap);
// return ocrResult;
// }
// catch (Exception ex)
// {
// ExceptionNotice.SendAsync(ex, "Ocr文字识别异常");
// throw;
// }
//}
private static string RootDirectory = EngineBase.GetRootDirectory().TrimEnd('\\') + "\\inference";
//建议程序全局初始化一次即可,不必每次识别都初始化,容易报错。
private static PaddleOCREngine paddleOCREngine = paddleOCREngine = new PaddleOCREngine(new OCRModelConfig()
{
det_infer = RootDirectory + "\\ch_PP-OCRv3_det_infer",
cls_infer= RootDirectory + "\\ch_ppocr_mobile_v2.0_cls_infer",
rec_infer = RootDirectory + "\\ch_PP-OCRv3_rec_infer",
keys = RootDirectory + "\\ppocr_keys.txt",
}, new OCRParameter());
public static OCRResult GetOcrResult(Bitmap bitmap)
{
try
{
using (var tempBitmap = new Bitmap(bitmap))
{
return paddleOCREngine.DetectText(tempBitmap);
}
}
catch (Exception ex)
{
throw;
}
}
public static OCRResult GetOcrResult(byte[] bytes)
{
try
{
var ocrResult = paddleOCREngine.DetectText(bytes);
return ocrResult;
}
catch (Exception ex)
{
throw;
}
}
}