如何使用電腦視覺尋找文本

This article was translated from English: Does it need improvement?
Translated
View the article in English

介紹

IronOCR 利用 OpenCV 使用計算機視覺來檢測圖像中存在文本的區域。這對於包含大量噪點的圖像、文本分布在多個不同位置的圖像以及文本變形的圖像非常有用。在 IronOCR 中使用計算機視覺可以確定文本區域的位置,然後使用 Tesseract 嘗試讀取這些區域。



通過 NuGet 套件安裝 IronOCR.ComputerVision

IronOCR 中執行計算機視覺的 OpenCV 方法在普通的 IronOCR NuGet 套件中可見。

使用這些方法需要將 IronOcr.ComputerVision NuGet 安裝到解決方案中,如果您未安裝,系統會提示您下載。

  • Windows: IronOcr.ComputerVision.Windows
  • Linux: IronOcr.ComputerVision.Linux
  • macOS: IronOcr.ComputerVision.MacOS
  • macOS ARM: IronOcr.ComputerVision.MacOS.ARM

使用 NuGet 套件管理器安裝或在套件管理器控制台中粘貼以下內容:

PM> Install-Package IronOcr.ComputerVision.Windows

這將提供必要的組件,以便使用IronOCR計算機視覺與我們的模型文件。

功能和 API

本教程稍後會提供程式碼示例。以下是目前可用的方法的概要:

方法解釋
尋找文本區域檢測包含文字元素的區域並指示Tesseract僅在檢測到文字的區域內搜尋文字。
查找多個文本區域檢測包含文字元素的區域,並根據文字區域將頁面劃分為單獨的圖像。
獲取文本區域掃描圖像並返回文本區域列表為 `List`.

FindTextRegion

使用 FindTextRegion 將使用計算機視覺來檢測 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
VB   C#

可以選擇使用自訂參數調用:

: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
VB   C#

在這個範例中,我將使用以下圖片寫一個方法,該方法需要裁剪包含文字的區域,但輸入圖片的文字位置可能會有所不同。在這種情況下,我可以使用 FindTextRegion 將掃描範圍縮小到電腦視覺檢測到文字的區域。這是一個範例圖片:

帶有文字的圖片
: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)
VB   C#

現在這段程式碼有兩個輸出,第一個是由 StampCropRectangleAndSaveAs 保存的 .png 檔案,用於偵錯。我們可以看到 IronCV 的位置。 (電腦視覺) 認為文字是:

帶有文字區域突顯的圖像

看起來很不錯。現在第二個輸出是文本本身,即:

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

使用 FindMultipleTextRegionsOcrInput 對象的所有頁面進行處理,並使用計算機視覺檢測包含文字元素的區域,根據文字區域將輸入劃分為獨立的圖像:

: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
VB   C#

可以選擇使用自訂參數調用:

: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
VB   C#

FindMultipleTextRegions 的另一種重載方法會接收一個 OCR 頁面並返回一個包含多個 OCR 頁面的列表,每個頁面分別對應一個文本區域:

: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()
VB   C#

GetTextRegions

使用 GetTextRegions 將返回一個裁剪區域的列表,這些區域是頁面中檢測到文字的位置:

: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();
VB   C#

特定使用案例指南

使用正確的設置和輸入文件,OCR 可能是一個非常強大的工具。它幾乎可以完美模擬人類的閱讀能力。