跳過到頁腳內容
OCR 工具
如何在 Windows 上使用 Tesseract OCR for .NET

Windows 中的 Tesseract OCR(代碼範例教學)

Tesseract OCR 是什麼?

Tesseract 是一個可以在多種作業系統上使用的光學字符識別引擎。 這是一個免費軟體,根據 Apache 授權條款發佈。 在本指南中,我將帶您了解如何在我的 Windows 10 機器上安裝 Tesseract 的步驟。主要版本 5 是當前穩定版本,並於 2021 年 11 月 30 日發佈的 5.0.0 版本開始。

class="hsg-featured-snippet">

如何在 Windows 中使用 Tesseract OCR

  1. 在 Windows 10 上使用 .exe 文件安裝 Tesseract OCR
  2. 配置 Tesseract 安裝
  3. 將安裝路徑添加到環境變量
  4. 在測試圖像上運行 Tesseract OCR for Windows
  5. 在 Windows 中使用 C# 函式庫以獲得更直觀的 API 和高級方法

步驟 1:以 .exe 文件安裝 Tesseract OCR in Windows 10:

要安裝語言數據: sudo port install tesseract -<langcode> MacPorts Tesseract 頁面 Homebrew 上可以找到 langcode 列表。 安裝 Windows 的 Tesseract OCR 的第一步是下載對應於您機器操作系統的 .exe 安裝程式。

步驟 2: 配置安裝

接下來,我們需要配置 Tesseract 的安裝。 如果您感到有信心且只想在 Windows 上運行 Tesseract OCR 並將默認語言設置為英語,那麼選擇所有默認選項安裝應該可以。

安裝程式語言

這只是用於對話框和幫助信息的語言。 如果我們願意,我們可以在 Windows 中以多種語言運行 Tesseract OCR:

class="content-img-align-center"> Tesseract Ocr Windows 1 related to 安裝程式語言

class="content__image-caption">

Windows 的 Tesseract OCR 安裝程序語言

Tesseract OCR 安裝

設置屏幕建議在繼續安裝之前關閉所有其他應用程序。

class="content-img-align-center"> Tesseract Ocr Windows 2 related to Tesseract OCR 安裝

class="content__image-caption">

Windows 的 Tesseract OCR 安裝屏幕。

選擇安裝位置

接下來,我們將選擇安裝位置。 在繼續下一步之前,請確保將安裝位置複製到 .txt 文件。我們完成安裝後需要將安裝位置添加到我們機器的環境變量中。

class="content-img-align-center"> Tesseract Ocr Windows 3 related to 選擇安裝位置

class="content__image-caption">

選擇安裝位置。

選擇組件

默認情況下,ScrollView、培訓工具、快捷方式創建和語言數據全部被選中。 除非您有特殊原因不安裝這些,我們會希望保留選擇所有這些。

class="content-img-align-center"> Tesseract Ocr Windows 4 related to 選擇組件

class="content__image-caption">

Windows 默認的 Tesseract OCR 安裝組件。

如果我們向下滾動並展開“附加腳本數據”,我們將看到可以下載和安裝附加腳本數據的選項。 這有助於提高從某些編寫語言中提取文本的準確性。 是否要安裝這些完全由您決定。

class="content-img-align-center"> Tesseract Ocr Windows 5 related to 選擇組件

class="content__image-caption">

可選的腳本安裝組件。

選擇開始菜單文件夾

在安裝的最後一步,我們將被要求選擇 Windows 的 Tesseract OCR 快捷方式的開始選單資料夾。 我將我的設置為默認名稱:'Tesseract-OCR'。

class="content-img-align-center"> Tesseract Ocr Windows 6 related to 選擇開始菜單文件夾

class="content__image-caption">

選擇 Windows 的 Tesseract OCR 快捷方式的開始選單資料夾。

點擊安裝後,Windows 的 Tesseract OCR 將開始安裝。 我們的下一步是將安裝路徑添加到我們計算機的環境變量中。

步驟 3:將安裝路徑添加到環境變量

控制面板

要將安裝位置添加到環境變量中,請前往開始選單並搜索 'environment variables'。 您應該會看到編輯系統環境變量的選項。 如果沒有,您隨時可以遵循以下步驟: 開始選單 > 控制面板 > 編輯系統環境變量

class="content-img-align-center"> Tesseract Ocr Windows 7 related to 控制面板

class="content__image-caption">

搜索‘環境變量’

系統屬性

當出現 ‘系統屬性’ 對話框時,我們需要確保點擊 ‘高級’ 標籤,然後點擊屏幕右下角的 ‘環境變量’ 按鈕。

