OCR 工具

Windows OCR 引擎與 Tesseract 的比較 (OCR 功能比較)

發佈 2024年4月3日
分享:

在今天的數位時代,光學文字辨識 (光學字符識別) 技術已成為各行各業不可或缺的一部分,使圖像和掃描文件轉換為可編輯和可搜索的文本。

在眾多OCR軟件中,如Google Cloud Vision (云端视觉 API),Adobe Acrobat Pro DC、ABBYY Finereader 和許多其他,Windows OCR 引擎 vs Tesseract,和 IronOCR 脫穎而出,成為突出的競爭者,每個都提供獨特的功能和能力來幫助文件分析。

本文旨在對這三個OCR引擎進行全面比較分析,評估其準確性、性能和集成的便利性。

1. OCR 引擎簡介

OCR 引擎是設計用來識別和提取圖像、PDF 和其他掃描文件中的純文字的軟件工具。它們採用複雜的算法和機器學習技術來準確識別字符,並將其轉換為機器可讀的文本文件。Windows OCR Engine、Tesseract 和 IronOCR 代表了三種廣泛使用的 OCR 解決方案,每個都有其優勢和應用。

2. Windows OCR 引擎

Windows OCR 引擎集成到 Windows 操作系統提供了一個方便且使用者友好的解決方案,用於從輸入影像和掃描文件中提取文本。利用先進的影像處理技術,它能夠準確識別多種語言和字體樣式的文本。Windows OCR 引擎可通過 Windows Runtime 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;
class Program
{
    static async Task Main(string [] args)
    {
        // Provide the path to the image file
        string imagePath = "sample.png";
        try
        {
            // Instantiate the program class
            Program program = new Program();
            // Call the ExtractText method to extract text from the image
            string extractedText = await program.ExtractText(imagePath);
            // Display the extracted text
            Console.WriteLine("Extracted Text:");
            Console.WriteLine(extractedText);
        }
        catch (Exception ex)
        {
            Console.WriteLine("An error occurred: " + ex.Message);
        }
    }
    public 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 = System.IO.File.OpenRead(image))
            {
                Console.WriteLine("Extracted Text:");
                // Create a BitmapDecoder from the image file stream
                var bmpDecoder = await Windows.Graphics.Imaging.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 = Windows.Media.Ocr.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)
        {
            throw ex; // Propagate the exception
        }
        // Return the extracted text
        return text.ToString();
    }
}
using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;
class Program
{
    static async Task Main(string [] args)
    {
        // Provide the path to the image file
        string imagePath = "sample.png";
        try
        {
            // Instantiate the program class
            Program program = new Program();
            // Call the ExtractText method to extract text from the image
            string extractedText = await program.ExtractText(imagePath);
            // Display the extracted text
            Console.WriteLine("Extracted Text:");
            Console.WriteLine(extractedText);
        }
        catch (Exception ex)
        {
            Console.WriteLine("An error occurred: " + ex.Message);
        }
    }
    public 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 = System.IO.File.OpenRead(image))
            {
                Console.WriteLine("Extracted Text:");
                // Create a BitmapDecoder from the image file stream
                var bmpDecoder = await Windows.Graphics.Imaging.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 = Windows.Media.Ocr.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)
        {
            throw ex; // Propagate the exception
        }
        // Return the extracted text
        return text.ToString();
    }
}
Imports System
Imports System.IO
Imports System.Text
Imports System.Threading.Tasks
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
			' Instantiate the program class
			Dim program As New Program()
			' Call the ExtractText method to extract text from the image
			Dim extractedText As String = Await program.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 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 = System.IO.File.OpenRead(image)
				Console.WriteLine("Extracted Text:")
				' Create a BitmapDecoder from the image file stream
				Dim bmpDecoder = Await Windows.Graphics.Imaging.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 = Windows.Media.Ocr.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
			Throw ex ' Propagate the exception
		End Try
		' Return the extracted text
		Return text.ToString()
	End Function
End Class
VB   C#

2.2.1 輸出

Windows OCR 引擎與 Tesseract 的比較(OCR 功能比較):圖1 - Windows OCR 引擎代碼的控制台輸出

3. Tesseract

Tesseract,Google開發的一個開源 OCR 引擎,因其準確性和多功能性而廣受歡迎。它支持超過 100 種語言,並且可以處理各種圖像格式,包括 TIFF、JPEG 和 PNG。Tesseract OCR 引擎使用深度學習算法和神經網絡來實現高水平的文本識別準確性,使其適用於廣泛的應用。

3.1 Tesseract 的主要功能

  • 語言支援:Tesseract 引擎支援超過 100 種語言,包括阿拉伯語和中文等複雜文字。
  • 圖像預處理:它提供廣泛的圖像預處理功能,包括去偏斜、二值化和降噪,以提高文字識別精度。
  • 自訂選項:Tesseract 允許用戶微調 OCR 參數並針對特定用例訓練自訂模型,以提升準確性和性能。

3.2 代碼範例

