C#とVB.NETでOCRを始める

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

IronOCRは、.NETプラットフォームのソフトウェア開発者が画像やPDFドキュメントからテキストを認識して読み取ることを可能にするC#ソフトウェアライブラリです。 それは、最も高度なTesseractエンジンを使用した純粋な.NET OCRライブラリです。

インストール

NuGetパッケージマネージャーでインストール

Visual StudioやコマンドラインのNuGetパッケージマネージャーでIronOCRをインストールします。 Visual Studio で、コンソールに移動します:

  • ツール ->
  • NuGet パッケージ マネージャー

  • パッケージ マネージャー コンソール
Install-Package IronOcr

そしてチェックしてください NuGetのIronOCR バージョンアップデートやインストールについての詳細はこちらをご覧ください。

他のプラットフォーム向けに利用可能なIronOCR NuGetパッケージもあります。

IronOCRの.ZIPをダウンロード

また、IronOCRを .ZIPファイルとしてダウンロードすることもできます。 クリックして DLLを直接ダウンロードする. .zipファイルをダウンロードしたら:

.NET Framework 4.0以降のインストール手順:

  • net40フォルダのIronOcr.dllをプロジェクトに含めてください。
  • 次に、アセンブリ参照を追加します:

    • システム構成

    • System.Drawing(システム ドローイング)

    • System.Web (システムウェブ)

.NET Standard & .NET Core 2.0+、および.NET 5のための指示

  • netstandard2.0フォルダ内のIronOcr.dllをプロジェクトに含めてください。
  • 次に、NuGetパッケージ参照を追加します:

    * System.Drawing.Common 4.7以上

IronOCRインストーラーをダウンロードする(Windowsのみ)

もう一つの選択肢は、IronOCRのインストーラーをダウンロードすることです。これにより、IronOCRが即座に動作するために必要なすべてのリソースがインストールされます。 このオプションはWindowsシステム専用ですので、ご注意ください。 インストーラーをダウンロードするには ここをクリック. .zipファイルをダウンロードしたら:

.NET Framework 4.0以降のインストール手順:

  • net40フォルダのIronOcr.dllをプロジェクトに含めてください。
  • 次に、アセンブリ参照を追加します:

    • システム構成

    • System.Drawing(システム ドローイング)

    • System.Web (システムウェブ)

.NET Standard & .NET Core 2.0+、および.NET 5のための指示

  • netstandard2.0フォルダ内のIronOcr.dllをプロジェクトに含めてください。
  • 次に、NuGetパッケージ参照を追加します:

    * System.Drawing.Common 4.7以上

なぜIronOCRを選ぶのか?

IronOCRは、インストールが簡単で、完全かつ詳細なドキュメントを備えた.NETソフトウェアライブラリです。

IronOCRを選択すると、外部のウェブサービスを使用したり、継続的な費用を発生させたり、機密文書をインターネット経由で送信したりすることなく、99.8%以上のOCR精度を達成できます。

C#開発者がVanilla TesseractよりIronOCRを選ぶ理由:

  • 単一のDLLまたはNuGetとしてインストール
  • Tesseract 5、4、および3エンジンをすぐに利用可能です。
  • 精度99.8%は、通常のTesseractを大幅に上回ります。
  • 驚異的な速度とマルチスレッド
  • MVC、Webアプリ、デスクトップ、コンソール、サーバーアプリケーション対応
  • 作業するための実行可能ファイルやC++コードはありません。
  • 完全なPDF OCRサポート
  • ほとんどすべての画像ファイルまたはPDFに対してOCRを実行します
  • 完全な .NET Core、Standard、および FrameWork のサポート
  • Windows、Mac、Linux、Azure、Docker、Lambda、AWS にデプロイ
  • バーコードとQRコードを読み取る
  • XHTMLにOCRをエクスポート
  • 検索可能なPDFドキュメントにOCRをエクスポート
  • マルチスレッドのサポート
  • 125の国際言語はすべてNuGetまたはOcrDataファイルで管理されています。
  • 画像、座標、統計情報、フォントを抽出する。 テキストだけではありません。

  • 商用および専有アプリケーション内でTesseract OCRを再配布するために使用できます。

    IronOCRは、デジタルノイズや欠陥が含まれているかもしれない低解像度のスキャンや写真など、現実世界の画像や不完全な文書を扱う際に優れています。

    その他 無料OCR .NETプラットフォーム向けの他の.NET Tesseract APIやウェブサービスなどのライブラリは、これらの実世界のユースケースにはあまり性能が良くありません。

    Tesseract 5 を使用した OCR - C#でコードを始めよう

    以下のコードサンプルは、C#またはVB .NETを使用して画像からテキストをどれほど簡単に読み取るかを示しています。

