How to use PaddleOCRSharp under NET framework

Open VSIDE, create a new Windows Forms application (.NETFramework) type project, select a .NET framework, such as .NETFramework 4.0, right-click the project, select Properties > Build, and set the target platform to X64.

Install the nuget package

Search for PaddleOCRSharp and install the latest version

the code

OpenFileDialog ofd = new OpenFileDialog();

ofd.Filter ="*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";

if (ofd.ShowDialog() != true) return;

var imagebyte =File.ReadAllBytes(ofd.FileName);

Bitmap bitmap = newBitmap(new MemoryStream(imagebyte));

OCRModelConfig config =null;

OCRParameter oCRParameter = new OCRParameter ();

OCRResult ocrResult =new OCRResult();

using (PaddleOCREngine engine = new PaddleOCREngine(config, oCRParameter))

{
    
    

	ocrResult =engine.DetectText(bitmap );

}

if (ocrResult != null)

{
    
    

	MessageBox.Show(ocrResult.Text,"识别结果");

}

winform

using PaddleOCRSharp;
using Spire.OCR;

namespace WinFormsApp
{
    
    
    public partial class Form1 : Form
    {
    
    
        public PaddleOCREngine engine;
        public Form1()
        {
    
    
            InitializeComponent();
            engine = CreateOCRParameter();// 这个只能引用一次,否则会出现内存一直增加的问题
        }

        #region SpireOCR
        private void SpireOCR_Click(object sender, EventArgs e)
        {
    
    
            textBox1.Text = "";
            this.openFileDialog1.FileName = "";
            this.openFileDialog1.Filter = "所有文件(*.*)|*.*";
            if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
            {
    
    
                OcrScanner scanner = new OcrScanner();
                scanner.Scan(this.openFileDialog1.FileName);
                scanner.Dispose();
                textBox1.Text = "SpireOCR识别结果:\r\n" + scanner.Text.ToString().Split("Evaluation")[0];
            }
        }
        #endregion

        #region PaddleOCRSharp
        public 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;
        }

        private void PaddleOCRSharp_Click(object sender, EventArgs e)
        {
    
    
            textBox1.Text = "";
            this.openFileDialog1.FileName = "";
            this.openFileDialog1.Filter = "所有文件(*.*)|*.*";
            if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
            {
    
    
                OCRResult ocrResult = engine.DetectText(this.openFileDialog1.FileName);
                textBox1.Text = "PaddleOCRSharp识别结果:\r\n" + ocrResult.Text;
            }
        }
        #endregion
    }
}

Guess you like

Origin blog.csdn.net/BeanGo/article/details/129194692
Recommended