C#でOCRを高速化する設定
IronOCRの高速設定では、EnglishFast言語モードを使用し、バーコードの読み取りなど不要な機能を無効にします。以下の10枚の画像を使ったベンチマークでは、精度への影響を抑えながら、OCR処理を約17%高速化できました。この設定は、大量の画像を短時間で処理する用途に適しています。
IronOCRは既定の設定ですぐに使用できます。精度よりも処理速度を重視する場合は、高速設定を利用できます。精度への影響を抑えながら、標準のOCR設定よりも読み取りを大幅に高速化できます。
この記事では、高速設定の方法を紹介し、IronOCRの高速設定と標準設定のベンチマーク結果を比較します。スキャンした文書、PDF、画像のいずれを処理する場合も、これらの最適化でアプリケーションのパフォーマンスを大幅に向上させられます。
OCR高速設定のセットアップ方法
- NuGetを使ってOCRライブラリをインストールし、
IronOcrをセットアップします。 - OCRエンジンを初期化します。
LanguageをEnglishFastに設定します。ReadBarCodesプロパティをfalseに設定します。- 画像を読み込み、テキストを抽出します。
高速設定の中心となるのは、Languageプロパティです。LanguageプロパティにOcrLanguage.EnglishFastを指定すると、精度がわずかに低下する可能性はありますが、速度を優先できます。IronOCRで一括読み取りを大幅に高速化でき、処理時間が重要なミッションクリティカルなアプリケーションに役立ちます。
高速な言語モデルを指定したうえで、ReadBarCodesなどの不要な機能を無効にすると、さらに高速化できます。ページ分割はIronOCRの自動検出に任せると設定を簡略化できます。より高度なオプションについては、Tesseract詳細設定ガイドをご覧ください。
以下のコード例は、次の入力画像を処理します:
どの入力フォーマットを使用すべきですか?

