使用 C# 快速設定 OCR 以獲得最佳效能
IronOCR 的快速設定功能,透過使用 EnglishFast 語言模式並停用 BarCode 讀取等非必要功能,可在不犧牲準確性的前提下,將 OCR 處理速度提升高達 17%。 此優化方案特別適合時間至關重要的大量處理情境。
IronOCR 開箱即用,效能卓越。 當速度比絕對準確性更為重要時,IronOCR 提供快速設定方案。 此設定能在幾乎不影響精準度的情況下,大幅提升掃描效能,使其比標準 OCR 配置快得多。
本文將示範如何設定快速配置,並比較快速與標準 IronOCR 配置之間的效能測試結果。 無論您處理的是掃描文件、PDF 檔案或圖像,這些優化措施都能顯著提升應用程式的效能。
如何設定 OCR 快速設定
- 透過 NuGet 安裝 OCR 程式庫以設定
IronOcr - 初始化 OCR 引擎
- 將
語言設定為英文Fast - 將
ReadBarCodes屬性設定為false - 載入圖片並擷取文字
快速入門:在 C# 中設定 Fast OCR
快速配置的主要元件是 Language 屬性。 將 Language 屬性設定為 OcrLanguage.EnglishFast,意在優先考量速度,即使這可能導致準確性略有犧牲。 這使得 IronOCR 能夠以更快的速度進行批量讀取,對於時間至關重要的關鍵任務型應用程式而言,此功能尤為實用。
除了設定快取語言外,您還可透過停用不必要的設定(例如 ReadBarCodes)來進一步提升速度。 讓 IronOCR 自動偵測頁面分割,以簡化設定流程。 如需更進階的設定選項,請參閱我們的 Tesseract 詳細設定指南。
以下程式碼範例處理下列輸入圖片:
我應該使用哪種輸入格式?
快速設定需要哪些程式碼?
-
using NuGet 套件管理員安裝 https://www.nuget.org/packages/IronOcr
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.PageSegmentation模式 = TesseractPageSegmentation模式.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}")
此基準程式碼演示了幾個重要概念:
-
批次處理:程式碼能在單次操作中處理多張圖片,類似於我們多執行緒 OCR 的範例,該範例展示了如何利用平行處理來進一步提升速度。
-
效能測量:使用
Stopwatch類別可提供精確至毫秒級的計時測量,這對於比較不同配置至關重要。 - 結果記錄:透過控制台和檔案輸出,確保您日後能分析結果,並驗證不同設定間的準確性差異。
我可以預期獲得哪些效能提升?
| 模式 | 總時間 | 平均時間 / 圖片 | 節省時間 vs. 標準 | 精準度提升 vs. 標準 |
|---|---|---|---|---|
| 標準 | 10.40 秒 | 1.040 秒 | 基準版本 | 基準版本 |
| 快速 | 8.60 秒 | 0.860 秒 | +17.31% (更快) | +0% (完全一致) |
標準配置與快速配置之間的基準比較顯示,快速配置在效能上具有顯著優勢。 以標準模式為基準(總耗時 10.40 秒),快速配置模式僅需 8.60 秒即可完成同一批 10 張圖片的處理。 這代表節省了 17.31% 的時間。 關鍵在於,這項速度提升並未犧牲品質; 兩種模式的準確度完全一致,兩組設定產生的文字輸出完全相同。
何時該使用快速設定?
快速設定對於以下情況特別有益:
- 大量文件處理,需快速處理數千頁文件
- 回應時間至關重要的即時應用程式
- 需要維持響應式使用者體驗的網頁應用程式
- 運行於嚴格時程表下的批次處理系統
若涉及多語言、掃描品質不佳,或車牌、護照等特殊文件類型的較複雜情境,建議使用標準設定以確保最高準確度。
IronOCR 讓配置切換變得簡單——只需變更幾個屬性,您的應用程式就能適應不同的效能需求,無需進行大規模的程式碼修改。
常見問題
相較於標準設定,快速 OCR 配置的速度快了多少?
IronOCR 的快速配置模式,相較於標準 OCR 設定,處理速度可提升高達 17%,且對準確度的影響極小。此效能提升是透過 EnglishFast 語言模式,並停用不必要的功能來實現的。
什麼是能實現快速 OCR 處理的主要設定?
IronOCR 中實現快速配置的主要設定,是將 Language 屬性設為 OcrLanguage.Fast。此設定優先考量速度,即使可能犧牲些微的準確性,因此非常適合大量處理及時間敏感的應用場景。
除了使用 EnglishFast 模式外,還有什麼方法可以進一步優化 OCR 速度?
您可透過停用 IronOCR 中的非必要功能來進一步提升速度,例如若無需 BarCode 偵測功能,可將 ReadBarCodes 設為 false。此外,建議使用 TesseractPageSegmentationMode.Auto 讓 IronOCR 自動偵測頁面分割。
何時應使用快速 OCR 設定而非標準設定?
IronOCR 的快速 OCR 設定非常適合時間緊迫且可接受些微準確度折衷的大量處理情境。對於需要快速處理掃描文件、PDF 或圖像的關鍵任務應用程式而言,此功能特別實用。
快速設定功能是否適用於所有文件類型?
是的,IronOCR 的快速設定功能能有效處理各種文件類型,包括掃描文件、PDF 及圖像。無論您處理的輸入格式為何,皆可享受其優化效益。
使用快速 OCR 模式時,會不會影響準確度?
IronOCR 的快速配置功能能在幾乎不影響精準度的情況下,顯著提升掃描效能。雖然使用 EnglishFast 模式時精準度可能會略有下降,但對於優先考量速度的應用場景而言,這種取捨通常是值得的。
IronOCR 能否整合至現有應用程式中?
IronOCR 設計上可輕鬆透過 C# 整合至現有應用程式中,讓開發人員能以最少的努力,為其軟體增添 OCR 功能。
使用 IronOCR 進行文件管理有哪些好處?
使用 IronOCR 進行文件管理,可將掃描文件轉換為可搜尋且可編輯的文字,從而簡化工作流程,減少人工資料輸入的需求,並提升文件的可存取性。
IronOCR 如何提升資料準確性?
IronOCR 透過其先進的辨識演算法與影像校正功能來提升資料準確性,確保文字擷取過程既可靠又精確。
IronOCR 是否有提供免費試用版?
是的,Iron Software 提供 IronOCR 的免費試用版,讓使用者能在決定購買前測試其功能與效能。

