C# と VB.NET で OCR を始めよう
IronOCR は、.NET プラットフォーム ソフトウェア開発者が画像や PDF ドキュメントからテキストを認識して読み取ることができる C# ソフトウェア ライブラリです。 これは、最も高度なTesseractエンジンを使用した純粋な.NET OCRライブラリです。
インストール
NuGetパッケージマネージャでインストール
IronOcr を Visual Studio にインストールするか、NuGet パッケージ マネージャーを使用してコマンド ラインでインストールします。 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# 開発者が Vanilla 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ライブラリは、これらの実際の使用ケースではそれほど優れたパフォーマンスを発揮しません。
Tesseract 5 を使用した OCR - C# でコーディングを始めましょう
以下のコード サンプルは、C# または VB .NET を使用して画像からテキストを簡単に読み取る方法を示しています。
ワンライナー
:path=/static-assets/ocr/content-code-examples/get-started/get-started-1.csstring 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.csusing 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.csusing 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.csusing 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.csusing 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.Rectangleオブジェクトを使用できるように、 System.Drawingへのシステム参照を追加する必要があります。
:path=/static-assets/ocr/content-code-examples/get-started/get-started-6.csusing 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.csusing 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.csusing 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.csusing 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.csusing 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.csusing 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 プロセスにおける色深度のピクセルあたりのビット数を決定します。 深度を高くすると品質は向上しますが、処理時間も長くなります。
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 リファレンスも利用可能です。







