C# utilise le framework Spire.OCR pour reconnaître les lettres, les chiffres, le texte, etc. dans les images

ROC

OCR (reconnaissance optique de caractères), reconnaissance optique de caractères.

      La reconnaissance de texte OCR fait référence au processus par lequel un équipement électronique (tel qu'un scanner ou un appareil photo numérique) vérifie les caractères imprimés sur papier, puis utilise la méthode de reconnaissance de caractères pour traduire la forme en texte informatique ; c'est-à-dire que les données textuelles sont numérisées , puis le fichier image est analysé Le processus de traitement et d'obtention des informations de texte et de mise en page.
      Comment déboguer ou utiliser des informations auxiliaires pour améliorer la précision de la reconnaissance est le sujet le plus important de l'OCR.
      Les principaux indicateurs pour mesurer la performance d'un système OCR sont : le taux de rejet, le taux de fausses reconnaissances, la vitesse de reconnaissance, la convivialité de l'interface utilisateur, la stabilité du produit, l'utilisabilité et la faisabilité, etc.
      La réalisation de la technologie OCR peut généralement être divisée en cinq étapes : prétraitement des images, découpage des caractères, reconnaissance des caractères, restauration de la mise en page et post-traitement du texte. Les trois étapes du milieu sont le noyau, et les deux premières et dernières étapes sont les plus difficiles.

L'utilisation du framework Spire.OCR en C # peut reconnaître des lettres, du texte, des chiffres, etc. dans des images.

1. Test de démonstration

Créez une nouvelle application de formulaire ImageRecognitionTextDemo dans VS2019 et renommez le Form1 par défaut en FormImageRecognitionText.

Cliquez avec le bouton droit sur les propriétés du projet

Sélectionnez la plate-forme cible comme X64 

Cliquez avec le bouton droit sur le projet ImageRecognitionTextDemo, sélectionnez [Gérer le package NuGet] et recherchez Spire.OCR

Installez le package Nuget terminé

Placez la dll non gérée téléchargée sous le débogage de l'application, ou copiez-la dans le répertoire racine du projet, et choisissez de toujours copier.

 Notez que l'hébergement de Spire.OCR.dll doit automatiquement référencer les six packages de framework ci-dessus, de sorte que Spire.OCR.dll et les six packages de framework doivent se trouver dans le même chemin.

2. Le code du concepteur de formulaire FormImageRecognitionText est le suivant

Le programme source du fichier FormImageRecognitionText.Designer.cs est le suivant :


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. Le programme source du formulaire FormImageRecognitionText est le suivant :

Fichier de programme 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. L'exécution du test est la suivante :

 

 

Je suppose que tu aimes

Origine blog.csdn.net/ylq1045/article/details/128718881
conseillé
Classement