跳至頁尾內容
OCR 工具
如何在C#中於Windows上安裝 Tesseract OCR

安裝 Tesseract(逐步圖片教學)

Tesseract OCR 是什麼?

Tesseract 是一個開源軟體程式庫,在 Apache 授權協議下發布。 它最初是於 1980 年代由惠普公司開發的。 它是一個主要用於識別和提取影像中文字的文字識別工具。 Tesseract OCR 提供了一個命令提示介面來執行這項功能。

如何在 Windows 中下載 Tesseract OCR

  1. 下載 Windows 用 Tesseract 安裝程式
  2. 安裝 Tesseract OCR
  3. 將安裝路徑新增到環境變數
  4. 執行 Tesseract OCR

1. 下載 Tesseract 安裝程式以用於 Windows

若要在 Windows 上使用 Tesseract 命令,我們首先需要下載 Tesseract OCR 二進制可執行檔的 Windows 安裝程式。

有許多地方可以下載最新版本的 Tesseract OCR。 其中一個地方是從 UB Mannheim,這是從 Tesseract-ocr/tesseract(主儲存庫)分叉出的。

Install Tesseract, Figure 1: Tesseract Wiki

Tesseract Wiki

Download the Tesseract-ocr-w64-setup-5.3.0.20221222.exe (64 bit) Windows Installer.

對於 macOS 使用者,可以使用以下任一命令在終端安裝 Tesseract:

brew install tesseract
brew install tesseract
SHELL
sudo port install tesseract
sudo port install tesseract
SHELL

2. 安裝 Tesseract OCR

接下來,我們將使用在上一步下載的 .exe 文件安裝 Tesseract。啟動 .exe 安裝程式以開始安裝 Tesseract。

安裝程式語言

一旦設置解壓完成後,安裝程式語言資料對話框將出現。 您可以通過選擇其他語言包來安裝 Tesseract 使用多種語言,但這裡我們只安裝英文語言資料。

Install Tesseract, Figure 2: Tesseract 安裝程式

Tesseract 安裝程式

點擊 OK,並設置 Tesseract OCR 的安裝語言。

Tesseract OCR 安裝設置

接下來,設置精靈將出現。 此設置精靈將指導在 Windows 上的 Tesseract 安裝。

Install Tesseract, Figure 3: Tesseract OCR

Tesseract OCR 安裝精靈

點擊 Next 以繼續安裝。

接受許可協議

Tesseract OCR 採用 Apache 版權協議 2.0 授權。由於它是開源且免費使用的,您可以在無需考慮版稅的情況下重新分發和修改 Tesseract 的版本。

Install Tesseract, Figure 4: Tesseract License

Tesseract OCR 採用 Apache 授權協議 v2.0 授權。請接受此許可以繼續安裝。

點擊 我同意 以繼續安裝。

選擇使用者

您可以選擇為多個使用者或單個使用者安裝 Tesseract。

Install Tesseract, Figure 5: Tesseract Choose Users

選擇為當前使用者(您)或所有使用者帳戶安裝 Tesseract OCR

點擊 Next 來選擇要與 Tesseract 一起安裝的元件。

選擇元件

從要安裝的元件列表中,ScrollView、培訓工具、快捷方式建立和語言資料都預設選中。 我們將保留所有預設選擇的選項。 您可以根據需要選擇或略過任何元件。 通常,所有元件都是必須安裝的。

Install Tesseract, Figure 6: Tesseract Components

在這裡,您可以選擇包含或排除 Tesseract OCR 元件。為了獲得最佳效果,繼續安裝時選擇預設元件。

點擊 Next 來選擇安裝位置。

選擇安裝位置

接下來,我們將選擇安裝 Tesseract 的位置。 確保您複制目標資料夾路徑。 我們稍後需要將此安裝位置新增到計算機的環境變數中。

Install Tesseract, Figure 7: Tesseract Install Location

選擇一個 Tesseract OCR 程式庫的安裝位置,並記住這個位置以備後用。

點擊 Next 以進一步設置 Tesseract 的安裝。

