Windows OCR 引擎與 Tesseract:詳細比較
在當今的數位時代,光學字元辨識 (OCR) 技術已成為各行各業不可或缺的一部分,能夠將影像和掃描文件轉換為可編輯和可搜尋的文字。
在眾多可用的 OCR 軟體中,Google Cloud Vision(Cloud Vision API)、Adobe Acrobat Pro DC、ABBYY Finereader、Windows OCR Engine、Tesseract 和IronOCR等軟體脫穎而出,成為傑出的競爭者,每款軟體都提供獨特的功能和特性來幫助進行文件分析。
本文旨在對這三種 OCR 引擎進行全面的比較分析,評估它們的準確性、性能和整合便利性。
1. OCR引擎簡介
OCR引擎是一種軟體工具,旨在識別和提取影像、PDF和其他掃描文件中的純文字。 它們採用複雜的演算法和機器學習技術來精確識別字元並將其轉換為機器可讀的文字檔案。 Windows OCR Engine、Tesseract 和 IronOCR 是三種廣泛使用的 OCR 解決方案,各有優點和應用場景。
2. Windows OCR引擎
Windows OCR 引擎整合到 Windows 作業系統中,為從輸入影像和掃描文件中提取文字提供了一種便捷且使用者友好的解決方案。 利用先進的圖像處理技術,它可以準確地識別各種語言和字體樣式的文字。 Windows OCR 引擎可透過 Windows 執行時間 API 訪問,從而能夠無縫整合到 Windows 應用程式中,並具備命令列工具的功能。
2.1 Windows OCR引擎的主要特性
-語言支援:Windows OCR 引擎支援多種語言,使其適用於多語言文件。 -影像處理:它採用複雜的影像處理演算法來提高印刷文字的辨識準確率,即使在低品質影像中也是如此。 -與 Windows 應用程式整合:Windows OCR 引擎與 Windows 應用程式無縫集成,使開發人員能夠將 OCR 功能完全融入他們的軟體中。
2.2 程式碼範例
using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Windows.Graphics.Imaging;
using Windows.Media.Ocr;
class Program
{
static async Task Main(string[] args)
{
// Provide the path to the image file
string imagePath = "sample.png";
try
{
// Call the ExtractText method to extract text from the image
string extractedText = await ExtractText(imagePath);
// Display the extracted text
Console.WriteLine("Extracted Text:");
Console.WriteLine(extractedText);
}
catch (Exception ex)
{
Console.WriteLine("An error occurred: " + ex.Message);
}
}
public static async Task<string> ExtractText(string image)
{
// Initialize StringBuilder to store extracted text
StringBuilder text = new StringBuilder();
try
{
// Open the image file stream
using (var fileStream = File.OpenRead(image))
{
// Create a BitmapDecoder from the image file stream
var bmpDecoder = await BitmapDecoder.CreateAsync(fileStream.AsRandomAccessStream());
// Get the software bitmap from the decoder
var softwareBmp = await bmpDecoder.GetSoftwareBitmapAsync();
// Create an OCR engine from user profile languages
var ocrEngine = OcrEngine.TryCreateFromUserProfileLanguages();
// Recognize text from the software bitmap
var ocrResult = await ocrEngine.RecognizeAsync(softwareBmp);
// Append each line of recognized text to the StringBuilder
foreach (var line in ocrResult.Lines)
{
text.AppendLine(line.Text);
}
}
}
catch (Exception ex)
{
Console.WriteLine("Error during OCR process: " + ex.Message);
}
// Return the extracted text
return text.ToString();
}
}using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Windows.Graphics.Imaging;
using Windows.Media.Ocr;
class Program
{
static async Task Main(string[] args)
{
// Provide the path to the image file
string imagePath = "sample.png";
try
{
// Call the ExtractText method to extract text from the image
string extractedText = await ExtractText(imagePath);
// Display the extracted text
Console.WriteLine("Extracted Text:");
Console.WriteLine(extractedText);
}
catch (Exception ex)
{
Console.WriteLine("An error occurred: " + ex.Message);
}
}
public static async Task<string> ExtractText(string image)
{
// Initialize StringBuilder to store extracted text
StringBuilder text = new StringBuilder();
try
{
// Open the image file stream
using (var fileStream = File.OpenRead(image))
{
// Create a BitmapDecoder from the image file stream
var bmpDecoder = await BitmapDecoder.CreateAsync(fileStream.AsRandomAccessStream());
// Get the software bitmap from the decoder
var softwareBmp = await bmpDecoder.GetSoftwareBitmapAsync();
// Create an OCR engine from user profile languages
var ocrEngine = OcrEngine.TryCreateFromUserProfileLanguages();
// Recognize text from the software bitmap
var ocrResult = await ocrEngine.RecognizeAsync(softwareBmp);
// Append each line of recognized text to the StringBuilder
foreach (var line in ocrResult.Lines)
{
text.AppendLine(line.Text);
}
}
}
catch (Exception ex)
{
Console.WriteLine("Error during OCR process: " + ex.Message);
}
// Return the extracted text
return text.ToString();
}
}Imports System
Imports System.IO
Imports System.Text
Imports System.Threading.Tasks
Imports Windows.Graphics.Imaging
Imports Windows.Media.Ocr
Friend Class Program
Shared Async Function Main(ByVal args() As String) As Task
' Provide the path to the image file
Dim imagePath As String = "sample.png"
Try
' Call the ExtractText method to extract text from the image
Dim extractedText As String = Await ExtractText(imagePath)
' Display the extracted text
Console.WriteLine("Extracted Text:")
Console.WriteLine(extractedText)
Catch ex As Exception
Console.WriteLine("An error occurred: " & ex.Message)
End Try
End Function
Public Shared Async Function ExtractText(ByVal image As String) As Task(Of String)
' Initialize StringBuilder to store extracted text
Dim text As New StringBuilder()
Try
' Open the image file stream
Using fileStream = File.OpenRead(image)
' Create a BitmapDecoder from the image file stream
Dim bmpDecoder = Await BitmapDecoder.CreateAsync(fileStream.AsRandomAccessStream())
' Get the software bitmap from the decoder
Dim softwareBmp = Await bmpDecoder.GetSoftwareBitmapAsync()
' Create an OCR engine from user profile languages
Dim ocrEngine = OcrEngine.TryCreateFromUserProfileLanguages()
' Recognize text from the software bitmap
Dim ocrResult = Await ocrEngine.RecognizeAsync(softwareBmp)
' Append each line of recognized text to the StringBuilder
For Each line In ocrResult.Lines
text.AppendLine(line.Text)
Next line
End Using
Catch ex As Exception
Console.WriteLine("Error during OCR process: " & ex.Message)
End Try
' Return the extracted text
Return text.ToString()
End Function
End Class2.2.1 輸出
Windows OCR 引擎與 Tesseract(OCR 功能比較):圖 1 - Windows OCR 引擎程式碼的控制台輸出
3. 超立方體
Tesseract是Google開發的開源 OCR 引擎,因其準確性和多功能性而廣受歡迎。 它支援 100 多種語言,並可處理各種影像格式,包括 TIFF、JPEG 和 PNG。 Tesseract OCR 引擎採用深度學習演算法和神經網絡,實現了高水準的文字辨識準確率,使其適用於廣泛的應用。
3.1 超立方體的主要特徵
-語言支援:Tesseract 引擎支援 100 多種語言,包括阿拉伯語和中文等複雜文字。 -影像預處理:它提供廣泛的影像預處理功能,包括去斜、二值化和降噪,以提高文字辨識準確率。 -自訂選項:Tesseract 允許使用者微調 OCR 參數並針對特定用例訓練自訂模型,從而提高準確性和效能。
3.2 程式碼範例
using Patagames.Ocr;
class TesseractExample
{
static void Main(string[] args)
{
// Create an OCR API instance
using (var api = OcrApi.Create())
{
// Initialize the OCR engine for the English language
api.Init(Patagames.Ocr.Enums.Languages.English);
// Extract text from the image
string plainText = api.GetTextFromImage(@"C:\Users\buttw\source\repos\ironqr\ironqr\bin\Debug\net5.0\Iron.png");
// Display the extracted text
Console.WriteLine(plainText);
}
}
}using Patagames.Ocr;
class TesseractExample
{
static void Main(string[] args)
{
// Create an OCR API instance
using (var api = OcrApi.Create())
{
// Initialize the OCR engine for the English language
api.Init(Patagames.Ocr.Enums.Languages.English);
// Extract text from the image
string plainText = api.GetTextFromImage(@"C:\Users\buttw\source\repos\ironqr\ironqr\bin\Debug\net5.0\Iron.png");
// Display the extracted text
Console.WriteLine(plainText);
}
}
}Imports Patagames.Ocr
Friend Class TesseractExample
Shared Sub Main(ByVal args() As String)
' Create an OCR API instance
Using api = OcrApi.Create()
' Initialize the OCR engine for the English language
api.Init(Patagames.Ocr.Enums.Languages.English)
' Extract text from the image
Dim plainText As String = api.GetTextFromImage("C:\Users\buttw\source\repos\ironqr\ironqr\bin\Debug\net5.0\Iron.png")
' Display the extracted text
Console.WriteLine(plainText)
End Using
End Sub
End Class3.2.1 輸出
Windows OCR 引擎與 Tesseract(OCR 功能比較):圖 2 - Tesseract 程式碼的控制台輸出
4. 鐵氧體
IronOCR是由 Iron Software 開發的一款功能強大的 OCR 引擎,以其卓越的準確性、易用性和多樣的語言支援而聞名。 它提供本地 OCR 功能,支援 125 多種語言,使其適用於全球應用。 IronOCR 利用先進的機器學習演算法和雲端視覺技術,即使在具有挑戰性的場景下也能提供精確的文字辨識結果。
4.1 IronOCR 的主要特點
-高精度:IronOCR 在文字辨識方面提供業界領先的精確度,確保在各種文件類型和語言中都能獲得可靠的結果。 -多功能語言支援:支援超過 125 種語言,並提供全面的語言包,以實現無縫的多語言文字識別。 -簡單集成:IronOCR 提供與 .NET 應用程式的簡單集成,具有直觀的 API 和豐富的文檔,可簡化開發過程,包括對原始圖像進行預處理和後處理以提取文字。
4.2 安裝 IronOCR
在進行編碼範例之前,讓我們先來看看如何使用 NuGet 套件管理器安裝 IronOCR。
- 在 Visual Studio 中,前往"工具"功能表並選擇 NuGet 套件管理器。
- 將出現一個新列表,在這裡選擇 NuGet 套件管理器作為解決方案。
Windows OCR 引擎與 Tesseract(OCR 功能比較):圖 3 - Visual Studio NuGet 套件管理器位置
- 將出現一個新窗口,轉到"瀏覽"選項卡,然後點擊在搜尋欄中鍵入"IronOCR"。
- 將顯示包裹清單。 選擇最新的 IronOCR 軟體包,然後點擊安裝。
4.3 程式碼範例(C#)
using IronOcr;
class IronOCRExample
{
static void Main(string[] args)
{
// Create an IronTesseract instance
var ocr = new IronTesseract();
// Set the language for OCR recognition
ocr.Language = OcrLanguage.English;
// Perform OCR on the specified image
var result = ocr.Read(@"C:\Users\buttw\source\repos\ironqr\ironqr\bin\Debug\net5.0\Iron.png");
// Display the extracted text
Console.WriteLine(result.Text);
}
}using IronOcr;
class IronOCRExample
{
static void Main(string[] args)
{
// Create an IronTesseract instance
var ocr = new IronTesseract();
// Set the language for OCR recognition
ocr.Language = OcrLanguage.English;
// Perform OCR on the specified image
var result = ocr.Read(@"C:\Users\buttw\source\repos\ironqr\ironqr\bin\Debug\net5.0\Iron.png");
// Display the extracted text
Console.WriteLine(result.Text);
}
}Imports IronOcr
Friend Class IronOCRExample
Shared Sub Main(ByVal args() As String)
' Create an IronTesseract instance
Dim ocr = New IronTesseract()
' Set the language for OCR recognition
ocr.Language = OcrLanguage.English
' Perform OCR on the specified image
Dim result = ocr.Read("C:\Users\buttw\source\repos\ironqr\ironqr\bin\Debug\net5.0\Iron.png")
' Display the extracted text
Console.WriteLine(result.Text)
End Sub
End Class4.3.1 輸出
5. 對比評估
5.1 準確性和性能
- Windows OCR 引擎和Tesseract具有不錯的準確度,但可能難以處理複雜的佈局。
- IronOCR :準確度極高,能夠對各種文件類型和語言(包括雜訊影像)提供可靠的結果。
5.2 整合便利性
- Windows OCR 引擎:與 Windows 應用程式無縫集成,但缺乏自訂選項。
- Tesseract :整合需要額外的配置和依賴項,但提供了廣泛的自訂選項。
- IronOCR :提供與 .NET 應用程式的簡單集成,具有直覺的 API 和全面的文檔。
5.3 語言支持
與 Tesseract 和 IronOCR 相比, Windows OCR 引擎支援的語言數量有限。
- Tesseract :支援超過 100 種語言。
- IronOCR :支援超過 125 種語言,使其適用於全球應用。
6.結論
總之,雖然 Windows OCR 引擎和 Tesseract 都是常用的文字辨識工具,但IronOCR憑藉其精準度和多功能性脫穎而出,成為最優秀的 OCR 引擎。它業界領先的準確率、廣泛的語言支援以及簡易的整合方式,使其成為尋求可靠 OCR 功能的企業和開發人員的理想之選。 透過利用 IronOCR,組織可以簡化文件處理工作流程,提高資料擷取準確性,並從掃描的文件和影像中挖掘有價值的見解。






