跳過到頁腳內容
OCR 工具
如何使用 C# 在 Windows 上安裝 Tesseract OCR

安裝 Tesseract(逐步圖片教學)

什麼是 Tesseract OCR?

Tesseract 是一個開源軟體庫,根據 Apache 許可協議發布。 它最初是由惠普公司在 20 世紀 80 年代開發的。 它是一款文字辨識工具,主要用於識別和提取圖像中的文字。 Tesseract OCR 提供了一個命令列介面來執行此功能。

如何在 Windows 系統中下載 Tesseract OCR

  1. 下載適用於 Windows 的 Tesseract 安裝程序
  2. 安裝 Tesseract OCR
  3. 將安裝路徑加入環境變數中
  4. 運行 Tesseract OCR

1. 下載適用於 Windows 的 Tesseract 安裝程序

要在 Windows 上使用 Tesseract 命令,我們首先需要下載 Tesseract OCR 二進位 .exe Windows 安裝程式。

有很多地方可以下載最新版本的 Tesseract OCR。 One such place is from UB Mannheim, which is forked from tesseract-ocr/tesseract (Main Repository).

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 安裝程序

點擊"確定",Tesseract OCR 的安裝程式語言就設定好了。

Tesseract OCR 設定

接下來,將出現安裝精靈。 此安裝精靈將引導您在 Windows 系統上安裝 Tesseract。

Install Tesseract, Figure 3: Tesseract OCR

Tesseract OCR 設定精靈

點選"下一步"繼續安裝。

接受授權協議

Tesseract OCR 採用 Apache License Version 2.0 授權。由於它是開源且免費使用的,您可以重新分發和修改 Tesseract 的各個版本,而無需擔心版稅問題。

Install Tesseract, Figure 4: Tesseract License

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

點選"我同意"繼續安裝。

選擇用戶

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

Install Tesseract, Figure 5: Tesseract Choose Users

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

按一下"下一步"選擇要與 Tesseract 一起安裝的元件。

選擇組件

在要安裝的元件清單中,ScrollView、訓練工具、捷徑建立和語言資料預設全部被選取。 我們將保留所有預設選取的選項。 您可以根據需要選擇或跳過任何組件。 通常情況下,所有這些都是安裝所必需的。

Install Tesseract, Figure 6: Tesseract Components

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

點選"下一步"選擇安裝位置。

選擇安裝位置

接下來,我們將選擇 Tesseract 的安裝位置。 請務必複製目標資料夾路徑。 稍後我們需要使用它,以便將安裝位置新增到機器的路徑環境變數中。

Install Tesseract, Figure 7: Tesseract Install Location

選擇 Tesseract OCR 庫的安裝位置,並記住此位置以備後用。

點選"下一步"繼續設定 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 系統屬性對話框

系統屬性

開啟"系統屬性"對話方塊後,按一下"進階"選項卡,然後按一下畫面右下角的"環境變數"按鈕。

系統將彈出"環境變數"對話框。

環境變數

"系統變數"下,按一下"路徑"變數。

Install Tesseract, Figure 11: Environment Variables

存取Windows系統環境變數

現在,點選"編輯"。

將 Tesseract OCR for Windows 安裝目錄新增至環境變數。

"編輯環境變數"對話方塊中,按一下"新建"。貼上在第二步驟中複製的安裝位置路徑,然後按一下"確定"。

Install Tesseract, Figure 12: Edit Environment Variable

編輯 Windows 系統環境變數 Path,新增一個項目,其中包含 Tesseract OCR 安裝的絕對路徑。

就是這樣! 我們已成功在 Windows 機器上下載、安裝並設定了 Tesseract OCR 的環境變數。

4. 運行 Tesseract OCR

若要檢查 Tesseract OCR for Windows 是否已成功安裝並新增至環境變數中,請在 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 中安裝 IronOCR 或使用 NuGet 套件管理器透過命令列安裝 IronOCR 都很簡單。 在 Visual Studio 中,使用以下命令導航至選單選項:

工具 > NuGet 套件管理員 > 套件管理器控制台

然後在命令列中,輸入以下命令:

Install-Package IronOcr

這樣就能輕鬆安裝 IronOCR,現在你可以充分利用它的全部功能了。

您也可以下載適用於不同平台的其他IronOCR NuGet 套件

IronOCR 與 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》。