ワンライナー

:path=/static-assets/ocr/content-code-examples/get-started/get-started-1.cs
string Text = new IronTesseract().Read(@"img\Screenshot.png").Text;
Dim Text As String = (New IronTesseract()).Read("img\Screenshot.png").Text
VB   C#

設定可能なHello World

:path=/static-assets/ocr/content-code-examples/get-started/get-started-2.cs
using IronOcr;

IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();

// Add multiple images
input.LoadImage("images/sample.jpeg");

OcrResult result = ocr.Read(input);
Console.WriteLine(result.Text);
Imports IronOcr

Private ocr As New IronTesseract()
Private OcrInput As using

' Add multiple images
input.LoadImage("images/sample.jpeg")

Dim result As OcrResult = ocr.Read(input)
Console.WriteLine(result.Text)
VB   C#

C# PDF OCR (オーシーアール)

同じアプローチを使って、任意のPDFドキュメントからテキストを抽出することもできます。

:path=/static-assets/ocr/content-code-examples/get-started/get-started-3.cs
using IronOcr;

IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();

// We can also select specific PDF page numbers to OCR
input.LoadPdf("example.pdf", Password: "password");

OcrResult result = ocr.Read(input);
Console.WriteLine(result.Text);

// 1 page for every page of the PDF
Console.WriteLine($"{result.Pages.Length} Pages");
Imports IronOcr

Private ocr As New IronTesseract()
Private OcrInput As using

' We can also select specific PDF page numbers to OCR
input.LoadPdf("example.pdf", Password:= "password")

Dim result As OcrResult = ocr.Read(input)
Console.WriteLine(result.Text)

' 1 page for every page of the PDF
Console.WriteLine($"{result.Pages.Length} Pages")
VB   C#

マルチページTIFFのためのOCR

:path=/static-assets/ocr/content-code-examples/get-started/get-started-4.cs
using IronOcr;

IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();
var pageindices = new int[] { 1, 2 };
input.LoadImageFrames("multi-frame.tiff", pageindices);
OcrResult result = ocr.Read(input);
Console.WriteLine(result.Text);
Imports IronOcr

Private ocr As New IronTesseract()
Private OcrInput As using
Private pageindices = New Integer() { 1, 2 }
input.LoadImageFrames("multi-frame.tiff", pageindices)
Dim result As OcrResult = ocr.Read(input)
Console.WriteLine(result.Text)
VB   C#

バーコードとQR

IronOCRのユニークな機能の一つは、テキストをスキャンしている最中にドキュメントからバーコードやQRコードを読み取ることができる点です。 OcrResult.OcrBarcode クラスのインスタンスは、開発者に各スキャンされたバーコードに関する詳細情報を提供します。

:path=/static-assets/ocr/content-code-examples/get-started/get-started-5.cs
using IronOcr;

IronTesseract ocr = new IronTesseract();
ocr.Configuration.ReadBarCodes = true;

using OcrInput input = new OcrInput();
input.LoadImage("img/Barcode.png");

