Get Started with OCR in C# and VB.NET
IronOCR は、.NET プラットフォーム ソフトウェア開発者が画像や PDF ドキュメントからテキストを認識して読み取ることができる C# ソフトウェア ライブラリです。 これは、最も高度なTesseractエンジンを使用した純粋な.NET OCRライブラリです。
インストール
NuGetパッケージマネージャでインストール
Visual StudioまたはコマンドラインでNuGet Package Managerを使用してIronOCRをインストールします。 Visual Studio で、次のコマンドを使用してコンソールに移動します。
- ツール ->
- NuGet パッケージ マネージャー ->
- パッケージマネージャーコンソール
Install-Package IronOcr
NuGet上のIronOCRを確認して、バージョン更新やインストールについての詳細を確認してください。
さまざまなプラットフォームで利用できる他のIronOCR NuGet パッケージがあります。
Linux および macOS 用の IronOcr.Extensions.AdvancedScan
このパッケージは、LinuxとMacを使用し、IronOCRの高度な機能を利用したいユーザー向けです。
- Linux: https://www.nuget.org/packages/IronOcr.Extensions.AdvancedScan.Linux
- MacOS: https://www.nuget.org/packages/IronOcr.Extensions.AdvancedScan.MacOs
トラブルシューティング
このパッケージの新しいアップデートにより、IronOCR はパッケージ内の OpenCV 依存関係を結合して合理化しますが、現在 OpenCV 依存関係をインポートしている開発者は次のエラーを受け取ります。
The type of namespace name `OpenCvSharp` could not be found(are you missing a using directive or an assembly reference)
OpenCV 名前空間を安全に削除すると、問題は解決されます。
IronOCR .ZIPをダウンロード
代わりに、.ZIP ファイル経由で IronOCR をダウンロードすることもできます。 クリックするとDLL を直接ダウンロードします。 .zip をダウンロードしたら:
.NET Framework 4.0+ のインストール手順:
- net40フォルダ内のIronOcr.dllをプロジェクトに含めます
-
次にアセンブリ参照を追加します:
- システム構成
- システム.描画
- システム.Web
.NET Standard、.NET Core 2.0+、.NET 5 の手順
- netstandard2.0フォルダ内のIronOcr.dllをプロジェクトに含めます
-
次に、NuGet パッケージ参照を追加します。
- システム.描画.Common 4.7 以上
IronOCRインストーラーをダウンロードする(Windowsのみ)
別の選択肢として、IronOCRインストーラーをダウンロードし、必要なリソースをインストールします。 このオプションは Windows システム専用であることに注意してください。 インストーラーをダウンロードするにはここをクリックしてください。 .zip をダウンロードしたら:
.NET Framework 4.0+ のインストール手順:
- net40フォルダ内のIronOcr.dllをプロジェクトに含めます
-
次にアセンブリ参照を追加します:
- システム構成
- システム.描画
- システム.Web
.NET Standard、.NET Core 2.0+、.NET 5 の手順
- netstandard2.0フォルダ内のIronOcr.dllをプロジェクトに含めます
-
次に、NuGet パッケージ参照を追加します。
- システム.描画.Common 4.7 以上
IronOCRを選ぶ理由
IronOCR は、簡単にインストールでき、完全で、十分に文書化された .NET ソフトウェア ライブラリです。
IronOCR を選択すると、外部 Web サービスを使用したり、継続的な料金を支払ったり、機密文書をインターネット経由で送信したりすることなく、99.8% 以上の OCR 精度を実現できます。
なぜC#開発者が標準のTesseractよりIronOCRを選ぶのか:
- 単一のDLLまたはNuGetとしてインストール
- Tesseract 5、4、および 3 エンジンがすぐに使用できます。
- 精度99.8% は通常の Tesseract を大幅に上回ります。
- 驚異的なスピードとマルチスレッド
- MVC、WebApp、デスクトップ、コンソール、サーバーアプリケーションと互換性があります
- 作業に必要なEXEやC++コードはありません
- 完全なPDF OCRサポート
- ほぼすべての画像ファイルやPDFでOCRを実行
- .NET Core、Standard、Framework の完全サポート
- Windows、Mac、Linux、Azure、Docker、Lambda、AWS にデプロイ可能
- バーコードとQRコードを読み取る
- OCR結果をXHTMLとしてエクスポート
- OCRを検索可能なPDF文書にエクスポート
- マルチスレッドサポート
- 125の国際言語はすべてNuGetまたはOcrDataファイルで管理されます
- 画像、座標、統計、フォントを抽出します。 テキストだけではありません。
- 商用および独自のアプリケーション内で Tesseract OCR を再配布するために使用できます。
IronOCR は、現実世界の画像や写真などの不完全な文書、あるいはデジタル ノイズや不完全さがある可能性のある低解像度のスキャンを処理する場合に威力を発揮します。
他の .NET Tesseract API や Web サービスなど、.NET プラットフォーム用の他の無料 OCRライブラリは、これらの実際の使用ケースではそれほど優れたパフォーマンスを発揮しません。
OCR with Tesseract 5 - Start Coding in 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
設定可能な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)
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")
マルチページ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)
バーコードと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
画像の特定領域のOCR
IronOCR のすべてのスキャンおよび読み取り方法では、ページのどの部分からテキストを読み取りたいかを正確に指定できます。 これは、標準化されたフォームを参照する場合に非常に役立ち、多くの時間を節約して効率を向上させることができます。
クロップ領域を使用するには、System.Drawing へのシステム参照を追加して System.Drawing.Rectangle オブジェクトを使用する必要があります。
: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)
低品質スキャン用の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)
OCR結果を検索可能なPDFとしてエクスポートする
: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")
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")
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")
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)
OCR画像フィルターのリスト
IronOCR に組み込まれている OCR パフォーマンスを向上させる入力フィルターには次のものがあります。
OcrInput.Rotate(double degrees)- 画像を時計回りに数度回転させます。反時計回りに回転させるには負の数を使用します。OcrInput.Binarize()- このフィルターは、すべてのピクセルを白黒のどちらかに変換し、中間色がないため、非常に低コントラストの画像でOCRのパフォーマンスを向上させる可能性があります。OcrInput.ToGrayScale()- すべてのピクセルをグレースケールの色合いに変換します。 精度は向上しないかもしれませんが、速度は向上する可能性があります。OcrInput.Contrast()- 自動的にコントラストを増加させ、低コントラストのスキャンでの速度と精度をしばしば向上させます。OcrInput.DeNoise()- デジタルノイズを取り除きます。ノイズが予想される場合のみお勧めです。OcrInput.Invert()- すべての色を反転させます(白は黒になり、その逆も同様)。OcrInput.Dilate()- 形態学を高度化し、オブジェクトの境界にピクセルを追加します。これは、浸食 (Erode) の反対です。OcrInput.Erode()- 形態学を高度化し、オブジェクトの境界からピクセルを除去します。これは、膨張 (Dilate) の反対です。OcrInput.Deskew()- 画像を正しい向きに回転させます。 Tesseract のスキュー許容範囲が限られているため便利です。OcrInput.EnhanceResolution- 低品質の画像の解像度を向上させます。 この設定は通常、低 DPI 入力を自動的に管理するために使用されます。EnhanceResolutionは275 dpi以下の低解像度画像を検出し、拡大して文字をシャープにし、OCRの結果を改善します。 時間はかかりますが、全体的な OCR 操作時間が短縮されることが多いです。Language- 22の国際言語パックから選択できます。Strategy- 単語の統計的関係に基づいて、速くて精度の低い戦略とAIを使用した精度のある高度な戦略を選択できます。ColorSpace- グレースケールまたはカラーでOCRを選択します; グレースケールは一般的に最適ですが、特定のコントラストのシナリオではカラーの方が適している場合があります。DetectWhiteTextOnDarkBackgrounds- 負の画像に対応し、暗い背景に白い文字を自動的に検出し読取します。InputImageType- OCRライブラリをガイドし、ドキュメント全体またはスニペット上で作業しているかを指定します。RotateAndStraighten- IronOCR に回転したり、視点の歪みがあるドキュメントを適切に処理させます。ReadBarcodes- テキストスキャンと同時にバーコードとQRコードを読み取り、自動的に実行し、顕著に時間が増加することはありません。ColorDepth- OCRプロセスでの色深度における1ピクセルあたりのビット数を決定します。 深度を高くすると品質は向上しますが、処理時間も長くなります。
125 言語パック
IronOCR は、DLL として配布される言語パックを介して125 の国際言語をサポートしており、この Web サイトまたはNuGet パッケージ マネージャーからダウンロードできます。
言語には、ドイツ語、フランス語、英語、中国語、日本語などがあります。 専門的な言語パックはMRZ、MICRチェック、財務データ、ナンバープレート等に存在します。加えて、カスタムのTesseract".traineddata"ファイルを使用できます。
言語の例
// Reference to the path of the source file that demonstrates setting language packs for OCR
:path=/static-assets/ocr/content-code-examples/get-started/get-started-12.cs
// Reference to the path of the source file that demonstrates setting language packs for OCR
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");
' Reference to the path of the source file that demonstrates setting language packs for OCR
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
複数言語の例
複数の言語を同時にOCR処理することも可能です。これにより、Unicode文書内の英語のメタデータやURLのOCR処理能力が向上します。
// Reference to the path of the source file that demonstrates multi-language OCR
:path=/static-assets/ocr/content-code-examples/get-started/get-started-13.cs
// Reference to the path of the source file that demonstrates multi-language OCR
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");
' Reference to the path of the source file that demonstrates multi-language OCR
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
詳細なOCR結果オブジェクト
IronOCR は、各操作に対して OCR 結果オブジェクトを返します。 一般に、開発者はスキャンされたテキストを取得するためにText プロパティにアクセスします。 ただし、結果オブジェクトにはさらに詳細な情報が含まれています。
// Reference to the path of the source file demonstrating detailed OCR result object usage
:path=/static-assets/ocr/content-code-examples/get-started/get-started-14.cs
// Reference to the path of the source file demonstrating detailed OCR result object usage
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
' Reference to the path of the source file demonstrating detailed OCR result object usage
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
パフォーマンス
IronOCR は、パフォーマンスの調整や画像の変更を必要とせず、すぐに使用できます。
速度は驚異的です:IronOCR.2020+は最大10倍の速さで、以前のビルドよりも250%以上少ないエラーを出します。
もっと詳しく知る
C#、VB、F#、またはその他の .NET 言語での OCR の詳細については、コミュニティ チュートリアルをお読みください。このチュートリアルでは、IronOCR の実際の使用例を示し、ライブラリの最適化のニュアンスを説明しています。
.NET 開発者向けの完全な API リファレンスも利用可能です。

