跳過到頁腳內容
OCR 工具

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

利用諸如IronOCR和Tesseract等庫使開發人員能夠訪問用於從圖像和掃描文件中提取文本信息的高級算法和機器學習技術。 本教程將向讀者顯示如何使用Tesseract庫從圖像中執行文本提取,然後最後介紹IronOCR的獨特方法。

1. 使用Tesseract進行光學字符識別

1.1. 安裝Tesseract

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

Install-Package Tesseract

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

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

在安裝NuGet包後,您必須手動安裝並將語言文件保存在項目文件夾中。 這可以被認為是該特定庫的一個缺點。

訪問以下網站下載語言文件。 下載後,解壓文件,將"tessdata"文件夾添加到您的項目調試文件夾中。

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加載所需的圖像文件。
  • 圖像被傳遞到TesseractEngine中使用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進行光學字符識別

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版本。
  • 然後創建一個OcrInput物件來使用AddImage方法加載圖像文件。
  • IronTesseractRead方法處理圖像並提取文本,然後將其打印到控制台。

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

2.3 IronOCR考慮事項

  1. IronOCR是Tesseract庫的擴展,引入了更高的穩定性和準確性。
  2. IronOCR可以從PDF和照片中讀取文本內容。 它還可以閱讀超過20種不同類型的條形碼和QR碼。
  3. 輸出可以呈現為純文本、結構化數據、條形碼或QR碼。
  4. 該庫可識別全球125種語言
  5. IronOCR works in all .NET environments flexibly (console, Web, desktop, etc.), and also supports the latest mobile frameworks such as Mono, Xamarin, Azure, and MAUI.
  6. IronOCR提供免費試用,並有比價較低的開發版本。 了解更多關於許可的資訊

有關詳細的IronOCR教程,請參考此文章閱讀如何從C#中的圖像讀取文本

Kannaopat Udonpant
軟體工程師
在成為软件工程師之前,Kannapat 從日本北海道大學完成了環境資源博士學位。在追逐學位期间,Kannapat 還成為了生產工程系一部份——汽車机器人实验室的成員。2022 年,他利用他的 C# 技能加入 Iron Software 的工程團隊, 專注於 IronPDF。Kannapat 珍惜他的工作,因为他直接向编写大部分 IronPDF 使用的代码的开发者学习。除了同行学习,Kannapat 还喜欢在 Iron Software 工作的社交十环。当他不编写代码或文档时,Kannapat 通常在他的 PS5 上打游戏或重看《The Last of Us》。