OcrResult Result = ocr.Read(input);
foreach (var Barcode in Result.Barcodes)
{
    // type and location properties also exposed
    Console.WriteLine(Barcode.Value);
}
Imports IronOcr

Private ocr As New IronTesseract()
ocr.Configuration.ReadBarCodes = True

Using input As New OcrInput()
	input.LoadImage("img/Barcode.png")
	
	Dim Result As OcrResult = ocr.Read(input)
	For Each Barcode In Result.Barcodes
		' type and location properties also exposed
		Console.WriteLine(Barcode.Value)
	Next Barcode
End Using
VB   C#

画像上の特定の領域でのOCR

IronOCRのすべてのスキャンおよび読取りメソッドは、読み取りたいページの特定の部分を正確に指定する機能を提供します。 標準化されたフォームを確認するときに非常に役立ち、非常に多くの時間を節約し、効率を向上させることができます。

クロップ領域を使用するには、System.Drawing.Rectangle オブジェクトを使用できるように System.Drawing へのシステム参照を追加する必要があります。

:path=/static-assets/ocr/content-code-examples/get-started/get-started-6.cs
using IronOcr;

IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();

// Dimensions are in pixel
var contentArea = new System.Drawing.Rectangle() { X = 215, Y = 1250, Height = 280, Width = 1335 };

input.LoadImage("document.png", contentArea);

OcrResult result = ocr.Read(input);
Console.WriteLine(result.Text);
Imports IronOcr

Private ocr As New IronTesseract()
Private OcrInput As using

' Dimensions are in pixel
Private contentArea = New System.Drawing.Rectangle() With {
	.X = 215,
	.Y = 1250,
	.Height = 280,
	.Width = 1335
}

input.LoadImage("document.png", contentArea)

Dim result As OcrResult = ocr.Read(input)
Console.WriteLine(result.Text)
VB   C#

低品質スキャン用OCR

IronOCRの OcrInput クラスは、通常のTesseractが読み取れないスキャンを修正することができます。

:path=/static-assets/ocr/content-code-examples/get-started/get-started-7.cs
using IronOcr;

IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();
var pageindices = new int[] { 1, 2 };
input.LoadImageFrames(@"img\Potter.tiff", pageindices);

// fixes digital noise and poor scanning
input.DeNoise();

// fixes rotation and perspective
input.Deskew();

OcrResult result = ocr.Read(input);
Console.WriteLine(result.Text);
Imports IronOcr

Private ocr As New IronTesseract()
Private OcrInput As using
Private pageindices = New Integer() { 1, 2 }
input.LoadImageFrames("img\Potter.tiff", pageindices)

' fixes digital noise and poor scanning
input.DeNoise()

' fixes rotation and perspective
input.Deskew()

Dim result As OcrResult = ocr.Read(input)
Console.WriteLine(result.Text)
VB   C#

検索可能なPDFとしてOCR結果をエクスポート

:path=/static-assets/ocr/content-code-examples/get-started/get-started-8.cs
using IronOcr;

IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();
input.Title = "Quarterly Report";
input.LoadImage("image1.jpeg");
input.LoadImage("image2.png");
var pageindices = new int[] { 1, 2 };
input.LoadImageFrames("image3.gif", pageindices);

OcrResult result = ocr.Read(input);
result.SaveAsSearchablePdf("searchable.pdf");
Imports IronOcr

Private ocr As New IronTesseract()
Private OcrInput As using
input.Title = "Quarterly Report"
input.LoadImage("image1.jpeg")
input.LoadImage("image2.png")
Dim pageindices = New Integer() { 1, 2 }
input.LoadImageFrames("image3.gif", pageindices)

Dim result As OcrResult = ocr.Read(input)
result.SaveAsSearchablePdf("searchable.pdf")
VB   C#

TIFFから検索可能なPDFへの変換

:path=/static-assets/ocr/content-code-examples/get-started/get-started-9.cs
using IronOcr;

IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();
var pageindices = new int[] { 1, 2 };
input.LoadImageFrames("example.tiff", pageindices);
ocr.Read(input).SaveAsSearchablePdf("searchable.pdf");
Imports IronOcr

Private ocr As New IronTesseract()
Private OcrInput As using
Private pageindices = New Integer() { 1, 2 }
input.LoadImageFrames("example.tiff", pageindices)
ocr.Read(input).SaveAsSearchablePdf("searchable.pdf")
VB   C#

OCR結果をHTMLとしてエクスポート

:path=/static-assets/ocr/content-code-examples/get-started/get-started-10.cs
using IronOcr;

IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();
input.Title = "Html Title";
input.LoadImage("image1.jpeg");

OcrResult Result = ocr.Read(input);
Result.SaveAsHocrFile("results.html");
Imports IronOcr

Private ocr As New IronTesseract()
Private OcrInput As using
input.Title = "Html Title"
input.LoadImage("image1.jpeg")

Dim Result As OcrResult = ocr.Read(input)
Result.SaveAsHocrFile("results.html")
VB   C#

OCR画像強調フィルター

IronOCRは、OCRパフォーマンスを向上させるためにOcrInputオブジェクトに独自のフィルターを提供します。

画像強調のコード例

:path=/static-assets/ocr/content-code-examples/get-started/get-started-11.cs
using IronOcr;

IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();
input.LoadImage("LowQuality.jpeg");

// fixes digital noise and poor scanning
input.DeNoise();

// fixes rotation and perspective
input.Deskew();

OcrResult result = ocr.Read(input);
Console.WriteLine(result.Text);
Imports IronOcr

Private ocr As New IronTesseract()
Private OcrInput As using
input.LoadImage("LowQuality.jpeg")

' fixes digital noise and poor scanning
input.DeNoise()

' fixes rotation and perspective
input.Deskew()

Dim result As OcrResult = ocr.Read(input)
Console.WriteLine(result.Text)
VB   C#

OCR画像フィルターのリスト