選擇開始選單資料夾

這是我們將在開始選單中建立快捷方式的最後一步。 您可以命名文件夾為任何名稱,但我將其保持為預設值。

Install Tesseract, Figure 8: Tesseract Start Menu

選擇 Tesseract OCR 的開始選單資料夾名稱

現在,點擊安裝並等待安裝完成。 安裝完成後,將出現以下畫面。 點擊完成,我們已成功在 Windows 中安裝 Tesseract OCR。

Install Tesseract, Figure 9: Tesseract 安裝程式

Tesseract OCR 安裝現已完成。

3. 將安裝路徑新增到系統環境變數

現在,我們將 Tesseract 安裝路徑新增到 Windows 的環境變數中。

在開始選單中,輸入 "環境變數" 或 "高級系統設置"

Install Tesseract, Figure 10: System Path Variables

Windows 系統屬性對話框

系統屬性

系統屬性對話框開啟後,點擊高級標籤,然後點擊畫面右下角的環境變數按鈕。

環境變數對話框將顯示給您。

環境變數

系統變數 下,點擊Path變數。

Install Tesseract, Figure 11: Environment Variables

存取 Windows 的系統環境變數

現在,點擊編輯。

將 Windows 安裝目錄的 Tesseract OCR 新增到環境變數

編輯環境變數 對話框中,點擊 新增。貼上在第二步中複製的安裝位置路徑,然後點擊 OK。

Install Tesseract, Figure 12: Edit Environment Variable

通過新增包含 Tesseract OCR 安裝的絕對路徑的條目來編輯 Windows 的 Path 系統環境變數。

完成了! 我們已成功下載、安裝並設置了 Windows 機器中 Tesseract OCR 的環境變數。

4. 執行 Tesseract OCR

要檢查 Windows 的 Tesseract OCR 是否成功安裝並新增到環境變數中,請在 Windows 機器上打開命令提示(cmd)然後執行 "Tesseract" 命令。 如果一切正常,將快速顯示包含 OCR 和其他單一選項,如 Tesseract 版本的使用指南。

Install Tesseract, Figure 13: Edit Environment Variable

在 Windows 命令行(或 Windows Powershell)中運行 tesseract 命令以確認上述安裝步驟完成正確。控制台輸出是 Windows 成功安裝的預期結果。

恭喜! 我們已成功為 Windows 安裝 Tesseract OCR。

IronOCR 程式庫

IronOCR 是一個基於 Tesseract 的 C# 程式庫,允許 .NET 軟體開發人員從圖像和 PDF 文件中識別和提取文字。 它完全在 .NET 中構建,使用無處不在的最先進的 Tesseract 引擎。

使用 NuGet 封裝管理員安裝

在 Visual Studio 中或使用命令行與 NuGet 封裝管理員安裝 IronOCR 是很容易的。 在 Visual Studio 中,導航到功能表選項:

工具 > NuGet 封裝管理員 > 封裝管理員控制台

然後在命令行中鍵入以下命令:

Install-Package IronOcr

這將輕鬆安裝 IronOCR,現在您可以使用它來發揮其全部潛力。

您還可以為不同的平台下載其他 IronOCR NuGet 封裝

IronOCR with Tesseract 5

以下範例程式碼顯示了使用 IronOCR Tesseract 從影像中讀取文字並使用 C# 執行 OCR 是多麼簡單。

// Import the IronOCR library
using IronOcr;

// Create an instance of IronTesseract
var Ocr = new IronTesseract();

string Text = Ocr.Read(@"test-files/redacted-employmentapp.png").Text;

// Output the extracted text to the console
Console.WriteLine(Text); // Printed text
// Import the IronOCR library
using IronOcr;

// Create an instance of IronTesseract
var Ocr = new IronTesseract();

string Text = Ocr.Read(@"test-files/redacted-employmentapp.png").Text;

// Output the extracted text to the console
Console.WriteLine(Text); // Printed text
' Import the IronOCR library
Imports IronOcr