class="content-img-align-center"> Tesseract Ocr Windows 8 related to 系統屬性

class="content__image-caption">

環境變量

在系統變量下,我們將點擊 編輯 按鈕。

class="content-img-align-center"> Tesseract Ocr Windows 9 related to 環境變量

class="content__image-caption">

當出現 "編輯環境變量" 屏幕時,點擊 新增 按鈕,並貼上我們在步驟 2 中複製的 Tesseract OCR 安裝路徑。完成後點擊 ‘確定‘ 按鈕。

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

class="content-img-align-center"> Tesseract Ocr Windows 10 related to 將 Tesseract OCR for Windows 安裝目錄添加到環境變量中

class="content__image-caption">

就是這樣! 現在我們已經運行了 .exe 安裝程式並將 Windows 的 Tesseract OCR 安裝位置添加到我們的環境變量中,我們可以通過運行 Tesseract 在測試圖像上測試我們的安裝是否正常工作。

步驟 4:在測試圖像上運行 Windows 的 Tesseract OCR

要測試 Windows 的 Tesseract OCR 是否安裝成功,請在計算機上打開命令提示字元,然後運行 Tesseract 命令。 您應該會看到一個關於 Tesseract 使用選項的簡要說明。

class="content-img-align-center"> Tesseract Ocr Windows 11 related to 步驟 4:在測試圖像上運行 Windows 的 Tesseract OCR

class="content__image-caption">

檢查 Windows 的 Tesseract OCR 的成功安裝

恭喜! 您已成功在計算機上安裝了 Windows 的 Tesseract OCR。


使用 IronOCR 執行 OCR 工作的優勢:

IronOCR provides Tesseract OCR on Mac, Windows, Linux, Azure and Docker for:

  • .NET Framework 4.0 +
  • .NET Standard 2.0 +
  • .NET Core 2.0 +
  • .NET 5
  • macOS 和 Linux 下的 Mono
  • macOS 下的 Xamarin

IronOCR 使用最新的 Tesseract 5 引擎從所有主要影像和 PDF 格式中讀取文本,條碼和 QR 碼。這個函式庫能在幾分鐘內為桌面程式、控制台和網頁應用程式添加 OCR 功能。 它支持 125 多種國際語言。 Licenses start from $799.

步驟 1:安裝最新版本的 IronOCR

安裝 DLL

Download the IronOcr DLL directly to your machine.

安裝 NuGet

或者,您可以通過以下命令使用 NuGet 安裝它:

Install-Package IronOcr

步驟 2:應用您的授權金鑰

使用代碼設置 IronOCR 的許可鍵

在使用 IronOCR 之前,將這段代碼添加到您的應用程式的啟動中。

IronOcr.Installation.LicenseKey = "IRONOCR-MYLICENSE-KEY-1EF01";
IronOcr.Installation.LicenseKey = "IRONOCR-MYLICENSE-KEY-1EF01";
IronOcr.Installation.LicenseKey = "IRONOCR-MYLICENSE-KEY-1EF01"
$vbLabelText   $csharpLabel

步驟 3:測試您的鍵

測試您的金鑰是否已正確安裝。

bool isValidLicense = IronOcr.License.IsValidLicense("IRONOCR-MYLICENSE-KEY-1EF01");
bool isValidLicense = IronOcr.License.IsValidLicense("IRONOCR-MYLICENSE-KEY-1EF01");
Dim isValidLicense As Boolean = IronOcr.License.IsValidLicense("IRONOCR-MYLICENSE-KEY-1EF01")
$vbLabelText   $csharpLabel

開始專案

// PM > Install-Package IronOcr
// using IronOcr;

var Ocr = new IronTesseract();

// Set the recognition language to English
Ocr.Language = OcrLanguage.English;

using (var Input = new OcrInput())
{
    // Add an example image to the OCR input
    Input.Add(@"img\example.tiff");

    // Optional: Clean the image before processing
    // Input.DeNoise();
    // Input.Deskew();

    // Read the text from the image
    IronOcr.OcrResult result = Ocr.Read(Input);

    // Output the recognized text
    Console.WriteLine(result.Text);

    // Explore the OcrResult using IntelliSense
}
// PM > Install-Package IronOcr
// using IronOcr;

var Ocr = new IronTesseract();

// Set the recognition language to English
Ocr.Language = OcrLanguage.English;

