Windows 系統中的 Tesseract OCR(程式碼範例教學)
什麼是 Tesseract OCR?
Tesseract 是一款光學字元辨識引擎,可用於多種作業系統。 它是根據 Apache 許可證發布的自由軟體。 在本指南中,我將帶您了解我在 Windows 10 電腦上安裝 Tesseract 的步驟。目前穩定版本為 5,始於 2021 年 11 月 30 日發布的 5.0.0 版本。
如何在Windows中使用Tesseract OCR
- 使用 .exe 檔案在 Windows 10 上安裝 Tesseract OCR
- 配置 Tesseract 安裝
- 將安裝路徑新增至環境變數中
- 在測試映像上執行適用於 Windows 的 Tesseract OCR
- 在 Windows 系統中,使用 C# 函式庫可以獲得更直覺的 API 和進階方法。
步驟 1:使用 .exe 檔案在 Windows 10 中安裝 Tesseract OCR:
安裝語言資料: sudo port install tesseract -<langcode> MacPorts Tesseract 頁面 Homebrew 上提供了語言代碼清單。 安裝適用於 Windows 的 Tesseract OCR 的第一步是下載與您的電腦作業系統對應的 .exe 安裝程式。
步驟 2:設定安裝
接下來,我們需要設定 Tesseract 安裝。 如果您信心十足,只想在 Windows 系統下執行 Tesseract OCR,並將預設語言設為英語,那麼請按照安裝步驟選擇所有預設選項即可。
安裝程式語言
這只是對話框和幫助訊息中使用的語言。 如果需要,我們可以執行適用於 Windows 的 Tesseract OCR,支援多種語言:

適用於 Windows 的 Tesseract OCR 安裝程式語言
Tesseract OCR 設定
安裝介面建議在繼續安裝之前關閉所有其他應用程式。

Tesseract OCR for Windows 安裝介面。
選擇安裝位置
接下來,我們將選擇安裝位置。 在進行下一步之前,請務必將安裝位置複製到 .txt 檔案中。安裝完成後,我們需要將該安裝位置新增到電腦的環境變數中。

選擇安裝位置。
選擇組件
預設情況下,"捲動視圖"、"訓練工具"、"快速鍵建立"和"語言資料"全部處於選取狀態。 除非您有特殊原因不安裝這些組件,否則我們希望保持所有這些組件都已選取。

適用於 Windows 的 Tesseract OCR 預設安裝元件。
如果我們向下捲動並展開"附加腳本資料",就會看到我們可以選擇下載和安裝附加腳本資料。 這有助於提高從某些腳本語言中提取文字的準確性。 是否安裝這些取決於您。

可選的腳本安裝組件。
選擇"開始"功能表資料夾
在安裝的最後一步,系統會要求我們選擇 Tesseract OCR for Windows 捷徑的開始功能表資料夾。 我保留了預設名稱:"Tesseract-OCR"。

選擇 Tesseract OCR for Windows 捷徑的開始功能表資料夾。
點擊安裝後,Tesseract OCR for Windows 將開始安裝。 下一步是將安裝路徑新增至機器的環境變數。
步驟 3:將安裝路徑新增至環境變量
控制面板
若要將安裝位置新增至環境變數中,請前往"開始"功能表並蒐尋"環境變數"。 您應該會看到可以編輯系統環境變數的選項。 如果不行,您可以按照下列步驟操作:開始功能表 > 控制台 > 編輯系統環境變數。

正在搜尋"環境變數"
系統屬性
當出現"系統屬性"對話方塊時,我們需要確保選取"進階"選項卡,然後按一下螢幕右下角的"環境變數"按鈕。

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

當出現"編輯環境變數"畫面時,請按一下"新建"按鈕,然後貼上我們在步驟 2 中複製的 Tesseract OCR 安裝路徑。完成此操作後,按一下"確定"按鈕。
將 Tesseract OCR for Windows 安裝目錄新增至環境變數。

