跳至頁尾內容
OCR 工具

如何使用 Tesseract 從影像中提取文字

利用像IronOCR和Tesseract這樣的程式庫,開發者可以存取先進的算法和機器學習技術,從圖像和掃描的文件中提取文字資訊。 本教程將向讀者展示如何使用Tesseract程式庫執行圖像文字提取,然後介紹IronOCR的獨特方法。

1. 使用Tesseract進行OCR

1.1. 安裝Tesseract

使用NuGet套件管理器控制台,輸入以下命令:

Install-Package Tesseract

或者通過NuGet套件管理器下載套件。

如何獲得OCR文字識別,圖1:在NuGet套件管理器中安裝Tesseract套件 在NuGet套件管理器中安裝Tesseract套件

您必須在安裝NuGet套件後,手動安裝並將語言文件保存到專案資料夾中。 這可以被視為這個特定程式庫的不足。

存取以下網站以下載語言文件。 下載後,解壓縮文件,並將"tessdata"文件夾新增到您的專案的debug資料夾中。

1.2. 使用Tesseract(快速入門)

可以使用下面的源程式碼對給定的圖像進行OCR處理:

using Tesseract;

class Program
{
    static void Main()
    {
        // Initialize Tesseract engine with English language data
        using var ocrEngine = new TesseractEngine(@"tessdata", "eng", EngineMode.Default);

        // Load the image to be processed
        using var img = Pix.LoadFromFile("Demo.png");

        // Process the image to extract text
        using var res = ocrEngine.Process(img);

        // Output the recognized text
        Console.WriteLine(res.GetText());
        Console.ReadKey();
    }
}
using Tesseract;

class Program
{
    static void Main()
    {
        // Initialize Tesseract engine with English language data
        using var ocrEngine = new TesseractEngine(@"tessdata", "eng", EngineMode.Default);

        // Load the image to be processed
        using var img = Pix.LoadFromFile("Demo.png");

        // Process the image to extract text
        using var res = ocrEngine.Process(img);

        // Output the recognized text
        Console.WriteLine(res.GetText());
        Console.ReadKey();
    }
}
Imports Tesseract

Friend Class Program
	Shared Sub Main()
		' Initialize Tesseract engine with English language data
		Dim ocrEngine = New TesseractEngine("tessdata", "eng", EngineMode.Default)

		' Load the image to be processed
		Dim img = Pix.LoadFromFile("Demo.png")

		' Process the image to extract text
		Dim res = ocrEngine.Process(img)

		' Output the recognized text
		Console.WriteLine(res.GetText())
		Console.ReadKey()
	End Sub
End Class
$vbLabelText   $csharpLabel
  • 首先,必須建立一個TesseractEngine物件,將語言資料載入引擎中。
  • 然後藉助Pix.LoadFromFile載入所需的圖像文件。
  • 然後將圖像傳入Process方法提取文字。
  • 使用GetText方法獲得識別的文字並列印到控制台。

如何獲得OCR文字識別,圖2:從圖像中提取的文字 從圖像中提取的文字

1.3 Tesseract考量

  1. 從3.00版本起,Tesseract支持輸出文字格式、OCR位置資料和頁面佈局分析。
  2. 雖然Tesseract在Windows、Linux和MacOS上可用,但由於開發支持有限,因此主要確認在Windows和Ubuntu上按預期運作。
  3. Tesseract能區分等寬和比例字體文字。
  4. 使用像OCRopus這樣的前端,Tesseract非常適合用作後端,並可以用於更具挑戰性的OCR工作,如佈局分析。
  5. Tesseract的一些不足之處:
    • 最新版本未設計為可在Windows上編譯
    • Tesseract的C# API封裝維護不頻繁,已經落後於Tesseract的新發布數年

若要了解更多關於Tesseract與C#的資訊,請存取Tesseract教程

2. 使用IronOCR進行OCR

2.1. 安裝IronOCR

在NuGet套件管理器控制台中輸入下一條命令:

