C# 识别图片文字(PaddleOCRSharp) 源码

//using PaddleOCRSharp;
//using System.Drawing;
 
public class PaddleOCREngineHandler
{//本文来自www.51dev.com
    public static PaddleOCREngine engine = CreateOCRParameter();
    public static PaddleOCREngine CreateOCRParameter()
    {
        OCRParameter oCRParameter = new OCRParameter();
        oCRParameter.numThread = 6;//预测并发线程数
        oCRParameter.Enable_mkldnn = 1;//web部署该值建议设置为0,否则出错,内存如果使用很大,建议该值也设置为0.
        oCRParameter.cls = 1; //是否执行文字方向分类;默认false
        oCRParameter.det = 1;//是否开启方向检测,用于检测识别180旋转
        oCRParameter.use_angle_cls = 1;//是否开启方向检测,用于检测识别180旋转
        oCRParameter.det_db_score_mode = 1;//是否使用多段线,即文字区域是用多段线还是用矩形,
        oCRParameter.UnClipRatio = 8.6f;
 
        oCRParameter.MaxSideLen = 960;
 
        OCRModelConfig config = null;
        PaddleOCREngine engine = new PaddleOCREngine(config, oCRParameter);
        return engine;
    }
    /// <summary>
    /// 读取 byte[]  
    /// </summary>
    /// <param name="imagebyte"></param>
    /// <returns></returns>
    public string OCR2Str(byte[] imagebyte)
    {
        OCRResult ocrResult = engine.DetectText(imagebyte);
        return ocrResult.Text;
    }
    /// <summary>
    /// 读取 图片地址
    /// </summary>
    /// <param name="imagebyte"></param>
    /// <returns></returns>
    public string OCR2Str(string imagefile)
    {
        OCRResult ocrResult = engine.DetectText(imagefile);
        return ocrResult.Text;
    }
    /// <summary>
    /// 读取 image
    /// </summary>
    /// <param name="imagebyte"></param>
    /// <returns></returns>
    public string OCR2Str(Image image)
    {
        OCRResult ocrResult = engine.DetectText(image);
        return ocrResult.Text;
    }
    /// <summary>
    /// 读取图片Base64
    /// </summary>
    /// <param name="imagebase64"></param>
    /// <returns></returns>
    public string DetectTextBase64(string imagebase64)
    {
        OCRResult ocrResult = engine.DetectTextBase64(imagebase64);
        return ocrResult.Text;
    }
}

 

你可能感兴趣的