Windows 中的 Tesseract OCR(程式碼範例教學)
Tesseract OCR 是什麼?
Tesseract是一個光學字元識別引擎,可以在多種操作系統上使用。 它是一個免費軟體,根據Apache許可證發布。 在本指南中,我將帶您一步步了解如何在我的Windows 10計算機上安裝Tesseract。第5個主要版本是當前的穩定版本,始於2021年11月30日發布的5.0.0。
如何在Windows中使用Tesseract OCR
- 在Windows 10上使用.exe文件安裝Tesseract OCR
- 配置Tesseract安裝
- 將安裝路徑新增到環境變數中
- 在測試圖像上運行Tesseract OCR for Windows
- 使用C#程式庫來獲得更直觀的API和進階方法
步驟1:使用.exe文件在Windows 10中安裝Tesseract OCR:
要安裝語言資料:sudo port install tesseract -<langcode>
在MacPorts的Tesseract Homebrew頁面上可以找到語言程式碼的列表。 安裝Windows的Tesseract OCR的第一步是下載與您的計算機的操作系統相對應的.exe安裝程式。
步驟2:配置安裝
接下來,我們需要配置Tesseract的安裝。 如果您對設置信心十足並且只想將Tesseract OCR for Windows的預設語言設為英語,那麼選擇所有預設選項進行安裝應該可行。
安裝程式語言
這只是對話框和幫助資訊的語言。 如果我們想,可以在多種語言下運行Tesseract OCR for Windows:
Windows的Tesseract OCR安裝語言
Tesseract OCR 安裝設置
安裝介面建議在繼續安裝之前關閉所有其他應用程式。
Windows的Tesseract OCR安裝介面。
選擇安裝位置
接下來,我們將選擇安裝位置。 在進行下一步之前,確保將安裝位置複製到.txt文件中。安裝完成後,我們需要將安裝位置新增到計算機的環境變數中。
選擇安裝位置。
選擇元件
預設情況下,ScrollView、Training Tools、Shortcuts建立和語言資料會被選中。 除非您有特定的原因不安裝這些,我們會想保留所有選項。
Windows的Tesseract OCR安裝組件預設設定。
如果我們向下滾動並展開附加的腳本資料,我們會看到可以下載並安裝附加腳本資料的選項。 這有助於提高從某些有腳本的語言中提取文字的準確性。 是否安裝這些由您自行決定。
可選的腳本安裝組件。
選擇開始選單資料夾
在安裝的最後一步,我們將被要求選擇Tesseract OCR for Windows快捷方式的開始選單文件夾。 我已經將其設為預設名稱:'Tesseract-OCR'。
選擇Tesseract OCR for Windows快捷方式的開始選單文件夾。
點擊安裝後,Windows的Tesseract OCR將開始安裝。 我們的下一步是將安裝路徑新增到計算機的環境變數中。
步驟3:將安裝路徑新增到環境變數中
控制面板
要將安裝位置新增到環境變數中,請前往開始選單並搜尋'環境變數'。 您應該會看到一個用於編輯系統環境變數的結果。 如果沒有,您可以始終使用以下步驟: 開始選單 > 控制面板 > 編輯系統環境變數。
搜尋'環境變數'
系統屬性
當展示'系統屬性'對話框時,我們會確保點擊了'高級'選項卡,然後點擊螢幕右下角的'環境變數'按鈕。
環境變數
在系統變數下,我們將點擊編輯按鈕。
在'編輯環境變數'介面上,點擊新建按鈕,並貼上我們在步驟2中複製的Tesseract OCR安裝路徑。完成後,點擊'OK' 按鈕。
將 Windows 安裝目錄的 Tesseract OCR 新增到環境變數
就是這樣! 現在我們已經運行了.exe安裝程式並將這個Tesseract OCR for Windows的安裝位置新增到環境變數中,我們可以通過在測試圖像上運行Tesseract來測試我們的安裝是否正常。
步驟4:在測試圖像上運行Windows的Tesseract OCR
為了測試Windows的Tesseract OCR是否已成功安裝,請在您的計算機上打開命令提示符,然後運行Tesseract指令。 您應該能看到一個簡單說明Tesseract使用選項的輸出。
檢查Tesseract OCR for Windows是否成功安裝
恭喜! 您已經成功在您的計算機上安裝了Tesseract OCR for Windows。
使用IronOCR進行Optical Character Recognition (OCR)工作的優點:
IronOCR提供Tesseract OCR在Mac、Windows、Linux、Azure和Docker上用於:
- .NET Framework 4.0 +
- .NET Standard 2.0 +
- .NET Core 2.0 +
- .NET 5
- Mono在macOS和Linux上
- Xamarin在macOS上
IronOCR使用最新版Tesseract 5引擎從所有主要圖像和PDF格式中讀取文字、條碼和QR碼。此程式庫可讓桌面、控制台和網頁應用程式在幾分鐘內即可擁有OCR功能。 支持超過125種國際語言。 許可證起價從$999。
步驟1:安裝最新版本的IronOCR
安裝DLL
下載IronOCR DLL 直接到您的計算機。
安裝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"
步驟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")
開始專案
// 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
如何在C#中使用Tesseract OCR for .NET?
- 在Visual Studio中安裝Google Tesseract和IronOCR for .NET
- 檢查C#中的最新版本
- 檢查準確性和圖像相容性
- 測試性能和API功能
- 考慮多語言支持
Code Example for .NET OCR Usage — Extract Text from Images in 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
IronOCR Tesseract for C
使用IronOCR,所有Tesseract的安裝都在NuGet程式包管理器中完全完成。
Install-Package IronOcr
Tesseract 5 API in 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);
}
' 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
Tesseract 4 API in 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);
}
' 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
為什麼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可以做到這一點及更多,僅需一行程式碼。 的確,IronOCR使用Tesseract作為其內部OCR引擎,一個精細調整的Tesseract,專為C#建構,並具有作為標準的許多性能增強和功能。