高速設定のコード例
-
1IronOCR を NuGet Package Manager でインストールします
-
2このコード スニペットをコピーして実行します。
/* :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);Imports IronOcr Imports System Dim 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 ocrInput As New OcrInput() ocrInput.LoadImage("image.png") Dim ocrResult = ocrTesseract.Read(ocrInput) Console.WriteLine(ocrResult.Text) End UsingImports IronOcr Imports System Dim 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 ocrInput As New OcrInput() ocrInput.LoadImage("image.png") Dim ocrResult = ocrTesseract.Read(ocrInput) Console.WriteLine(ocrResult.Text) End Using -
3実際の環境でテストするためにデプロイする
今日からご自身のプロジェクトでIronOCRを使い始めるには、無料トライアルをお試しください。
どのような出力を期待できますか?

上の画像から抽出したテキストです。OCRエンジンは元の書式や構造を保ちながら、小説の本文を正確に読み取ります。この例のように鮮明でコントラストの高いテキストは、高速設定でも良好な結果が得られます。
高速設定と標準設定の比較
高速設定が実際の処理に与える影響を確認するために、標準設定とのベンチマークを実施しました。数段落ずつを含むサンプル画像10枚を使って処理性能を比較し、高速設定のトレードオフを確認します。
標準的な設定では、スピード指向のプロパティを適用せず、デフォルトの設定でIronTesseractを初期化します。 このベンチマークアプローチは、OCR操作をリアルタイムで監視する方法を示すパフォーマンストラッキングガイドに似ています。
こちらがテストを実行するために使用するサンプル入力です。これらの画像は、複数ページの文書やバッチ操作を処理する際に遭遇するであろう典型的な文書シナリオを表しています。
ベンチマークをどのように実行しますか?
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}");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}")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}")このベンチマーク・コードは、いくつかの重要な概念を示しています:
- バッチ処理: コードは複数の画像を一つの操作で処理します。これは、並列処理を利用してさらに速度向上を図る方法を示すマルチスレッドOCRの例に似ています。 この2つの設定は異なる軸で作用し、うまく組み合わさります:高速設定は精度を少し犠牲にして速度を重視し、
MaxDegreeOfParallelismはスループットを減らしてメモリ使用量を削減します。 - 性能測定:
Stopwatchクラスを使用して、設定の比較に必要な処理時間をミリ秒単位で測定します。 - 結果の記録: コンソール出力とファイル出力の両方により、結果を後で分析し、設定間の精度の違いを確認できます。
どのような性能向上を期待できますか?
| モード | 合計時間 | 画像1枚あたりの平均時間 | 標準設定に対する時間短縮率 | 標準設定に対する精度向上率 |
|---|---|---|---|---|
| 標準 | 10.40秒 | 1.040秒 | Baseline | Baseline |
| 高速 | 8.60秒 | 0.860秒 | +17.31%(時間短縮) | +0%(同等) |
同じ10枚の画像を使ったベンチマークでは、標準設定で10.40秒かかった処理が、高速設定では8.60秒で完了し、処理時間が17.31%短縮されました。これらの鮮明でコントラストの高い画像では、両設定のテキスト出力は同一でした。低品質またはノイズの多い画像では、EnglishFastで認識精度が低下する場合があります。
結果を確認するために、高速テキスト出力と標準テキスト出力の両方をダウンロードすることができます。
高速設定が適している場面
高速設定は、特に次のような用途に適しています。
- 数千ページを短時間で処理する大量の文書処理。
- 応答時間が重要なリアルタイムアプリケーション。
- ユーザー操作にすばやく応答する必要があるwebアプリケーション。
- タイトなスケジュールで稼働するバッチ処理システム。
複数の言語や低品質のスキャン画像、車両のナンバープレートやパスポートなどの特殊な文書を扱う複雑な用途では、精度を優先して標準設定の使用を検討してください。
IronOCRでは、いくつかのプロパティを変更するだけで設定を切り替えられます。コードを大きく変更せずに、異なるパフォーマンス要件に対応できます。
よくある質問
C#におけるIronOCRの迅速な構成の主な利点は何ですか?
IronOCRの迅速な構成を使用する主な利点は、ベンチマークテストで示されているように、約17%の性能向上です。これは精度への影響を最小限に抑えた状態で達成されるため、速度が重要な大量のOCRタスクに最適です。
IronOCRのEnglishFastモードはどのようにOCR処理速度を向上させますか?
IronOCRのEnglishFastモードは、言語処理を簡素化することで速度を優先します。それにより、わずかな精度のトレードオフを伴いつつも、より迅速なテキスト抽出を可能にします。これは、明瞭でコントラストの高いテキストにはほとんど影響を与えません。
迅速な構成でバーコード読み取りのような不要な機能を無効にすべき理由は?
IronOCRの迅速な構成でバーコード読み取りのような不要な機能を無効にすることで、処理のオーバーヘッドが軽減され、OCRエンジンがテキスト抽出に専念できるようになり、その結果プロセス全体がより高速化されます。
IronOCRの迅速な構成から最も利益を得るアプリケーションのタイプは?
迅速な構成から最も利益を得るアプリケーションには、高量の文書処理、リアルタイムシステム、迅速な応答を必要とするWebアプリケーション、およびスケジュールが厳しいバッチ処理システムが含まれます。
IronOCRは迅速な構成で精度をどのように維持しつつ速度を向上させますか?
IronOCRは、EnglishFastモードを使用して言語処理を最適化し、情報を抽出するときにテキスト領域に焦点を当てることで精度を維持し、さらに高速な速度でも高コントラストのテキストが正確に読み取られることを保証します。
IronOCRの迅速な構成を設定する際の手順は何ですか?
IronOCRの迅速な構成を設定するために:NuGetを介してOCRライブラリをインストールし、OCRエンジンを初期化し、LanguageをEnglishFastに設定し、ReadBarCodesプロパティを無効にし、画像の読み込みと処理を進めます。
IronOCRの迅速な構成はバッチ処理を効率的に処理できますか?
はい、IronOCRの迅速な構成は効率的なバッチ処理を目的として設計されており、複数の画像を単一操作で処理することができ、1枚あたりの計算負荷が軽減されるため、速度が向上します。
ベンチマークテストは迅速な構成の性能をどのように示しますか?
サンプル画像のセットを使用したベンチマークテストは、迅速な構成が標準モードよりも17%早く画像を処理できることを明らかにし、その優れた速度をテキスト出力精度を損なうことなく示しています。
標準のOCR構成は、いつ迅速な構成よりも優先されるべきですか?
標準のOCR構成は、高精度が求められるシナリオ、例えば低品質なスキャンの処理、複数言語の認識、またはパスポートやナンバープレートのような特殊な文書の処理において使用されるべきです。
IronOCRでは迅速な構成と標準構成を簡単に切り替えることができますか?
はい、IronOCRでは、いくつかのプロパティを変更することで、簡単に構成を切り替えることができ、複雑なコードの調整を必要とせずに、さまざまな性能ニーズに迅速に適応できます。
Curtis Chauは、カールトン大学でコンピュータサイエンスの学士号を取得し、Node.js、TypeScript、JavaScript、およびReactに精通したフロントエンド開発を専門としています。直感的で美しいユーザーインターフェースを作成することに情熱を持ち、Curtisは現代のフレームワークを用いた開発や、構造の良い視覚的に魅力的なマニュアルの作成を楽しんでいます。