using Patagames.Ocr;
using (var api = OcrApi.Create())
{
    api.Init(Patagames.Ocr.Enums.Languages.English);
    string plainText = api.GetTextFromImage(@"C:\Users\buttw\source\repos\ironqr\ironqr\bin\Debug\net5.0\Iron.png");
    Console.WriteLine(plainText);
}
using Patagames.Ocr;
using (var api = OcrApi.Create())
{
    api.Init(Patagames.Ocr.Enums.Languages.English);
    string plainText = api.GetTextFromImage(@"C:\Users\buttw\source\repos\ironqr\ironqr\bin\Debug\net5.0\Iron.png");
    Console.WriteLine(plainText);
}
Imports Patagames.Ocr
Using api = OcrApi.Create()
	api.Init(Patagames.Ocr.Enums.Languages.English)
	Dim plainText As String = api.GetTextFromImage("C:\Users\buttw\source\repos\ironqr\ironqr\bin\Debug\net5.0\Iron.png")
	Console.WriteLine(plainText)
End Using
VB   C#

3.2.1 輸出

Windows OCR 引擎與 Tesseract 比較(OCR 功能比較):圖 2 - Tesseract 代碼的控制臺輸出

4. IronOCR

IronOCR由 Iron Software 開發的強大 OCR 引擎,以其卓越的準確性、易用性和多語言支持脫穎而出。它提供內部部署的 OCR 功能,支持超過127種語言,使其適用於全球應用。IronOCR 利用先進的機器學習算法和雲端視覺技術,即使在具有挑戰性的場景中也能提供精確的文本識別結果。

4.1 IronOCR 的主要功能

  • 高準確度:IronOCR 提供業界領先的文字識別準確度,確保在各種文件類型和語言中獲得可靠的結果。
  • 多種語言支持:它支持超過127種語言,並提供全面的語言包,以實現無縫的多語種文字識別。
  • 簡單整合:IronOCR 提供與 .NET 應用的簡單整合,使用直觀的 API 和詳細的文件,通過預處理和後處理原始圖像來提取文字,簡化開發過程。

4.2 安裝 IronOCR

在進行程式碼範例之前,讓我們看看如何使用 NuGet 套件管理工具安裝 IronOCR。

  1. 在 Visual Studio 中,進入工具選單並選擇 NuGet 套件管理工具。

  2. 出現一個新列表,在此列表中選擇解決方案的 NuGet 套件管理工具。

Windows OCR 引擎與 Tesseract的比較(OCR功能比較):圖3 - 如何找到 Visual Studio 的 NuGet 程式包管理器

  1. 將會出現一個新窗口,前往「瀏覽」標籤頁,在搜索欄中輸入「IronOCR」。

  2. 會出現一個包列表,選擇最新的 IronOCR 包並點擊安裝。

Windows OCR 引擎與 Tesseract(OCR 功能對比):圖 4 - 安裝 IronOCR 套件

4.3 代碼範例 (C#)

using IronOcr;
var ocr = new IronTesseract();
ocr.Language = OcrLanguage.English;
var result = ocr.Read("C:\\Users\\buttw\\source\\repos\\ironqr\\ironqr\\bin\\Debug\\net5.0\\Iron.png");
Console.WriteLine(result.Text);
using IronOcr;
var ocr = new IronTesseract();
ocr.Language = OcrLanguage.English;
var result = ocr.Read("C:\\Users\\buttw\\source\\repos\\ironqr\\ironqr\\bin\\Debug\\net5.0\\Iron.png");
Console.WriteLine(result.Text);
Imports IronOcr
Private ocr = New IronTesseract()
ocr.Language = OcrLanguage.English
Dim result = ocr.Read("C:\Users\buttw\source\repos\ironqr\ironqr\bin\Debug\net5.0\Iron.png")
Console.WriteLine(result.Text)
VB   C#

4.3.1 輸出

Windows OCR 引擎 vs Tesseract (OCR 特性比較): 圖5 - IronOCR 代碼的控制台輸出

5. 比較評估

5.1 精確度和性能

  • Windows OCR 引擎和 Tesseract 提供相當的精確度,但可能在處理複雜的佈局和手寫文本時遇到困難。
  • IronOCR:在精確度上表現出色,能夠在各種文檔類型和語言中提供可靠的結果,包括手寫文本和噪聲影像。

5.2 易於整合

  • Windows OCR 引擎:可無縫整合至 Windows 應用程式,但缺乏自訂選項。
  • Tesseract:需要額外配置和依賴項才能完成整合,但提供豐富的自訂選項。
  • IronOCR:提供與 .NET 應用程式的簡單整合,具有直觀的API和全面的文件說明。

5.3 語言支持

  • Windows OCR 引擎和 Tesseract 支持的語言數量有限,相比之下,Tesseract 和 IronOCR 的支持範圍更廣。
  • IronOCR:提供超過127種語言的支持,非常適合全球應用。

6. 結論

總而言之,雖然 Windows OCR 引擎和 Tesseract 是文本識別的熱門選擇, IronOCR 成為最準確且多功能的OCR引擎。其業界領先的準確性、廣泛的語言支持以及簡單的整合,讓它成為企業和開發者尋求可靠OCR功能的卓越解決方案。通過利用IronOCR,組織可以簡化文件處理工作流程,提高數據提取的準確性,並從掃描的文件和圖像中解鎖有價值的見解。

IronOCR 提供 免費試用了解更多關於IronOCR及其功能,請訪問 這裡.

< 上一頁
雲端OCR(OCR功能比較)
下一個 >
Azure OCR 與 Google OCR(OCR 功能比較)

準備開始了嗎? 版本: 2024.10 剛剛發布

免費 NuGet 下載 總下載次數: 2,433,305 查看許可證 >