' Create an instance of IronTesseract
Private Ocr = New IronTesseract()

Private Text As String = Ocr.Read("test-files/redacted-employmentapp.png").Text

' Output the extracted text to the console
Console.WriteLine(Text) ' Printed text
$vbLabelText   $csharpLabel

如果您需要更穩健的程式碼,以下內容應能幫助您實現相同的任務:

// Import the IronOCR library
using IronOcr;

// Create an instance of IronTesseract
var Ocr = new IronTesseract();

// Using the OcrInput class to handle multiple images
using (var Input = new OcrInput()){
    // Add an image to the input collection
    Input.AddImage("test-files/redacted-employmentapp.png");
    // You can add any number of images

    // Read the OCR text from the input
    var Result = Ocr.Read(Input);

    // Output the extracted text to the console
    Console.WriteLine(Result.Text);
}
// Import the IronOCR library
using IronOcr;

// Create an instance of IronTesseract
var Ocr = new IronTesseract();

// Using the OcrInput class to handle multiple images
using (var Input = new OcrInput()){
    // Add an image to the input collection
    Input.AddImage("test-files/redacted-employmentapp.png");
    // You can add any number of images

    // Read the OCR text from the input
    var Result = Ocr.Read(Input);

    // Output the extracted text to the console
    Console.WriteLine(Result.Text);
}
' Import the IronOCR library
Imports IronOcr

' Create an instance of IronTesseract
Private Ocr = New IronTesseract()

' Using the OcrInput class to handle multiple images
Using Input = New OcrInput()
	' Add an image to the input collection
	Input.AddImage("test-files/redacted-employmentapp.png")
	' You can add any number of images

	' Read the OCR text from the input
	Dim Result = Ocr.Read(Input)

	' Output the extracted text to the console
	Console.WriteLine(Result.Text)
End Using
$vbLabelText   $csharpLabel

輸入圖像

Install Tesseract, Figure 14: Input Image

IronOCR 處理的輸入圖像範例

輸出圖像

控制台上顯示的輸出為:

Install Tesseract, Figure 15: Output Image

從 IronOCR 在範例圖像上執行的控制台返回結果。

為何選擇 IronOCR?

IronOCR 非常容易安裝。 它提供了一個完整且文件齊全的 .NET 軟體程式庫。

IronOCR 可達到 99.8% 的文字檢測準確率,無需其他第三方程式庫或網路服務。

它還提供多執行緒支持。 最重要的是,IronOCR 可適用於超過 125 種國際語言。

結論

在本教程中,我們學會了如何為 Windows 機器下載和安裝 Tesseract OCR。Tesseract OCR 是 C++ 開發人員的極佳軟體,但它確實有一些限制。 它不是完全開發用於 .NET。 掃描的圖像文件或拍攝的圖像需要處理和標準化為高解析度,讓它們免受數位噪聲的影響。只有這樣 Tesseract 才能準確地對它們工作。

相比之下,IronOCR 可以僅用一行程式碼處理任何提供的圖像,無論是掃描的還是拍攝的。 IronOCR 也使用 Tesseract 作為其內部 OCR 引擎,但它經過精細調整,以最大限度地發揮 Tesseract 的潛力,特別是為 C# 建立,具有高效能和改進的功能。

您可以從這個連結下載 IronOCR 軟體產品。

Kannaopat Udonpant
軟體工程師
在成為軟體工程師之前,Kannapat在日本北海道大學完成了環境資源博士學位。在攻讀學位期間,Kannapat還成為車輛機器人實驗室的一員,該實驗室隸屬於生產工程系。在2022年,他憑藉C#技能加入了Iron Software的工程團隊,專注於IronPDF。Kannapat珍視他的工作,因為他能直接向撰寫大部分IronPDF程式碼的開發者學習。除了同儕學習,Kannapat還喜歡在Iron Software工作的社交方面。不寫程式碼或文件時,Kannapat通常在他的PS5上玩遊戲或重看The Last of Us。

Iron 支援團隊

我們線上24小時,每週5天。
聊天
電子郵件
給我打電話