Wie man mit Computer Vision Text findet
Einführung
IronOCR nutzt OpenCV, um mit Hilfe von Computer Vision Bereiche zu erkennen, in denen Text in einem Bild vorhanden ist. Dies ist nützlich für Bilder, die viel Rauschen enthalten, Bilder mit Text an vielen verschiedenen Stellen und Bilder, bei denen der Text verzerrt ist. Mit Hilfe von Computer Vision in IronOCR wird festgestellt, wo Textbereiche vorhanden sind, und dann mit Tesseract versucht, diese Bereiche zu lesen.
- So erkennen Sie Kennzeichen mit OCR in C# (Tutorial)
- Wie man Text von einer Rechnung in C# erhält - Tutorial
- Wie man Text aus einem Screenshot in C# mit OCR extrahiert
-
Wie man Untertitel in C# mit OCR verarbeitet (Tutorial)
Schritte zur Verwendung von OCR mit Computer Vision
- Laden Sie die C#-Bibliothek herunter, um OCR mit Computer Vision zu verwenden
- Verwenden Sie die
FindTextRegion
-Methode, um Textregionen automatisch zu erkennen - Prüfen Sie, welcher Textbereich mit der
StampCropRectangleAndSaveAs
-Methode erkannt wird - Nutzen Sie Computer Vision, um das ursprüngliche Bild in Bilder auf der Grundlage von Textregionen mit der
FindMultipleTextRegions
-Methode zu trennen - Verwenden Sie die
GetTextRegions
-Methode, um eine Liste der Zonen zu erhalten, in denen Text erkannt wurde
IronOCR.ComputerVision Installation über NuGet-Paket
OpenCV-Methoden, die Computer Vision in IronOCR ausführen, sind im regulären IronOCR NuGet-Paket sichtbar.
Die Verwendung dieser Methoden erfordert die Installation von IronOcr.ComputerVision
über NuGet in der Lösung. Sie werden zum Herunterladen aufgefordert, wenn es nicht installiert ist.
- Windows:
IronOcr.ComputerVision.Windows
- Linux:
IronOcr.ComputerVision.Linux
- macOS:
IronOcr.ComputerVision.MacOS
-
macOS ARM:
IronOcr.ComputerVision.MacOS.ARM
Installieren Sie mit dem NuGet-Paketmanager oder fügen Sie Folgendes in die Paketmanager-Konsole ein:
PM> Install-Package IronOCR.ComputerVision.Windows
Dadurch werden die erforderlichen Baugruppen für die Verwendung von IronOCR Computer Vision mit unserer Modelldatei bereitgestellt.
## Funktionsweise und API
Code-Beispiele sind weiter unten in diesem Tutorial enthalten. Im Folgenden finden Sie einen allgemeinen Überblick über die derzeit verfügbaren Methoden:
<table class="table table__configuration-variables">
<tr>
<th scope="col">Method</th>
<th scope="col">Explanation</th>
</tr>
<tr>
<td><a href="#anchor-findtextregion">FindTextRegion</a></td>
<td class="word-break--break-word">Detect regions which contain text elements and instruct Tesseract to only search for text within the area in which text was detected.</td>
</tr>
<tr>
<td><a href="#anchor-findmultipletextregions">FindMultipleTextRegions</a></td>
<td class="word-break--break-word">Detect areas which contain text elements and divide the page into separate images based on text regions.</td>
</tr>
<tr>
<td><a href="#anchor-gettextregions">GetTextRegions</a></td>
<td class="word-break--break-word">Scans the image and returns a list of text regions as `List<CropRectangle>`.</td>
</tr>
</table>
## FindTextRegion
Die Verwendung von `FindTextRegion` nutzt Computer Vision, um Bereiche zu erkennen, die Textelemente auf jeder Seite eines OcrInput-Objekts enthalten.
```cs
:path=/static-assets/ocr/content-code-examples/how-to/computer-vision-findtextregion-1.cs
Kann optional mit benutzerdefinierten Parametern aufgerufen werden:
: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
In diesem Beispiel werde ich das folgende Bild für eine Methode verwenden, die ich gerade schreibe und die Bereiche mit Text beschneiden muss, aber die Eingabebilder können in der Textposition variieren. In diesem Fall kann ich FindTextRegion verwenden, um den Scan auf einen Bereich einzugrenzen, in dem Computer Vision Text erkannt hat. Dies ist ein Beispielbild:

: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)
Jetzt hat dieser Code zwei Ausgaben, die erste ist eine .png
-Datei, die von StampCropRectangleAndSaveAs
gespeichert wird und zum Debuggen verwendet wird. Wir können sehen, wo IronCV (Computer Vision) den Text vermutete:
Sieht ziemlich gut aus. Die zweite Ausgabe ist der Text selbst, der lautet:
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
Die Verwendung von FindMultipleTextRegions
nimmt alle Seiten eines OcrInput
-Objekts und nutzt Computer Vision, um Bereiche zu erkennen, die Textelemente enthalten, und teilt den Input basierend auf den Textbereichen in separate Bilder auf:
: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
Kann optional mit benutzerdefinierten Parametern aufgerufen werden:
: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
Eine weitere Überladung der Methode FindMultipleTextRegions
nimmt eine OCR-Seite und gibt eine Liste von OCR-Seiten zurück, eine für jede Textregion darauf:
: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()
GetTextRegions
Die Verwendung von GetTextRegions
gibt eine Liste von Schnittflächen zurück, in denen Text auf einer Seite erkannt wurde:
: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();
Spezifische Leitfäden für Anwendungsfälle
Mit den richtigen Einstellungen und Eingabedateien kann die OCR ein sehr leistungsfähiges Werkzeug sein. Es kann die Lesefähigkeit eines Menschen nahezu perfekt imitieren.