Install-Package IronOcr

或者通過NuGet套件管理器安裝IronOCR程式庫,並搭配其他語言額外套件,簡單方便。

如何獲得OCR文字識別,圖3:通過NuGet套件管理器安裝IronOCR和語言套件 通過NuGet套件管理器安裝IronOCR和語言套件

2.2. 使用IronOCR

以下是個範例程式碼,用於識別給定圖像中的文字:

using IronOcr;

class Program
{
    static void Main()
    {
        // Create an IronTesseract instance with predefined settings
        var ocr = new IronTesseract()
        {
            Language = OcrLanguage.EnglishBest,
            Configuration = { TesseractVersion = TesseractVersion.Tesseract5 }
        };

        // Create an OcrInput instance for image processing
        using var input = new OcrInput();

        // Load the image to be processed
        input.AddImage("Demo.png");

        // Process the image and extract text
        var result = ocr.Read(input);

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

class Program
{
    static void Main()
    {
        // Create an IronTesseract instance with predefined settings
        var ocr = new IronTesseract()
        {
            Language = OcrLanguage.EnglishBest,
            Configuration = { TesseractVersion = TesseractVersion.Tesseract5 }
        };

        // Create an OcrInput instance for image processing
        using var input = new OcrInput();

        // Load the image to be processed
        input.AddImage("Demo.png");

        // Process the image and extract text
        var result = ocr.Read(input);

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

Friend Class Program
	Shared Sub Main()
		' Create an IronTesseract instance with predefined settings
		Dim ocr = New IronTesseract() With {
			.Language = OcrLanguage.EnglishBest,
			.Configuration = { TesseractVersion = TesseractVersion.Tesseract5 }
		}

		' Create an OcrInput instance for image processing
		Dim input = New OcrInput()

		' Load the image to be processed
		input.AddImage("Demo.png")

		' Process the image and extract text
		Dim result = ocr.Read(input)

		' Output the recognized text
		Console.WriteLine(result.Text)
		Console.ReadKey()
	End Sub
End Class
$vbLabelText   $csharpLabel
  • 此程式碼初始化一個IronTesseract物件,設置語言和Tesseract版本。
  • 然後建立一個AddImage方法載入圖像文件。
  • Read方法處理圖像並提取文字,然後將其列印到控制台。

如何獲得OCR文字識別,圖4:使用IronOCR程式庫提取的文字輸出 使用IronOCR程式庫提取的文字輸出

2.3 IronOCR考量

  1. IronOCR是Tesseract程式庫的擴展,具有更高的穩定性和準確性。
  2. IronOCR可以從PDF和照片中讀取文字內容。 它還可以讀取超過20種不同型別的條碼和QR碼。
  3. 輸出可以以純文字、結構化資料、條碼或QR碼的形式呈現。
  4. 該程式庫可以辨識全球125種語言
  5. IronOCR能靈活地在所有 .NET 環境中運作(控制台、Web、桌面等),也支持最新的手機框架如Mono、Xamarin、AzureMAUI
  6. IronOCR提供免費試用,且有開發版價格較低。 了解更多關於授權的資訊

如需詳細的IronOCR教程,請參考本文章以從C#中的圖像中讀取文字

Kannaopat Udonpant
軟體工程師
在成為軟體工程師之前,Kannapat在日本北海道大學完成了環境資源博士學位。在攻讀學位期間,Kannapat還成為車輛機器人實驗室的一員,該實驗室隸屬於生產工程系。在2022年,他憑藉C#技能加入了Iron Software的工程團隊,專注於IronPDF。Kannapat珍視他的工作,因為他能直接向撰寫大部分IronPDF程式碼的開發者學習。除了同儕學習,Kannapat還喜歡在Iron Software工作的社交方面。不寫程式碼或文件時,Kannapat通常在他的PS5上玩遊戲或重看The Last of Us。

Iron 支援團隊

我們線上24小時,每週5天。
聊天
電子郵件
給我打電話