C# uses the Spire.OCR framework to recognize letters, numbers, text, etc. in pictures

OCR

OCR (optical character recognition), optical character recognition.

      OCR text recognition refers to the process of electronic equipment (such as a scanner or digital camera) checking the characters printed on paper, and then using the character recognition method to translate the shape into computer text; that is, the text data is scanned, and then the image file is analyzed The process of processing and obtaining text and layout information.
      How to debug or use auxiliary information to improve the accuracy of recognition is the most important topic of OCR.
      The main indicators to measure the performance of an OCR system are: rejection rate, false recognition rate, recognition speed, user interface friendliness, product stability, usability and feasibility, etc.
      The realization of OCR technology can be generally divided into five steps: preprocessing pictures, cutting characters, recognizing characters, restoring layout, and post-processing text. The middle three steps are the core, and the first and last two steps are the most difficult.

Using the Spire.OCR framework in C# can recognize letters, text, numbers, etc. in pictures.

1. Demo test

Create a new form application ImageRecognitionTextDemo in VS2019, and rename the default Form1 to FormImageRecognitionText.

Right click project properties

Select the target platform as X64 

Right-click the project ImageRecognitionTextDemo, select [Manage NuGet Package], and search for Spire.OCR

Install the Nuget package complete

Place the downloaded unmanaged dll under the debug of the application, or copy it to the root directory of the project, and choose to always copy.

 Note that hosting Spire.OCR.dll needs to automatically reference the above six framework packages, so Spire.OCR.dll and the six framework packages must be in the same path.

2. The form FormImageRecognitionText designer code is as follows

The source program of the file FormImageRecognitionText.Designer.cs is as follows:


namespace ImageRecognitionTextDemo
{
    partial class FormImageRecognitionText
    {
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows 窗体设计器生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要修改
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.pictureBox1 = new System.Windows.Forms.PictureBox();
            this.rtxbDisplay = new System.Windows.Forms.RichTextBox();
            this.btnOpen = new System.Windows.Forms.Button();
            this.btnRecognize = new System.Windows.Forms.Button();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
            this.SuspendLayout();
            // 
            // pictureBox1
            // 
            this.pictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.pictureBox1.Location = new System.Drawing.Point(12, 12);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new System.Drawing.Size(400, 400);
            this.pictureBox1.TabIndex = 0;
            this.pictureBox1.TabStop = false;
            // 
            // rtxbDisplay
            // 
            this.rtxbDisplay.Location = new System.Drawing.Point(418, 12);
            this.rtxbDisplay.Name = "rtxbDisplay";
            this.rtxbDisplay.ReadOnly = true;
            this.rtxbDisplay.Size = new System.Drawing.Size(562, 715);
            this.rtxbDisplay.TabIndex = 1;
            this.rtxbDisplay.Text = "";
            // 
            // btnOpen
            // 
            this.btnOpen.Font = new System.Drawing.Font("宋体", 13F, System.Drawing.FontStyle.Bold);
            this.btnOpen.Location = new System.Drawing.Point(51, 439);
            this.btnOpen.Name = "btnOpen";
            this.btnOpen.Size = new System.Drawing.Size(104, 56);
            this.btnOpen.TabIndex = 2;
            this.btnOpen.Text = "打开图片";
            this.btnOpen.UseVisualStyleBackColor = true;
            this.btnOpen.Click += new System.EventHandler(this.btnOpen_Click);
            // 
            // btnRecognize
            // 
            this.btnRecognize.Font = new System.Drawing.Font("宋体", 13F, System.Drawing.FontStyle.Bold);
            this.btnRecognize.Location = new System.Drawing.Point(217, 439);
            this.btnRecognize.Name = "btnRecognize";
            this.btnRecognize.Size = new System.Drawing.Size(104, 56);
            this.btnRecognize.TabIndex = 3;
            this.btnRecognize.Text = "识别图片";
            this.btnRecognize.UseVisualStyleBackColor = true;
            this.btnRecognize.Click += new System.EventHandler(this.btnRecognize_Click);
            // 
            // FormImageRecognitionText
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(982, 739);
            this.Controls.Add(this.btnRecognize);
            this.Controls.Add(this.btnOpen);
            this.Controls.Add(this.rtxbDisplay);
            this.Controls.Add(this.pictureBox1);
            this.Name = "FormImageRecognitionText";
            this.Text = "图像识别文本框架OCR";
            this.Load += new System.EventHandler(this.FormImageRecognitionText_Load);
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.PictureBox pictureBox1;
        private System.Windows.Forms.RichTextBox rtxbDisplay;
        private System.Windows.Forms.Button btnOpen;
        private System.Windows.Forms.Button btnRecognize;
    }
}

3. The source program of the FormImageRecognitionText form is as follows:

Program File FormImageRecognitionText.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Spire.OCR;

namespace ImageRecognitionTextDemo
{
    public partial class FormImageRecognitionText : Form
    {
        public FormImageRecognitionText()
        {
            InitializeComponent();
        }

