最適なパフォーマンスを実現するC#での高速文字認識(OCR)構成
IronOCR の高速構成では、EnglishFast 言語モードを使用し、バーコード読み取りなどの不要な機能を無効にすることで、文字起こし・テキスト抽出の処理速度を最大 17% 高速化できます。 この最適化は、時間が重要な大量処理に最適です。
IronOCRはすぐに効果的に機能します。 絶対的な正確さよりもスピードが優先される場合、IronOCRは迅速な設定を提供します。 この設定により、スキャン パフォーマンスが大幅に向上し、精度への影響が最小限に抑えられるため、標準の OCR 設定よりもはるかに速くなります。
この記事では、高速コンフィギュレーションの設定方法を示し、高速IronOCRコンフィギュレーションと標準IronOCRコンフィギュレーションのベンチマーク結果を比較します。 スキャンされたドキュメント、PDF、画像を処理しているかどうかにかかわらず、これらの最適化によってアプリケーションのパフォーマンスを大幅に向上させることができます。
OCR高速設定のセットアップ方法
- IronOCRをセットアップするには、NuGetを使用してOCRライブラリをインストールします。
- OCRエンジンを初期化
- `Language`英語に設定する
- `ReadBarCodes`プロパティを`false`に設定する
- 画像を読み込み、テキストを抽出する
クイックスタート: C# で高速 OCR を構成する
高速構成の主なコンポーネントは、Language プロパティです。 Language プロパティを OcrLanguage.EnglishFast に設定すると、精度の潜在的な低下よりも速度が優先されます。 これにより、IronOCRはより迅速に一括して読むことができ、これは特に時間が重要なミッションクリティカルなアプリケーションで役立ちます。
高速言語の設定に加えて、ReadBarCodes などの不要な構成を無効にすることで、さらに速度を向上させることができます。 IronOCRはページ分割を自動検出し、設定をシンプルに保ちます。 より高度な設定オプションについては、Tesseract 詳細設定ガイドをご覧ください。
以下のコード例は、次の入力画像を処理します:
どのような入力形式を使用すればよいですか?
高速コンフィギュレーションにはどのようなコードが必要ですか?
-
IronOCR をNuGetパッケージマネージャでインストール
PM > Install-Package IronOcr -
このコード スニペットをコピーして実行します。
/* :path=/static-assets/ocr/content-code-examples/how-to/ocr-fast-configuration.cs */ using IronOcr; using System; var ocrTesseract = new IronTesseract(); // Fast Dictionary ocrTesseract.Language = OcrLanguage.EnglishFast; // Turn off unneeded options ocrTesseract.Configuration.ReadBarCodes = false; // Assume text is laid out neatly in an orthogonal document ocrTesseract.Configuration.PageSegmentationMode = TesseractPageSegmentationMode.Auto; using var ocrInput = new OcrInput(); ocrInput.LoadImage("image.png"); var ocrResult = ocrTesseract.Read(ocrInput); Console.WriteLine(ocrResult.Text); -
実際の環境でテストするためにデプロイする
今日プロジェクトで IronOCR を使い始めましょう無料トライアル
どのようなアウトプットを期待できますか?
これは上記から抽出されたテキスト出力です。 OCRエンジンは、原文の書式と構造を維持しながら、文字テキストを正確にキャプチャします。 この例のような明瞭でコントラストの高いテキストには、高速な設定が優れた結果をもたらします。
高速コンフィギュレーションは標準と比較してどうですか?
実世界への影響を実証するために、標準のパフォーマンスと高速設定のベンチマークを行いました。 それぞれ数段落を含む10セットのサンプル画像を使用して、パフォーマンスを比較し、高速コンフィギュレーションを使用した場合のトレードオフを視覚化します。
標準構成では、速度重視のプロパティを適用せずに、IronTesseract をデフォルト設定で初期化します。 このベンチマーク アプローチは、パフォーマンス追跡ガイドに似ており、OCR 操作をリアルタイムで監視する方法を示しています。
テストの実行に使用するサンプル入力です。これらの画像は、複数ページのドキュメントやバッチ操作を処理するときに遭遇する可能性のある典型的なドキュメントのシナリオを表しています。
ベンチマークを実行するには?
:path=/static-assets/ocr/content-code-examples/how-to/ocr-fast-configuration-benchmark.cs
using IronOcr;
using System;
using System.Diagnostics;
using System.IO;
// --- Tesseract Engine Setup ---
var ocrTesseract = new IronTesseract();
ocrTesseract.Language = OcrLanguage.EnglishFast;
ocrTesseract.Configuration.ReadBarCodes = false;
ocrTesseract.Configuration.PageSegmentationMode = TesseractPageSegmentationMode.Auto;
// --- 1. Define folder and get files ---
string folderPath = @"images"; // IMPORTANT: Set this to your image directory
string filePattern = "*.png"; // Change to "*.jpg", "*.bmp", etc. as needed
string outputFilePath = "ocr_results.txt"; // The new results file
// Get all image files in the directory
var imageFiles = Directory.GetFiles(folderPath, filePattern);
Console.WriteLine($"Found {imageFiles.Length} total images to process...");
Console.WriteLine($"Results will be written to: {outputFilePath}");
// --- 2. Start timer and process images, writing to file ---
// Open the output file *before* the loop for efficiency
using (StreamWriter writer = new StreamWriter(outputFilePath))
{
var stopwatch = Stopwatch.StartNew();
foreach (var file in imageFiles)
{
string fileName = Path.GetFileName(file);
using var ocrInput = new OcrInput();
ocrInput.LoadImage(file);
var ocrResult = ocrTesseract.Read(ocrInput);
// Check if any text was actually found
if (!string.IsNullOrEmpty(ocrResult.Text))
{
// Write to Console
Console.WriteLine($"--- Text found in: {fileName} ---");
Console.WriteLine(ocrResult.Text.Trim());
Console.WriteLine("------------------------------------------");
// Write to File
writer.WriteLine($"--- Text found in: {fileName} ---");
writer.WriteLine(ocrResult.Text.Trim());
writer.WriteLine("------------------------------------------");
writer.WriteLine(); // Add a blank line for readability
}
else
{
// Write to Console
Console.WriteLine($"No text found in: {fileName}");
// Write to File
writer.WriteLine($"No text found in: {fileName}");
writer.WriteLine();
}
}
stopwatch.Stop();
// --- 3. Print and write final benchmark summary ---
string lineSeparator = "\n========================================";
string title = "Batch OCR Processing Complete";
string summary = $"Fast configuration took {stopwatch.Elapsed.TotalSeconds:F2} seconds";
// Write summary to Console
Console.WriteLine(lineSeparator);
Console.WriteLine(title);
Console.WriteLine("========================================");
Console.WriteLine(summary);
// Write summary to File
writer.WriteLine(lineSeparator);
writer.WriteLine(title);
writer.WriteLine("========================================");
writer.WriteLine(summary);
if (imageFiles.Length > 0)
{
string avgTime = $"Average time per image: {(stopwatch.Elapsed.TotalSeconds / (double)imageFiles.Length):F3} seconds";
Console.WriteLine(avgTime);
writer.WriteLine(avgTime);
}
}
Console.WriteLine($"\nSuccessfully saved results to {outputFilePath}");
Imports IronOcr
Imports System
Imports System.Diagnostics
Imports System.IO
' --- Tesseract Engine Setup ---
Dim ocrTesseract As New IronTesseract()
ocrTesseract.Language = OcrLanguage.EnglishFast
ocrTesseract.Configuration.ReadBarCodes = False
ocrTesseract.Configuration.PageSegmentationMode = TesseractPageSegmentationMode.Auto
' --- 1. Define folder and get files ---
Dim folderPath As String = "images" ' IMPORTANT: Set this to your image directory
Dim filePattern As String = "*.png" ' Change to "*.jpg", "*.bmp", etc. as needed
Dim outputFilePath As String = "ocr_results.txt" ' The new results file
' Get all image files in the directory
Dim imageFiles = Directory.GetFiles(folderPath, filePattern)
Console.WriteLine($"Found {imageFiles.Length} total images to process...")
Console.WriteLine($"Results will be written to: {outputFilePath}")
' --- 2. Start timer and process images, writing to file ---
' Open the output file *before* the loop for efficiency
Using writer As New StreamWriter(outputFilePath)
Dim stopwatch = Stopwatch.StartNew()
For Each file In imageFiles
Dim fileName As String = Path.GetFileName(file)
Using ocrInput As New OcrInput()
ocrInput.LoadImage(file)
Dim ocrResult = ocrTesseract.Read(ocrInput)
' Check if any text was actually found
If Not String.IsNullOrEmpty(ocrResult.Text) Then
' Write to Console
Console.WriteLine($"--- Text found in: {fileName} ---")
Console.WriteLine(ocrResult.Text.Trim())
Console.WriteLine("------------------------------------------")
' Write to File
writer.WriteLine($"--- Text found in: {fileName} ---")
writer.WriteLine(ocrResult.Text.Trim())
writer.WriteLine("------------------------------------------")
writer.WriteLine() ' Add a blank line for readability
Else
' Write to Console
Console.WriteLine($"No text found in: {fileName}")
' Write to File
writer.WriteLine($"No text found in: {fileName}")
writer.WriteLine()
End If
End Using
Next
stopwatch.Stop()
' --- 3. Print and write final benchmark summary ---
Dim lineSeparator As String = vbLf & "========================================"
Dim title As String = "Batch OCR Processing Complete"
Dim summary As String = $"Fast configuration took {stopwatch.Elapsed.TotalSeconds:F2} seconds"
' Write summary to Console
Console.WriteLine(lineSeparator)
Console.WriteLine(title)
Console.WriteLine("========================================")
Console.WriteLine(summary)
' Write summary to File
writer.WriteLine(lineSeparator)
writer.WriteLine(title)
writer.WriteLine("========================================")
writer.WriteLine(summary)
If imageFiles.Length > 0 Then
Dim avgTime As String = $"Average time per image: {(stopwatch.Elapsed.TotalSeconds / CDbl(imageFiles.Length)):F3} seconds"
Console.WriteLine(avgTime)
writer.WriteLine(avgTime)
End If
End Using
Console.WriteLine(vbLf & $"Successfully saved results to {outputFilePath}")
このベンチマーク・コードは、いくつかの重要な概念を示しています:
1.バッチ処理: コードは、マルチスレッド OCR の例と同様に、1 回の操作で複数の画像を処理します。これは、並列処理を活用して速度をさらに向上させる方法を示しています。
2.パフォーマンス測定: Stopwatch クラスを使用すると、異なる構成を比較するために不可欠な、ミリ秒単位までの正確なタイミング測定が可能になります。
3.結果ロギング:コンソールとファイル出力の両方で、後で結果を分析し、構成間の精度の違いを確認できるようにします。
どのようなパフォーマンスの向上が期待できますか?
| モード | 合計時間 | 平均時間 / 画像 | 標準に対する時間獲得 | 標準に対する精度の向上 |
|---|---|---|---|---|
| **標準**です。 | 10.40秒 | 1.040秒 | ベースライン | ベースライン |
| **速い**。 | 8.60秒 | 0.860秒 | +17.31% (より速い) | +0% (同一) |
標準構成と高速構成のベンチマーク比較では、高速構成に大きな性能上の利点があることを示しています。 標準モードをベースライン(合計時間10.40秒)に設定することで、高速コンフィギュレーションは10枚の同じバッチをわずか8.60秒で完了させました。 これは 17.31% という大幅な時間節約を意味します。 重要なのは、このスピードの向上が品質を損なわないことでした; どちらのモードでも精度は同じで、どちらの構成でも同じテキストが出力されました。
どのような場合に Fast Configuration を使用すべきですか?
高速コンフィギュレーションは、特に次のような場合に有効です:
- 何千ページもの迅速な処理が必要な、大量の文書処理。
- 応答時間が重要なリアルタイムアプリケーション。
- レスポンシブなユーザー体験を維持する必要があるWebアプリケーション。
- タイトなスケジュールで稼働するバッチ処理システム。
複数の言語、低品質スキャン、ライセンスプレートやパスポートのような特殊なドキュメントタイプを含む、より複雑なシナリオの場合は、最大限の精度を確保するために標準設定を使用することをお勧めします。
IronOCRはコンフィギュレーションの切り替えを簡単にします - いくつかのプロパティを変更するだけで、アプリケーションはコードを大きく変更することなく、異なるパフォーマンス要件に対応できます。
よくある質問
高速OCR設定は、標準設定と比べてどのくらい高速ですか?
IronOCRの高速設定は、精度への影響を最小限に抑えながら、標準的なOCR設定と比較して最大17%の処理速度向上を達成することができます。この性能向上は、EnglishFast言語モードと不要な機能を無効にすることで達成されます。
高速OCR処理を可能にする主な設定は何ですか?
IronOcrの高速コンフィギュレーションの主な要素は、LanguageプロパティをOcrLanguage.EnglishFastに設定することです。これは、正確さにおけるわずかな潜在的コストよりもスピードを優先させるもので、一括処理やタイムクリティカルなアプリケーションに最適です。
EnglishFastモードを使用する以外に、OCR速度をさらに最適化する方法はありますか?
IronOCRの不要な機能を無効にすることで、さらに速度を向上させることができます。例えば、バーコード検出が不要な場合はReadBarCodesをfalseに設定します。また、TesseractPageSegmentationMode.Autoを使用することで、IronOCRにページ分割を自動検出させることができます。
標準設定ではなく、高速OCR設定を使用するのはどのような場合ですか?
IronOCRの高速OCRコンフィギュレーションは、時間が非常に重要であり、精度の多少のトレードオフが許容される大量処理シナリオに最適です。特に、スキャンした文書、PDF、画像を迅速に処理する必要のあるミッションクリティカルなアプリケーションに有効です。
高速コンフィギュレーションは、すべてのドキュメントタイプに対応していますか?
IronOCRの高速設定は、スキャン文書、PDF、画像など様々なタイプの文書に効果的に対応します。最適化の利点は、処理する入力フォーマットに関係なく適用されます。
高速OCRモードを使用した場合、精度は落ちますか?
IronOCRの高速コンフィギュレーションは、スキャンのパフォーマンスを大幅に向上させ、精度への影響を最小限に抑えます。EnglishFastモードを使用する場合、精度に若干のコストがかかる可能性がありますが、スピードを優先するアプリケーションでは、トレードオフの価値はしばしばあります。

