コンピュータービジョンを使用してテキストを見つける方法

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の使用は、コンピュータビジョンを使用して、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#

このコードには2つの出力があります。1つ目はデバッグに使用される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
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ページを受け取り、その上の各テキスト領域ごとに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()
VB   C#

テキスト領域を取得

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は非常に強力なツールになります。 それはほぼ完全に人間の読み取り能力を模倣することができます。