コンピュータービジョンを使用してテキストを見つける方法
イントロダクション
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
このチュートリアルには、コード例がさらに下に含まれています。 以下は現在利用可能なメソッドの一般的な概要です:
<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`の使用は、コンピュータビジョンを使用して、OcrInputオブジェクトの各ページに含まれるテキスト要素の領域を検出します。
```cs
:path=/static-assets/ocr/content-code-examples/how-to/computer-vision-findtextregion-1.cs
カスタムパラメータを指定して呼び出すこともできます:
: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
この例では、入力画像のテキストの位置が異なる可能性があるため、テキストを含む領域にクロップする必要があるメソッドを書くために次の画像を使用します。 この場合、コンピュータービジョンがテキストを検出した領域にスキャンを絞り込むために、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)
このコードには2つの出力があります。最初の出力は、デバッグに使用されるStampCropRectangleAndSaveAs
によって保存された.png
ファイルです。 IronCV(コンピュータービジョン)がテキストがあると判断した場所を確認できます:
かなり良さそうです。 さて、2つ目の出力はテキスト自体です。
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
の使用は、OcrInput
オブジェクトのすべてのページを取り込み、コンピュータビジョンを使用してテキスト要素を含む領域を検出し、テキスト領域に基づいて入力を個別の画像に分割します。
: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
カスタムパラメータを指定して呼び出すこともできます:
: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
別のオーバーロードメソッドであるFindMultipleTextRegions
は、OCRページを受け取り、その中の各テキスト領域に対して1つの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()
テキスト領域を取得
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();
特定のユースケースガイド
適切な設定と入力ファイルがあれば、OCRは非常に強力なツールになります。 それはほぼ完全に人間の読み取り能力を模倣することができます。