在 C# 中使用快速 OCR 設定提升效能
使用 EnglishFast 語言模式,並關閉條碼讀取等不必要的功能,即可加快 IronOCR 的 OCR 處理速度,且對準確度的影響很小。下方的 10 張影像基準測試中,處理時間縮短約 17%。這項最佳化適合時間緊迫的大量處理作業。
IronOCR 採用預設設定即可順利運作。若您優先考量速度,可改用快速設定。相較於標準 OCR 設定,快速設定能顯著提升掃描速度,且對準確度的影響很小。
本文示範如何啟用快速設定,並比較 IronOCR 在快速與標準設定下的基準測試結果。無論處理的是掃描文件、PDF或影像,這些最佳化措施都能顯著提升應用程式效能。
如何啟用快速 OCR 設定
- 透過 NuGet 安裝 OCR 程式庫,以設定
IronOcr - 初始化 OCR 引擎
- 將
Language設為EnglishFast - 將
ReadBarCodes屬性設定為false - 載入影像並擷取文字
快速設定主要取決於 Language 屬性。將 Language 屬性設為 OcrLanguage.EnglishFast,即可優先提高辨識速度,準確度可能會略微下降。這讓 IronOCR 能更快完成大量辨識作業,尤其適用於對時間要求嚴格的關鍵應用程式。
選用快速語言模式後,您還可以關閉 ReadBarCodes 等不需要的功能,進一步加快辨識。讓 IronOCR 自動偵測頁面分割方式,可簡化設定。如需進階選項,請參閱 Tesseract 詳細設定指南。
下方的程式碼範例會處理這張輸入影像:
我應該使用什麼輸入格式?

如何以程式碼啟用快速設定?
-
1使用 IronOCR 安裝 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 範例中利用平行處理加速的做法。快速設定與平行處理可搭配使用。前者以少量準確度換取速度,後者則透過
MaxDegreeOfParallelism以較低的處理量換取較少的記憶體用量。 - 效能量測:使用
Stopwatch類別將執行時間量測到毫秒,以便比較不同設定。 - 記錄結果:將結果輸出至主控台和檔案,方便稍後分析及驗證不同設定的辨識準確度差異。
效能可以提升多少?
| 模式 | 總時間 | 平均耗時/影像 | 相較於標準模式節省的時間 | 相較於標準模式的準確度變化 |
|---|---|---|---|---|
| 標準 | 10.40秒 | 1.040秒 | 基準 | 基準 |
| 快速 | 8.60秒 | 0.860秒 | +17.31%(耗時較短) | +0%(相同) |
基準測試顯示,快速設定能顯著提升效能。標準模式處理同一批 10 張影像共耗時 10.40 秒,快速設定只需 8.60 秒,節省了 17.31% 的時間。對於這批清晰且對比度高的樣本,兩種設定的文字輸出完全相同。若輸入影像品質較差或雜訊較多,EnglishFast 可能會降低辨識準確度。
若要驗證結果,您可以下載快速設定的文字輸出和標準設定的文字輸出。
何時應使用快速設定?
快速設定特別適合以下情境:
- 大量文件處理,數千頁的文件需要快速處理
- 即時應用程式,需要在短時間內回應
- Web 應用程式,需要維持流暢的操作體驗
- 批次處理系統,需要在緊迫的時限內完成作業
若需辨識多種語言、處理低品質掃描檔,或讀取車牌及護照等特殊文件,可考慮使用標準設定,以維持最高的辨識準確度。
IronOCR 只需調整幾個屬性即可切換設定,讓應用程式適應不同的效能需求,無須大幅修改程式碼。
常見問題
使用 IronOCR 的快速配置在 C# 中的主要好處是什麼?
使用 IronOCR 快速配置的主要好處如基準測試所示為性能提升約17%,且對準確性影響甚微,這使其非常適合需要快速處理的大量 OCR 任務。
IronOCR 的 EnglishFast 模式如何增加 OCR 處理速度?
IronOCR 的 EnglishFast 模式透過簡化語言處理以優先考量速度。它允許更快速的文字擷取,對準確性有輕微的權衡,對於清晰高對比度的文字來說幾乎可以忽略不計。
為什麼您應該在快速配置中停用如條碼閱讀等不必要的功能?
在 IronOCR 的快速配置中停用如條碼閱讀等不必要的功能可以減少處理負擔,允許 OCR 引擎專注於文字擷取,從而加速整個過程。
哪些類型的應用最受益於 IronOCR 的快速配置?
受益於快速配置的應用包括高量文件處理、需要快速響應的即時系統和網頁應用,以及排程緊湊的批次處理系統。
IronOCR 如何在使用快速配置時,在提升速度的同時維持準確性?
IronOCR 通過利用 EnglishFast 模式優化語言處理,並專注於擷取資訊的文本區域來維持準確性,確保即使在更快速度下也能準確讀取高對比度文字。
設置 IronOCR 的快速配置涉及哪些步驟?
要設置 IronOCR 的快速配置:透過 NuGet 安裝 OCR 程式庫,初始化 OCR 引擎,將 Language 設為 EnglishFast,停用 ReadBarCodes 屬性,然後繼續加載和處理影像。
IronOCR 的快速配置能高效處理批次作業嗎?
是的,IronOCR 的快速配置專為高效的批次處理而設計,由於每個影像的計算負擔減少,可在單次操作中以更快速度處理多個影像。
基準測試如何展示快速配置的性能?
使用一組範例影像的基準測試顯示,快速配置可以比標準模式更快地處理影像17%,展示了其出色速度而不影響文本輸出準確性。
何時應優先選擇標準 OCR 配置而非快速配置?
在需要高準確性的場景,諸如低品質掃描、多語言識別或處理特殊文件如護照或車牌等時,應使用標準 OCR 配置。
在 IronOCR 中,在快速和標準配置之間切換是否容易?
是的,IronOCR 允許輕鬆地通過更改幾個屬性在配置間切換,能夠快速適應不同的性能需求而無需複雜的程式碼調整。
Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。