IronOCRに組み込まれているOCR性能を向上させる入力フィルターには以下が含まれます:

  • OcrInput.Rotate(オーシーアール入力.ローテート)( double degrees (度)) - 画像を時計回りに度数で回転させます。反時計回りにする場合は負の数値を使用してください。
  • OcrInput.Binarize()このイメージフィルターは、すべてのピクセルを白または黒に変換し、中間色を排除します。 OCRのパフォーマンスをテキストと背景のコントラストが非常に低い場合に改善する可能性があります。
  • OcrInput.ToGrayScale()** - この画像フィルターは、すべてのピクセルをグレースケールの色合いに変換します。 OCRの精度を向上させる可能性は低いが、速度の向上は期待できる。
  • OcrInput.Contrast(OCR入力コントラスト)()** - コントラストを自動的に向上させます。 このフィルターは、低コントラストのスキャンにおいて、OCRの速度と精度を向上させることがよくあります。
  • **OcrInput.DeNoise - ノイズ除去()デジタルノイズを除去します。このフィルターは、ノイズが予想される場合にのみ使用してください。
  • OcrInput.Invert()** - すべての色を反転します。 例えば、白が黒になります:黒が白になります。
  • OcrInput.Dilate(膨張)() - 高度な形態素解析。 「Dilation(膨張処理)は、画像内のオブジェクトの境界にピクセルを追加します。」 「Erode」の反対語
  • OcrInput.Erodeエロージョン()** - 高度な形態素解析。 エロージョンはオブジェクトの境界上のピクセルを削除します。 ダイレートの逆です。
  • OcrInput.Deskew()画像を正しい向きに回転させ、直交させます。 これは、OCRに非常に役立ちます。なぜなら、Tesseractの傾きの許容範囲は5度程度と低いためです。
  • OcrInput.EnhanceResolution - 低品質な画像の解像度を強化します。 このフィルターはあまり必要ありません。なぜなら、OcrInput.MinimumDPIOcrInput.TargetDPI が低解像度の入力を自動的に検出し、解決するためです。
  • EnhanceResolution は、低解像度の画像を自動的に検出する設定です。 (275 dpi未満) 画像を自動的に拡大し、すべてのテキストを鮮明にすることで、OCRライブラリによって完璧に読み取れるようにします。 この操作自体は時間がかかりますが、一般的に画像に対するOCR操作の全体的な時間を短縮します。
  • 言語 IronOCRは22の国際言語パックに対応しており、言語設定を使用してOCR操作に適用する1つまたは複数の言語を選択できます。
  • 戦略 IronOCRは2つの戦略をサポートしています。 ドキュメントのスキャンにおいて、迅速かつあまり正確でない方法を選択するか、あるいは、文の中の単語間の統計的関係を考慮してOCRテキストの精度を自動的に向上させるためにいくつかの人工知能モデルを使用する高度な戦略を選択することができます。
  • ColorSpaceは、グレースケールまたはカラーでOCRを行うことが選択できる設定です。 一般的に、グレースケールが最適なオプションです。 しかし、似たような色相を持つテキストや背景がある場合でも、完全なカラースペースを使用するとより良い結果を得ることができます。
  • DetectWhiteTextOnDarkBackgrounds. 一般的に、すべてのOCRライブラリは白い背景に黒いテキストが表示されることを期待しています。 この設定により、IronOCRはネガティブ画像や白い文字がある暗いページを自動的に検出して読み取ることができます。
  • InputImageType. この設定は、開発者にOCRライブラリが完全なドキュメントを見ているのか、スクリーンショットなどのスニペットを見ているのかを示すように指示することを可能にします。
  • RotateAndStraightenは、IronOCRがドキュメントを読み取る際に使用する高度な設定です。この設定により、回転しているだけではなく、テキストドキュメントの写真のように斜めになっている場合でも、読み取ることが可能になります。
  • ReadBarcodesは、IronOCRがテキストを読み取る際に、ページ上のバーコードおよびQRコードを自動的に読み取ることを可能にする便利な機能であり、大幅な追加時間の負担をかけることなく実行されます。

  • ColorDepth。 この設定は、OCRライブラリが色の深さを判定するために使用する1ピクセルあたりのビット数を決定します。 色深度を高くするとOCRの品質が向上する可能性がありますが、OCR操作の完了に必要な時間も増加します。

125 言語パック

IronOCRは、DLLとして配布される言語パックを通じて125の国際言語をサポートしています。 このウェブサイトからダウンロードしましたまたは、から NuGet パッケージマネージャー.

ドイツ語、フランス語、英語、中国語、日本語を含む多くの言語があります。 パスポートMRZ、MICRチェック、金融データ、ナンバープレートなどのための専門的な言語パックが存在します。 また、自分で作成したものを含む任意のtesseractの「.traineddata」ファイルも使用できます。

言語例

:path=/static-assets/ocr/content-code-examples/get-started/get-started-12.cs
using IronOcr;

// PM> Install IronOcr.Languages.Arabic
IronTesseract ocr = new IronTesseract();
ocr.Language = OcrLanguage.Arabic;

using OcrInput input = new OcrInput();

var pageindices = new int[] { 1, 2 };
input.LoadImageFrames("img/arabic.gif", pageindices);
// Add image filters if needed
// In this case, even thought input is very low quality
// IronTesseract can read what conventional Tesseract cannot.
OcrResult result = ocr.Read(input);
// Console can't print Arabic on Windows easily.
// Let's save to disk instead.
result.SaveAsTextFile("arabic.txt");
Imports IronOcr

' PM> Install IronOcr.Languages.Arabic
Private ocr As New IronTesseract()
ocr.Language = OcrLanguage.Arabic