using (var Input = new OcrInput())
{
    // Add an example image to the OCR input
    Input.Add(@"img\example.tiff");

    // Optional: Clean the image before processing
    // Input.DeNoise();
    // Input.Deskew();

    // Read the text from the image
    IronOcr.OcrResult result = Ocr.Read(Input);

    // Output the recognized text
    Console.WriteLine(result.Text);

    // Explore the OcrResult using IntelliSense
}
' PM > Install-Package IronOcr
' using IronOcr;

Dim Ocr = New IronTesseract()

' Set the recognition language to English
Ocr.Language = OcrLanguage.English

Using Input = New OcrInput()
	' Add an example image to the OCR input
	Input.Add("img\example.tiff")

	' Optional: Clean the image before processing
	' Input.DeNoise();
	' Input.Deskew();

	' Read the text from the image
	Dim result As IronOcr.OcrResult = Ocr.Read(Input)

	' Output the recognized text
	Console.WriteLine(result.Text)

	' Explore the OcrResult using IntelliSense
End Using
$vbLabelText   $csharpLabel

如何在 .NET 的 C# 中使用 Tesseract OCR?

  • 在 Visual Studio 中安裝 Google Tesseract 和 IronOCR for .NET
  • 檢查最新的 C# 构建
  • 審閱準確性和影像兼容性
  • 測試性能和 API 功能
  • 考慮多語言支持

.NET OCR 用法的代碼示例 — 從影像中提取文本 C#

使用 NuGet 套件管理器將 IronOCR NuGet 套件安裝到您的 Visual Studio 解決方案中。

// PM > Install-Package IronOcr
// using IronOcr;

var Ocr = new IronTesseract();

// Set the recognition language to English
Ocr.Language = OcrLanguage.English;

using (var Input = new OcrInput())
{
    // Add an example image to the OCR input
    Input.Add(@"img\example.tiff");

    // Optional: Clean the image before processing
    // Input.DeNoise();
    // Input.Deskew();

    // Read the text from the image
    IronOcr.OcrResult result = Ocr.Read(Input);

    // Output the recognized text
    Console.WriteLine(result.Text);

    // Explore the OcrResult using IntelliSense
}
// PM > Install-Package IronOcr
// using IronOcr;

var Ocr = new IronTesseract();

// Set the recognition language to English
Ocr.Language = OcrLanguage.English;

using (var Input = new OcrInput())
{
    // Add an example image to the OCR input
    Input.Add(@"img\example.tiff");

    // Optional: Clean the image before processing
    // Input.DeNoise();
    // Input.Deskew();

    // Read the text from the image
    IronOcr.OcrResult result = Ocr.Read(Input);

    // Output the recognized text
    Console.WriteLine(result.Text);

    // Explore the OcrResult using IntelliSense
}
' PM > Install-Package IronOcr
' using IronOcr;

Dim Ocr = New IronTesseract()

' Set the recognition language to English
Ocr.Language = OcrLanguage.English

Using Input = New OcrInput()
	' Add an example image to the OCR input
	Input.Add("img\example.tiff")

	' Optional: Clean the image before processing
	' Input.DeNoise();
	' Input.Deskew();

	' Read the text from the image
	Dim result As IronOcr.OcrResult = Ocr.Read(Input)

	' Output the recognized text
	Console.WriteLine(result.Text)

	' Explore the OcrResult using IntelliSense
End Using
$vbLabelText   $csharpLabel

C# 的 IronOCR Tesseract

通過使用 NuGet 套件管理器,所有 Tesseract 安裝都可以通過 IronOCR 完成。

Install-Package IronOcr

IronOCR Tesseract 的 Tesseract 5 API

迄今為止,IronTesseract 是唯一已知的 Tesseract 5 實現,適用於 .NET Framework 或 Core。

// using IronOcr;

var Ocr = new IronTesseract(); // nothing to configure

using (var Input = new OcrInput(@"images\image.png"))
{
    var result = Ocr.Read(Input);

    // Output the recognized text
    Console.WriteLine(result.Text);
}
// using IronOcr;

var Ocr = new IronTesseract(); // nothing to configure

using (var Input = new OcrInput(@"images\image.png"))
{
    var result = Ocr.Read(Input);

    // Output the recognized text
    Console.WriteLine(result.Text);
}
' using IronOcr;

Dim Ocr = New IronTesseract() ' nothing to configure

Using Input = New OcrInput("images\image.png")
	Dim result = Ocr.Read(Input)

	' Output the recognized text
	Console.WriteLine(result.Text)
End Using
$vbLabelText   $csharpLabel

IronOCR Tesseract 的 Tesseract 4 API

// using IronOcr;

