Fast OCR Configuration in C# for Optimal Performance

快速的 C# 語言 OCR 設定,達到最佳效能。
This article was translated from English: Does it need improvement?
Translated
View the article in English

IronOCR 的快速配置可透過使用 EnglishFast 語言模式和停用不必要的功能(如條碼讀取),使 OCR 處理速度提高 17%,且不會造成精確度的損失。 此最佳化非常適合時間緊迫的大批量處理。

IronOCR 開箱即可有效運作。 當速度優先於絕對精確度時,IronOCR 可提供快速的配置。 此設定可大幅提升掃描效能,且對精確度的影響極小,因此比標準的 OCR 設定更快速。

本文示範如何設定快速組態,並比較快速與標準 IronOCR 組態的基準結果。 無論您是處理 掃描的文件PDF,或是 圖片,這些最佳化都能大幅提升您應用程式的效能。


as-heading:2(快速入門:在 C# 中配置快速 OCR)

快速配置的主要元件是 Language 屬性。 將 Language 屬性設定為OcrLanguage.EnglishFast會優先處理速度,而不會在精確度上付出小的潛在代價。 這可讓 IronOCR 更快速地大量閱讀,這在時間非常重要的關鍵任務應用中特別有用。

除了設定快速語言之外,您還可以透過停用不必要的設定 (例如 ReadBarCodes) 來進一步提高速度。 讓 IronOCR 自動偵測頁面分割,以保持設定簡單。 如需更多進階組態選項,請參閱我們的 Tesseract 詳細組態指南

以下的程式碼範例會處理以下的輸入影像:

我應該使用何種輸入格式?

《白鯨記》開場文字以白色顯示在深色背景上,顯示 Ishmael 的介紹

快速組態需要哪些程式碼?

Nuget Icon立即開始使用 NuGet 建立 PDF 檔案:

  1. 使用 NuGet 套件管理器安裝 IronOCR

    PM > Install-Package IronOcr

  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.PageSegmentation模式 = TesseractPageSegmentation模式.Auto;
    
    using var ocrInput = new OcrInput();
    ocrInput.LoadImage("image.png");
    
    var ocrResult = ocrTesseract.Read(ocrInput);
    Console.WriteLine(ocrResult.Text);
  3. 部署到您的生產環境進行測試

    立即開始在您的專案中使用 IronOCR,免費試用!
    arrow pointer

我可以期待什麼樣的成果?

Visual Studio 編輯器顯示 Moby Dick 小說的開頭段落

這是從上方提取的文字輸出。 OCR 引擎可準確擷取文學文字,同時保持原始格式與結構。 快速組態可為清晰、高對比的文字(如本範例)提供絕佳的效果。


快速配置與標準配置相比如何?

<! -- ocr 快速配置基準實作示意圖 --> <!--說明:說明程式碼概念的圖表或截圖 -->

為了展現真實世界的影響,我們將標準與快速配置的效能進行比較。 我們使用一套 10 個範例圖片,每個圖片包含數個段落,來比較使用快速組態的效能與可視化權衡。

對於標準配置,我們以預設設定初始化 IronTesseract ,不套用任何速度導向的屬性。 此基準方法類似於我們的 效能追蹤指南,說明如何即時監控 OCR 作業。

以下是我們用來執行測試的範例 輸入。這些影像代表您在處理 多頁文件或批次作業時可能遇到的典型文件情境。

我要如何執行 Benchmark?

: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}")
$vbLabelText   $csharpLabel

本基準代碼展示了幾個重要的概念:

1.批次處理:代碼會在單一作業中處理多個影像,類似於我們的 多執行緒 OCR 實例,它展示了如何利用平行處理來獲得更大的速度提升。

2.效能測量:使用 Stopwatch 類別可提供精確到毫秒的定時測量,對於比較不同的組態非常重要。

3.結果記錄:控制台和檔案輸出可確保您稍後能夠分析結果,並驗證配置之間的精確度差異。

