跳至页脚内容
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"文件夹添加到项目的调试文件夹中。

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 超立方体的考虑因素

  1. Tesseract 从 3.00 版本开始支持输出文本格式、OCR 位置数据和页面布局分析。
  2. Tesseract 可在 Windows、Linux 和 MacOS 上运行,但由于开发支持有限,目前已确认其主要在 Windows 和 Ubuntu 上按预期运行。
  3. Tesseract 可以区分等宽字体和比例字体。
  4. 利用 OCRopus 等前端,Tesseract 非常适合用作后端,并可用于更具挑战性的 OCR 作业,例如布局分析。
  5. Tesseract 的一些不足之处:
    • 最新版本并未设计为可在 Windows 系统上编译。 Tesseract 的 C# API 封装器维护频率很低,而且比 Tesseract 的新版本落后数年。

要了解有关 C# 中 Tesseract 的更多信息,请访问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 版本。 然后创建一个OcrInput对象,使用AddImage方法加载图像文件。 IronTesseractRead方法处理图像并提取文本,然后将文本打印到控制台。

如何进行 OCR 文本识别,图 4:使用 IronOCR 库提取的文本输出 使用 IronOCR 库提取文本输出

2.3 铁氧浓度比 (IronOCR) 考量

  1. IronOCR 是 Tesseract 库的扩展,引入了更高的稳定性和更高的准确性。
  2. IronOCR 可以读取PDF和照片中的文本内容。 它还可以读取 20 多种不同类型的条形码和二维码。
  3. 输出可以呈现为纯文本、结构化数据、条形码或二维码。
  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 上玩游戏或重温《最后生还者》。