var Ocr = new IronTesseract();

// Specify the version of Tesseract
Ocr.Configuration.TesseractVersion = TesseractVersion.Tesseract4;

using (var Input = new OcrInput(@"images\image.png"))
{
    var result = Ocr.Read(Input);

    // Output the recognized text
    Console.WriteLine(result.Text);
}
// using IronOcr;

var Ocr = new IronTesseract();

// Specify the version of Tesseract
Ocr.Configuration.TesseractVersion = TesseractVersion.Tesseract4;

using (var Input = new OcrInput(@"images\image.png"))
{
    var result = Ocr.Read(Input);

    // Output the recognized text
    Console.WriteLine(result.Text);
}
' using IronOcr;

Dim Ocr = New IronTesseract()

' Specify the version of Tesseract
Ocr.Configuration.TesseractVersion = TesseractVersion.Tesseract4

Using Input = New OcrInput("images\image.png")
	Dim result = Ocr.Read(Input)

	' Output the recognized text
	Console.WriteLine(result.Text)
End Using
$vbLabelText   $csharpLabel

為什麼 IronOCR 比 Tesseract 更好:

準確性

TESSERACT:

如果 Tesseract 遇到旋轉、傾斜、低 DPI、已掃描或有背景噪音的圖片,幾乎不可能獲取該圖片中的數據。 此外,Tesseract 還需要很長時間來處理該文件,然後才為您提供無意義的信息。

IRONOCR:

IronOCR 消除了這些頭痛。 用戶通常以最少的配置達到 99.8-100% 的準確率。

影像兼容性

TESSERACT:

只接受 Leptonica PIX 影像格式,它在 C# 中是一個 IntPtr C++ 對象。 PIX 對象不是託管內存 — 在 C# 中不小心處理它們將導致記憶體洩漏。

IRONOCR:

影像是統一內存管理的。 支持 PDF 和 TIFF。 System.Drawing、Stream 和 Byte Array 都包括在每個文件格式中。

廣泛的影像支持:

  • PDF 檔
  • PDF 頁面
  • 多幀 TIFF 檔
  • JPEG 和 JPEG2000
  • GIF
  • PNG
  • System.Drawing.Image
  • 二进制图像数据(byte [])
  • 更多……

性能

TESSERACT:

如果正確調整並且輸入影像已通過 Photoshop 或 ImageMagick 預處理,Google 確認 Tesseract 能夠提供快速準確的結果。

IRONOCR:

IronOCR .NET Tesseract DLL 開箱即用地在大多數影像上準確和速度運行。 我們實現了多線程,以利用目前大多數計算機使用的多核心處理器。 即使是低分辨率影像,一般也能以高準確度運行於你的程序中。 不需要 Photoshop。

API

TESSERACT:

我們有兩個免費選擇:

  • 使用 Interop 層上的工作 — 很多在 GitHub 上發現的是不合時宜的,並且有未解決的工單、記憶體洩露和控制台警告。 可能不支持 .NET Core 或 Standard。
  • 使用命令行 EXE 工作—難以部署並經常被病毒掃描器和安全策略中斷。

IRONOCR:

一個稱為 IronTesseract 的管理和經過測試的 .NET 函式庫。

完整的文檔,支持 IntelliSense。

語言支持

TESSERACT:

僅支持 100 種語言。

IRONOCR:

支持 125 多種語言。


結論

Tesseract 是 C++ 開發人員的極佳資源,但它不是 .NET 的完整 OCR 函式庫。 掃描或拍攝的圖像需要進行處理,使其垂直、標準化、高分辨率,並在 Tesseract 可以準確處理之前消除數位噪音。

相比之下,IronOCR 可以做到這些及更多,只需一行代碼。 It is true that IronOCR uses Tesseract for its internal OCR engine, a very finely-tuned Tesseract, built for C#, with a lot of performance improvements and features added as standard.

Kannaopat Udonpant
軟體工程師
在成為软件工程師之前,Kannapat 從日本北海道大學完成了環境資源博士學位。在追逐學位期间,Kannapat 還成為了生產工程系一部份——汽車机器人实验室的成員。2022 年,他利用他的 C# 技能加入 Iron Software 的工程團隊, 專注於 IronPDF。Kannapat 珍惜他的工作,因为他直接向编写大部分 IronPDF 使用的代码的开发者学习。除了同行学习,Kannapat 还喜欢在 Iron Software 工作的社交十环。当他不编写代码或文档时,Kannapat 通常在他的 PS5 上打游戏或重看《The Last of Us》。