就是這樣! 現在我們已經執行了 .exe 安裝程式並將 Tesseract OCR for Windows 安裝位置新增至了環境變數中,我們可以透過在測試映像上執行 Tesseract 來測試我們的安裝是否正常運作。
步驟 4:在測試影像上執行 Tesseract OCR for Windows
若要測試 Tesseract OCR for Windows 是否已成功安裝,請開啟電腦上的命令提示符,然後執行Tesseract命令。 你應該會看到一個輸出,其中包含對 Tesseract 使用選項的簡要說明。

檢查 Tesseract OCR for Windows 是否安裝成功
恭喜您! 您已成功在電腦上安裝了適用於 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
- Mono for macOS 和 Linux
- Xamarin for macOS
IronOCR 使用最新的 Tesseract 5 引擎,可讀取所有主流圖像和 PDF 格式中的文字、條碼和二維碼。該程式庫可在幾分鐘內為桌面、控制台和 Web 應用程式新增 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";步驟 3:測試您的鑰匙
測試您的金鑰是否已正確安裝。
bool isValidLicense = IronOcr.License.IsValidLicense("IRONOCR-MYLICENSE-KEY-1EF01");bool isValidLicense = IronOcr.License.IsValidLicense("IRONOCR-MYLICENSE-KEY-1EF01");開始專案
// 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
}如何在 C# for .NET 中使用 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
}IronOCR Tesseract for C
使用 IronOCR,所有 Tesseract 安裝都完全透過 NuGet 套件管理器完成。
Install-Package IronOcr
Tesseract 5 API 在 IronOCR Tesseract 中
迄今為止,IronTesseract 是目前已知的唯一適用於 .NET Framework 或 Core 的 Tesseract 5 實作。
// 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);
}Tesseract 4 API 在 IronOCR Tesseract 中
// 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);
}為什麼 IronOCR 比 Tesseract 更好:
準確性
超立方體:
如果 Tesseract 遇到旋轉、傾斜、低 DPI、掃描或有背景雜訊的影像,Tesseract 幾乎不可能從該影像中取得資料。 此外,Tesseract 處理該文件也需要很長時間,然後才會提供毫無意義的資訊。
鐵甲:
IronOCR 可以消除這種煩惱。 使用者只需進行最少的配置,通常就能達到 99.8-100% 的準確率。
影像相容性
超立方體:
僅接受 Leptonica PIX 映像格式,該格式在 C# 中是 IntPtr C++ 物件。 PIX 物件不是託管記憶體——在 C# 中處理不當會導致記憶體洩漏。
鐵甲:
影像由記憶體管理。 支援PDF和TIFF格式。 System.Drawing、Stream 和 Byte Array 適用於所有檔案格式。
廣泛的圖像支援:
- PDF文檔
- PDF 頁面
- 多幀 TIFF 文件
- JPEG 和 JPEG2000
- GIF
- PNG
- System.Drawing.Image
- 二進位影像資料(位元組數組) 還有更多…
表現
超立方體:
如果經過適當的調整,並且輸入影像已使用 Photoshop 或 ImageMagick 進行預處理,Google Tesseract 可以執行快速且準確的結果。
鐵甲:
IronOcr .NET Tesseract DLL開箱即用,能夠準確、快速地處理大多數影像。 我們實現了多執行緒技術,以充分利用現在大多數機器使用的多核心處理器。 即使是低解析度影像,通常也能在您的程式中實現較高的精度。 無需使用 Photoshop。
API
超立方體:
我們有兩個自由選擇:
- 使用 Interop 層——GitHub 上找到的許多 Interop 層都已過時,存在未解決的問題、記憶體洩漏和控制台警告。 可能不支援 .NET Core 或 Standard。
- 使用命令列 EXE 程式-部署困難,並且經常受到病毒掃描程式和安全性策略的干擾。
鐵甲:
IronTesseract 是一個經過託管和測試的 Tesseract .NET 函式庫。
文件齊全,支援 IntelliSense。
語言
超立方體:
僅支援100種語言。
鐵甲:
支援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.