Using input As New OcrInput()
	
	Dim pageindices = New Integer() { 1, 2 }
	input.LoadImageFrames("img/arabic.gif", pageindices)
	' Add image filters if needed
	' In this case, even thought input is very low quality
	' IronTesseract can read what conventional Tesseract cannot.
	Dim result As OcrResult = ocr.Read(input)
	' Console can't print Arabic on Windows easily.
	' Let's save to disk instead.
	result.SaveAsTextFile("arabic.txt")
End Using
VB   C#

複数言語の例

複数の言語を同時に使用してOCRを行うことも可能です。これにより、ユニコード文書内の英語のメタデータやURLを取得するのに非常に役立ちます。

:path=/static-assets/ocr/content-code-examples/get-started/get-started-13.cs
using IronOcr;

// PM> Install IronOcr.Languages.ChineseSimplified
IronTesseract ocr = new IronTesseract();
ocr.Language = OcrLanguage.ChineseSimplified;

// We can add any number of languages
ocr.AddSecondaryLanguage(OcrLanguage.English);

using OcrInput input = new OcrInput();
input.LoadPdf("multi-language.pdf");
OcrResult result = ocr.Read(input);
result.SaveAsTextFile("results.txt");
Imports IronOcr

' PM> Install IronOcr.Languages.ChineseSimplified
Private ocr As New IronTesseract()
ocr.Language = OcrLanguage.ChineseSimplified

' We can add any number of languages
ocr.AddSecondaryLanguage(OcrLanguage.English)

Using input As New OcrInput()
	input.LoadPdf("multi-language.pdf")
	Dim result As OcrResult = ocr.Read(input)
	result.SaveAsTextFile("results.txt")
End Using
VB   C#

詳細なOCR結果オブジェクト

IronOCRは各OCR処理のためにOCR結果オブジェクトを返します。 一般的に、開発者はこのオブジェクトのテキストプロパティのみを使用して、画像からスキャンされたテキストを取得します。 しかし、OCR結果のDOMはこれよりもはるかに高度です。

:path=/static-assets/ocr/content-code-examples/get-started/get-started-14.cs
using IronOcr;

IronTesseract ocr = new IronTesseract();

// Must be set to true to read barcode
ocr.Configuration.ReadBarCodes = true;
using OcrInput input = new OcrInput();
var pageindices = new int[] { 1, 2 };
input.LoadImageFrames(@"img\sample.tiff", pageindices);

OcrResult result = ocr.Read(input);
var pages = result.Pages;
var words = pages[0].Words;
var barcodes = result.Barcodes;
// Explore here to find a massive, detailed API:
// - Pages, Blocks, Paraphaphs, Lines, Words, Chars
// - Image Export, Fonts Coordinates, Statistical Data, Tables
Imports IronOcr

Private ocr As New IronTesseract()

' Must be set to true to read barcode
ocr.Configuration.ReadBarCodes = True
Using input As New OcrInput()
	Dim pageindices = New Integer() { 1, 2 }
	input.LoadImageFrames("img\sample.tiff", pageindices)
	
	Dim result As OcrResult = ocr.Read(input)
	Dim pages = result.Pages
	Dim words = pages(0).Words
	Dim barcodes = result.Barcodes
	' Explore here to find a massive, detailed API:
	' - Pages, Blocks, Paraphaphs, Lines, Words, Chars
	' - Image Export, Fonts Coordinates, Statistical Data, Tables
End Using
VB   C#

パフォーマンス

IronOCRは、パフォーマンス調整や入力画像の大幅な変更なしにそのまま動作します。

速度は驚異的: IronOCR.2020 + は以前のバージョンに比べて最大10倍速く、エラーが250%以上少なくなっています。

詳しくはこちら

C#、VB、F#、またはその他の.NET言語でOCRについて学ぶには、以下をご参照ください コミュニティチュートリアルをお読みください, で、IronOCRをどのように使用するかの実際の例を挙げ、このライブラリを最大限に活用するためのニュアンスを示すことができます。

全ての .NET開発者のためのAPIリファレンス も利用可能です。