Comment utiliser la vision par ordinateur pour trouver du texte
Introduction
IronOCR utilise OpenCV pour utiliser la vision par ordinateur afin de détecter les zones où se trouve du texte dans une image. Cette fonction est utile pour les images contenant beaucoup de bruit, les images dont le texte se trouve à plusieurs endroits différents et les images dont le texte est déformé. L'utilisation de la vision par ordinateur dans IronOCR déterminera où se trouvent les régions de texte et utilisera ensuite Tesseract pour tenter de lire ces régions.
- Comment effectuer l'OCR de la plaque d'immatriculation en C# (Tutoriel)
- Comment extraire du texte d'une facture dans un tutoriel C#
- Comment extraire du texte d'une capture d'écran avec OCR en C#
-
Comment OCRiser des sous-titres en C# (Tutoriel)
Étapes de l'utilisation de l'OCR avec la vision par ordinateur
- Télécharger la bibliothèque C# pour utiliser OCR avec la vision par ordinateur
- Utilisez la méthode
FindTextRegion
pour détecter automatiquement les zones de texte - Vérifiez quelle région de texte est détectée avec la méthode
StampCropRectangleAndSaveAs
- Utilisez la vision par ordinateur pour séparer l'image originale en images en fonction des régions de texte avec la méthode
FindMultipleTextRegions
- Utilisez la méthode
GetTextRegions
pour obtenir la liste des zones de recadrage où du texte a été détecté
Installation d'IronOCR.ComputerVision via un paquet NuGet
Les méthodes OpenCV qui effectuent de la vision par ordinateur dans IronOCR sont visibles dans le paquet NuGet IronOCR normal.
L'utilisation de ces méthodes nécessite l'installation de NuGet de IronOcr.ComputerVision
dans la solution. Vous serez invité à le télécharger si vous ne l'avez pas installé.
- Windows:
IronOcr.ComputerVision.Windows
- Linux :
IronOcr.ComputerVision.Linux
- macOS :
IronOcr.ComputerVision.MacOS
-
macOS ARM :
IronOcr.ComputerVision.MacOS.ARM
Installer à l'aide du gestionnaire de paquets NuGet ou coller le texte suivant dans la console du gestionnaire de paquets : ``
PM> Installer-Package IronOCR.ComputerVision.Windows ``
Cela fournira les assemblages nécessaires pour utiliser IronOCR Computer Vision avec notre fichier modèle.
Fonctionnalité et API
Des exemples de code sont inclus plus loin dans ce tutoriel. Voici un aperçu général des méthodes actuellement disponibles :
Method | Explanation |
---|---|
FindTextRegion | Detect regions which contain text elements and instruct Tesseract to only search for text within the area in which text was detected. |
FindMultipleTextRegions | Detect areas which contain text elements and divide the page into separate images based on text regions. |
GetTextRegions | Scans the image and returns a list of text regions as `List |
FindTextRegion
L'utilisation de FindTextRegion
utilisera la vision par ordinateur pour détecter les régions contenant des éléments de texte sur chaque page d'un objet OcrInput.
:path=/static-assets/ocr/content-code-examples/how-to/computer-vision-findtextregion-1.cs
using IronOcr;
var ocr = new IronTesseract();
using var input = new OcrInput();
input.LoadImage("/path/file.png");
input.FindTextRegion();
OcrResult result = ocr.Read(input);
string resultText = result.Text;
Imports IronOcr
Private ocr = New IronTesseract()
Private input = New OcrInput()
input.LoadImage("/path/file.png")
input.FindTextRegion()
Dim result As OcrResult = ocr.Read(input)
Dim resultText As String = result.Text
Peut éventuellement être appelé avec des paramètres personnalisés :
:path=/static-assets/ocr/content-code-examples/how-to/computer-vision-findtextregion-2.cs
using IronOcr;
var ocr = new IronTesseract();
using var input = new OcrInput();
input.LoadImage("/path/file.png");
input.FindTextRegion(Scale: 2.0, DilationAmount: 20, Binarize: true, Invert: true);
OcrResult result = ocr.Read(input);
string resultText = result.Text;
Imports IronOcr
Private ocr = New IronTesseract()
Private input = New OcrInput()
input.LoadImage("/path/file.png")
input.FindTextRegion(Scale:= 2.0, DilationAmount:= 20, Binarize:= True, Invert:= True)
Dim result As OcrResult = ocr.Read(input)
Dim resultText As String = result.Text
Dans cet exemple, j'utiliserai l'image suivante pour une méthode que je suis en train d'écrire et qui doit recadrer les zones contenant du texte, mais les images d'entrée peuvent varier en ce qui concerne l'emplacement du texte. Dans ce cas, je peux utiliser FindTextRegion pour limiter l'analyse à une zone où la vision par ordinateur a détecté du texte. Il s'agit d'un exemple d'image :

:path=/static-assets/ocr/content-code-examples/how-to/computer-vision-findtextregion-3.cs
using IronOcr;
using IronSoftware.Drawing;
using System;
using System.Linq;
var ocr = new IronTesseract();
using var input = new OcrInput();
input.LoadImage("wh-words-sign.jpg");
// Find the text region using Computer Vision
Rectangle textCropArea = input.GetPages().First().FindTextRegion();
// For debugging and demonstration purposes, lets see what region it found:
input.StampCropRectangleAndSaveAs(textCropArea, Color.Red, "image_text_area", AnyBitmap.ImageFormat.Png);
// Looks good, so let us apply this region to hasten the read:
var ocrResult = ocr.Read("wh-words-sign.jpg", textCropArea);
Console.WriteLine(ocrResult.Text);
Imports IronOcr
Imports IronSoftware.Drawing
Imports System
Imports System.Linq
Private ocr = New IronTesseract()
Private input = New OcrInput()
input.LoadImage("wh-words-sign.jpg")
' Find the text region using Computer Vision
Dim textCropArea As Rectangle = input.GetPages().First().FindTextRegion()
' For debugging and demonstration purposes, lets see what region it found:
input.StampCropRectangleAndSaveAs(textCropArea, Color.Red, "image_text_area", AnyBitmap.ImageFormat.Png)
' Looks good, so let us apply this region to hasten the read:
Dim ocrResult = ocr.Read("wh-words-sign.jpg", textCropArea)
Console.WriteLine(ocrResult.Text)
Maintenant, ce code a deux sorties, la première est un fichier .png
enregistré par StampCropRectangleAndSaveAs
qui est utilisé pour le débogage. Nous pouvons voir où IronCV (Computer Vision) pensait que le texte était :
Il a l'air plutôt bon. La deuxième sortie est le texte lui-même qui est :
IRONSOFTWARE
50,000+
Developers in our active community
10,777,061 19,313
NuGet downloads Support tickets resolved
50%+ 80%+
Engineering Team growth Support Team growth
$25,000+
Raised with #TEAMSEAS to clean our beaches & waterways
FindMultipleTextRegions
L'utilisation de FindMultipleTextRegions
prend toutes les pages d'un objet OcrInput
et utilise la vision par ordinateur pour détecter les zones contenant des éléments textuels et divise l'entrée en images distinctes basées sur les régions de texte :
:path=/static-assets/ocr/content-code-examples/how-to/computer-vision-findmultipletextregions-1.cs
using IronOcr;
var ocr = new IronTesseract();
using var input = new OcrInput();
input.LoadImage("/path/file.png");
input.FindMultipleTextRegions();
OcrResult result = ocr.Read(input);
string resultText = result.Text;
Imports IronOcr
Private ocr = New IronTesseract()
Private input = New OcrInput()
input.LoadImage("/path/file.png")
input.FindMultipleTextRegions()
Dim result As OcrResult = ocr.Read(input)
Dim resultText As String = result.Text
Peut éventuellement être appelé avec des paramètres personnalisés :
:path=/static-assets/ocr/content-code-examples/how-to/computer-vision-findmultipletextregions-2.cs
using IronOcr;
var ocr = new IronTesseract();
using var input = new OcrInput();
input.LoadImage("/path/file.png");
input.FindMultipleTextRegions(Scale: 2.0, DilationAmount: -1, Binarize: true, Invert: false);
OcrResult result = ocr.Read(input);
string resultText = result.Text;
Imports IronOcr
Private ocr = New IronTesseract()
Private input = New OcrInput()
input.LoadImage("/path/file.png")
input.FindMultipleTextRegions(Scale:= 2.0, DilationAmount:= -1, Binarize:= True, Invert:= False)
Dim result As OcrResult = ocr.Read(input)
Dim resultText As String = result.Text
Une autre méthode surcharge de FindMultipleTextRegions
prend une page OCR et renvoie une liste de pages OCR, une pour chaque région de texte.
:path=/static-assets/ocr/content-code-examples/how-to/computer-vision-findmultipletextregions-3.cs
using IronOcr;
using System.Collections.Generic;
using System.Linq;
int pageIndex = 0;
using var input = new OcrInput();
input.LoadImage("/path/file.png");
var selectedPage = input.GetPages().ElementAt(pageIndex);
List<OcrInputPage> textRegionsOnPage = selectedPage.FindMultipleTextRegions();
Imports IronOcr
Imports System.Collections.Generic
Imports System.Linq
Private pageIndex As Integer = 0
Private input = New OcrInput()
input.LoadImage("/path/file.png")
Dim selectedPage = input.GetPages().ElementAt(pageIndex)
Dim textRegionsOnPage As List(Of OcrInputPage) = selectedPage.FindMultipleTextRegions()
Obtenir des régions de texte
L'utilisation de GetTextRegions
retourne une liste de zones de découpe où le texte a été détecté sur une page :
:path=/static-assets/ocr/content-code-examples/how-to/computer-vision-gettextregions.cs
using IronOcr;
using IronSoftware.Drawing;
using System.Collections.Generic;
using System.Linq;
int pageIndex = 0;
using var input = new OcrInput();
input.LoadImage("/path/file.png");
var selectedPage = input.GetPages().ElementAt(pageIndex);
// List<Rectangle> regions = selectedPage.GetTextRegions();
Imports IronOcr
Imports IronSoftware.Drawing
Imports System.Collections.Generic
Imports System.Linq
Private pageIndex As Integer = 0
Private input = New OcrInput()
input.LoadImage("/path/file.png")
Dim selectedPage = input.GetPages().ElementAt(pageIndex)
' List<Rectangle> regions = selectedPage.GetTextRegions();
Guides de cas d'utilisation spécifiques
Avec les bons paramètres et les bons fichiers d'entrée, l'OCR peut être un outil très puissant. Il peut imiter presque parfaitement la capacité de lecture d'un être humain.