我可以期待哪些效能增益?

模式 總時間 平均耗時/影像 與標準相比,時間增益 與標準相比,精度提升
標準 10.40秒 1.040秒 基線 基線
快速 8.60秒 0.860秒 +17.31%(速度更快) +0%(相同)

標準配置與快速配置的基準比較顯示,快速配置具有顯著的效能優勢。 透過建立標準模式為基準(總時間為 10.40 秒),快速配置只需 8.60 秒即可完成相同批次的 10 張圖像。 這意味著在時間上節省了17.31%。 最重要的是,速度的提升並沒有影響品質; 結果顯示,兩種模式的精確度完全相同,兩種配置都能產生相同的文字輸出。

若要驗證結果,您可以下載 快速文字輸出 標準文字輸出

何時應該使用快速組態?

快速配置對以下方面特別有利

  • 大量文件處理,數千頁的文件需要快速處理
  • 反應時間非常重要的即時應用程式
  • 需要維持回應式使用者體驗的 Web 應用程式
  • 批量處理系統,在緊迫的時間表上運行

對於涉及 多種語言低品質掃描,或特殊文件類型(如 許可證號牌護照)的更複雜情境,您可能需要使用標準配置以確保最大的精確度。

IronOCR 使配置之間的切換變得簡單 - 只需變更幾個屬性,您的應用程式就能適應不同的效能需求,而無需進行重大的程式碼變更。

常見問題解答

與標準設定相比,快速 OCR 配置的速度快多少?

與標準的 OCR 設定相比,IronOCR 的快速配置可使處理速度提高 17%,且對準確性的影響極小。這種性能增益是透過 EnglishFast 語言模式和停用不必要的功能來實現的。

實現快速 OCR 處理的主要設定是什麼?

IronOCR 中快速配置的主要元件是將語言屬性設定為 OcrLanguage.EnglishFast。這將速度優先於精確度上的小潛在代價,非常適合大量處理和時間緊迫的應用程式。

除了使用 EnglishFast 模式之外,我該如何進一步優化 OCR 速度?

您可以透過停用 IronOCR 中不必要的功能來獲得額外的速度改善,例如,如果您不需要條碼偵測,可將 ReadBarCodes 設為 false。此外,使用 TesseractPageSegmentationMode.Auto 讓 IronOCR 自動偵測頁面分割。

何時應該使用快速 OCR 設定而非標準設定?

IronOCR 中的快速 OCR 配置非常適合用於大批量處理的場景,在這些場景中,時間非常重要,而在精確度方面稍作取捨是可以接受的。它對於需要快速處理掃描文件、PDF 或影像的關鍵任務應用程式尤其有用。

快速配置是否適用於所有文件類型?

是的,IronOCR 的快速配置可有效處理各種文件類型,包括掃描文件、PDF 和影像。無論您處理的是何種輸入格式,優化優點都適用。

使用快速 OCR 模式時,精確度是否有任何損失?

IronOCR 的快速配置可大幅提升掃描效能,且對精確度的影響極小。雖然在使用 EnglishFast 模式時,可能會在精確度上付出少許潛在代價,但對於以速度為優先的應用程式來說,這樣的取捨往往是值得的。

Curtis Chau
技術撰稿人

Curtis Chau 擁有電腦科學學士學位(卡爾頓大學),專長於前端開發,精通 Node.js、TypeScript、JavaScript 和 React。Curtis 對製作直覺且美觀的使用者介面充滿熱情,他喜歡使用現代化的架構,並製作結構良好且視覺上吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 也有濃厚的興趣,他喜歡探索整合硬體與軟體的創新方式。在空閒時間,他喜歡玩遊戲和建立 Discord bots,將他對技術的熱愛與創意結合。

準備好開始了嗎?
Nuget 下載 5,384,824 | 版本: 2026.2 剛剛發布