        private void FormImageRecognitionText_Load(object sender, EventArgs e)
        {
            //C# .NET实现扫描识别图片中的文字 Spire.OCR
            //OCR:光学字符识别。
            /*
             * OCR(optical character recognition)文字识别是指电子设备(例如扫描仪或数码相机)检查纸上打印的字符,
             * 然后用字符识别方法将形状翻译成计算机文字的过程;即,对文本资料进行扫描,然后对图像文件进行分析处理,获取文字及版面信息的过程。
             * 如何除错或利用辅助信息提高识别正确率,是OCR最重要的课题。
             * 衡量一个OCR系统性能好坏的主要指标有:拒识率、误识率、识别速度、用户界面的友好性,产品的稳定性,易用性及可行性等。
             * OCR技术的实现,总体上可以分为五步:预处理图片、切割字符、识别字符、恢复版面、后处理文字。中间的三步是核心,头尾两步最难。
             * 百家号技术:
             * https://baijiahao.baidu.com/s?id=1744946979174786023&wfr=spider&for=pc
            */
            pictureBox1.BackgroundImageLayout = ImageLayout.Zoom;
        }

        /// <summary>
        /// 显示文本框内容
        /// </summary>
        /// <param name="message"></param>
        private void DisplayContent(string message)
        {
            this.BeginInvoke(new Action(() =>
            {
                if (rtxbDisplay.TextLength > 10240)
                {
                    rtxbDisplay.Clear();
                }
                rtxbDisplay.AppendText($"{DateTime.Now.ToString("HH:mm:ss.fff")}->{message}\n");
                rtxbDisplay.ScrollToCaret();
            }));
            Application.DoEvents();
        }

        /// <summary>
        /// 显示OCR处理结果
        /// </summary>
        /// <param name="ocrScanner"></param>
        /// <param name="stopwatch"></param>
        /// <param name="result"></param>
        private void DisplayProcessResultOCR(OcrScanner ocrScanner, System.Diagnostics.Stopwatch stopwatch, bool result)
        {
            IOCRText text = ocrScanner.Text;
            IOCRTextBlock[] ocrTextBlocks = text.Blocks;
            stopwatch.Stop();
            DisplayContent($"识别图片完成,耗时【{stopwatch.Elapsed.TotalMilliseconds}】ms:识别结果【{result}】,文本块个数【{ocrTextBlocks.Length}】");
            for (int i = 0; i < ocrTextBlocks.Length; i++)
            {
                DisplayContent($"第【{(i + 1).ToString("D2")}】个文本块:");
                DisplayContent($"  Text:【{ocrTextBlocks[i].Text}】");
                DisplayContent($"  Confidence:【{ocrTextBlocks[i].Confidence}】");
                DisplayContent($"  Level:【{ocrTextBlocks[i].Level}】");
                DisplayContent($"  IsTruncated:【{ocrTextBlocks[i].IsTruncated}】");
                DisplayContent($"  Box:【{ocrTextBlocks[i].Box}】");
                //DisplayContent($"  TextBlock:【{ocrTextBlocks[i].TextBlock == null}】");
            }
            MessageBox.Show(text.ToString());
        }

        private async void btnOpen_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "jpeg|*.jpg|bmp|*.bmp|gif|*.gif|png|*.png|tiff|*.tiff|All|*.*";
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                string fileName = openFileDialog.FileName;
                pictureBox1.BackgroundImage = Image.FromFile(fileName);

                await Task.Run(new Action(() =>
                {
                    OcrScanner ocrScanner = new OcrScanner();
                    System.Diagnostics.Stopwatch stopwatch = System.Diagnostics.Stopwatch.StartNew();
                    DisplayContent($"开始识别图片:【{fileName}】");
                    bool result = ocrScanner.Scan(fileName);
                    DisplayProcessResultOCR(ocrScanner, stopwatch, result);
                }));
            }
        }

        private async void btnRecognize_Click(object sender, EventArgs e)
        {
            MemoryStream memoryStream = new MemoryStream();
            pictureBox1.BackgroundImage.Save(memoryStream, pictureBox1.BackgroundImage.RawFormat);
            await Task.Run(new Action(() =>
            {
                OcrScanner ocrScanner = new OcrScanner();
                System.Diagnostics.Stopwatch stopwatch = System.Diagnostics.Stopwatch.StartNew();
                DisplayContent($"开始识别图片:图片大小【{memoryStream.Length / 1024}】KB");
                OCRImageFormat imageFormat;
                Enum.TryParse(pictureBox1.BackgroundImage.RawFormat.ToString(), true, out imageFormat);
                bool result = ocrScanner.Scan(memoryStream, imageFormat);
                DisplayProcessResultOCR(ocrScanner, stopwatch, result);
            }));
        }
    }
}

4. The test run is as follows:

 

 

Guess you like

Origin blog.csdn.net/ylq1045/article/details/128718881