跳過到頁腳內容
OCR 工具
如何在 Windows 的 C# 中安裝 Tesseract OCR

安裝 Tesseract(逐步圖片教學)

Tesseract OCR 是什麼?

Tesseract 是一個開源軟體庫,依據 Apache 許可協議發布。 它最初由惠普公司在1980年代開發。 這是一個主要用來從圖像識別和提取文本的文本識別工具。 Tesseract OCR 提供命令提示字元介面來執行該功能。

role="alert"> IronOCR 建立於 Tesseract 之上。在不依賴外部網路服務的情況下,只需幾行程式碼便能以 99.8% 的準確率從圖像和 PDF 中讀取文本。IronOCR 也能從質量較差的圖像和掃描件中提取內容。告別繁瑣的性能調整和單調的預處理工作。在速度、準確性和易用性重要的時候,信任 IronOCR 迅速完成工作。

了解更多關於 IronOCR 的功能立即註冊試用

如何在 Windows 中下載 Tesseract OCR

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

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

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

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

class="content-img-align-center">
class="center-image-wrapper"> 安裝 Tesseract, 圖 1: Tesseract Wiki

class="content__image-caption">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,但這次我們只安裝英語語言資料。

class="content-img-align-center">
class="center-image-wrapper"> 安裝 Tesseract, 圖 2: Tesseract 安裝程序

class="content__image-caption">Tesseract 安裝程序

點擊 OK,然後 Tesseract OCR 的安裝語言已設置。

Tesseract OCR 安裝

接下來,安裝向導將會出現。 這個安裝向導會指引您在 Windows 上的 Tesseract 安裝。

class="content-img-align-center">
class="center-image-wrapper"> 安裝 Tesseract, 圖 3: Tesseract OCR

class="content__image-caption">Tesseract OCR 安裝向導

點擊 Next 繼續安裝。

接受許可協議

Tesseract OCR 授權依據 Apache 許可證版本 2.0。由於它是開源且免費使用,您可以重新分發和修改 Tesseract 的版本而無需支付版稅。

class="content-img-align-center">
class="center-image-wrapper"> 安裝 Tesseract, 圖 4: Tesseract 許可證

class="content__image-caption">Tesseract OCR 授權依據 Apache 許可證 v2.0。請接受此許可證以繼續安裝。

點擊 I Agree 以繼續安裝。

選擇用戶

您可以選擇安裝 Tesseract 給多個用戶或單一用戶。

class="content-img-align-center">
class="center-image-wrapper"> 安裝 Tesseract, 圖 5: Tesseract 選擇用戶

class="content__image-caption">選擇安裝 Tesseract OCR 給當前用戶(您)或所有用戶帳號

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

選擇組件

從要安裝的組件列表中,ScrollView、訓練工具、快捷方式創建和語言數據都是默認選中的。 我們將保留所有默認選項。 您可以根據需要選擇或跳過任何組件。 通常,所有的都是必要的。

class="content-img-align-center">
class="center-image-wrapper"> 安裝 Tesseract, 圖 6: Tesseract 組件

class="content__image-caption">在此,您可以選擇包括或排除 Tesseract OCR 組件。為了獲得最佳效果,繼續安裝選擇默認的組件。

點擊 Next 選擇安裝位置。

選擇安裝位置

接下來,我們將選擇安裝 Tesseract 的位置。 請確保您複製好目錄位置。 稍後我們需要將它添加到機器的路徑環境變量中。

class="content-img-align-center">
class="center-image-wrapper"> 安裝 Tesseract, 圖 7: Tesseract 安裝位置

class="content__image-caption">選擇 Tesseract OCR 庫的安裝位置,並記住此位置以備後用。

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

選擇開始菜單文件夾

這是我們在開始菜單中創建捷徑的最後一步。 您可以隨便命名文件夾,但我保持為默認名稱。

class="content-img-align-center">
class="center-image-wrapper"> 安裝 Tesseract, 圖 8: Tesseract 開始菜單

class="content__image-caption">選擇 Tesseract OCR 的開始菜單文件夾名稱

現在,點擊 Install 並等待安裝完成。 安裝完成後,將顯示以下畫面。 點擊 Finish,我們已成功在 Windows 上安裝 Tesseract OCR。

class="content-img-align-center">
class="center-image-wrapper"> 安裝 Tesseract, 圖 9: Tesseract 安裝程序

class="content__image-caption">Tesseract OCR 安裝現在已完成。

3. 將安裝路徑添加到系統環境變數

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

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

class="content-img-align-center">
class="center-image-wrapper"> 安裝 Tesseract, 圖 10: 系統路徑變量

class="content__image-caption">Windows 系統屬性對話框

系統屬性

系統屬性對話框打開後,點擊“高級”標籤,然後點擊位於屏幕右下方的“環境變量”按鈕。

環境變量對話框將呈現在您面前。

環境變量

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

class="content-img-align-center">
class="center-image-wrapper"> 安裝 Tesseract, 圖 11: 環境變量

class="content__image-caption">訪問 Windows 的系統環境變量

現在,點擊編輯。

將 Tesseract OCR for Windows 安裝目錄添加到環境變量中

編輯環境變量對話框中,點擊新建。將第二步中複製的安裝位置路徑粘貼進去,然後點擊確定。

class="content-img-align-center">
class="center-image-wrapper"> 安裝 Tesseract, 圖 12: 編輯環境變量

class="content__image-caption">編輯 Windows 的路徑系統環境變量,添加包含 Tesseract OCR 安裝的絕對路徑的條目

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

4. 運行 Tesseract OCR

要檢查在 Windows 上的 Tesseract OCR 是否安裝並添加到環境變量中,請在 Windows 機器上打開命令提示符(cmd),然後運行"tesseract"命令。 如果一切正常,則應該顯示快速使用指南,其中包括 OCR 和其他單一選項,例如 Tesseract 版本。

class="content-img-align-center">
class="center-image-wrapper"> 安裝 Tesseract, 圖 13: 編輯環境變量

class="content__image-caption">在 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 使用 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

輸入圖像

class="content-img-align-center">
class="center-image-wrapper"> 安裝 Tesseract, 圖 14: 輸入圖像

class="content__image-caption">IronOCR 的輸入圖片範例

輸出圖像

輸出的內容打印在控制台中:

class="content-img-align-center">
class="center-image-wrapper"> 安裝 Tesseract, 圖 15: 輸出圖像

class="content__image-caption">從 IronOCR 對圖像範例的執行返回的控制台

為什麼選擇 IronOCR?

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

IronOCR 在不需要其他第三方庫或網路服務的情況下實現了 99.8% 的文本檢測精度。

它還提供了多線程支持。 最重要的是,IronOCR 能夠處理超過 125 種國際語言。

role="alert"> 從 NuGet 安裝 IronOCR 用於您的下個 OCR 專案,親自見證其全部能力。試用許可證提供 30 天的免費、無限制訪問 IronOCR 的完整功能。

結論

在本教程中,我們學會了如何下載和安裝 Tesseract OCR 到 Windows 機器。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》。