在 C# 和 VB.NET 中开始使用 OCR

This article was translated from English: Does it need improvement?
Translated
View the article in English

IronOCR 是一个 C# 软件库,允许 .NET 平台软件开发人员从图像和 PDF 文档中识别和读取文本。 它是一个纯粹的 .NET OCR 库,使用了目前已知的最先进的 Tesseract 引擎。

安装

使用 NuGet 软件包管理器安装

在 Visual Studio 中安装 IronOcr,或使用 NuGet 包管理器在命令行中安装。在 Visual Studio 中,用以下命令导航到控制台

  • 工具 ->
  • NuGet 软件包管理器 ->

  • 软件包管理器控制台
Install-Package IronOcr

并查看 NuGet 上的 IronOcr 了解有关版本更新和安装的更多信息。

还有其他IronOCR NuGet 软件包可用于不同平台:

下载 IronOCR .ZIP

您也可以选择通过 .ZIP 文件下载 IronOCR。点击 直接下载 DLL.下载 .zip 文件后:

.NET Framework 4.0+ 安装说明:

  • 在项目中包含 net40 文件夹中的 IronOcr.dll
  • 然后将程序集引用添加到

    • 系统配置

    • 系统.绘图

    • 系统.Web

关于 .NET Standard 和 .NET Core 2.0+ 以及 .NET 5 的说明

  • 在项目中包含 netstandard2.0 文件夹中的 IronOcr.dll
  • 然后添加一个 NuGet 包引用:

    * System.Drawing.Common 4.7 或更高版本

下载 IronOCR 安装程序 (仅限 Windows)

另一种方法是下载我们的 IronOCR 安装程序,该程序将安装 IronOCR 开箱即用所需的全部资源。请注意,此选项仅适用于 Windows 系统。要下载安装程序,请 点击这里.下载 .zip 文件后:

.NET Framework 4.0+ 安装说明:

  • 在项目中包含 net40 文件夹中的 IronOcr.dll
  • 然后将程序集引用添加到

    • 系统配置

    • 系统.绘图

    • 系统.Web

关于 .NET Standard 和 .NET Core 2.0+ 以及 .NET 5 的说明

  • 在项目中包含 netstandard2.0 文件夹中的 IronOcr.dll
  • 然后添加一个 NuGet 包引用:

    * System.Drawing.Common 4.7 或更高版本

为什么选择 IronOCR?

IronOCR 是一个易于安装、功能完备、文档齐全的 .NET 软件库。

选择 IronOCR,无需使用任何外部网络服务,无需持续付费,也无需通过互联网发送机密文件,即可实现99.8%以上的 OCR 精确度

为什么 C# 开发人员选择 IronOCR 而不是 Vanilla Tesseract?

  • 以单个 DLL 或 NuGet 的形式安装
  • 包括开箱即用的 Tesseract 5、4 和 3 引擎。
  • 精确度 99.8% 明显优于普通 Tesseract。
  • 超快速度和多线程处理
  • 兼容 MVC、WebApp、桌面、控制台和服务器应用程序
  • 无需使用 Exes 或 C++ 代码
  • 完全支持 PDF OCR
  • 可对几乎所有图像文件或 PDF 执行 OCR 识别
  • 完全支持 .NET Core、标准和 FrameWork
  • 可在 Windows、Mac、Linux、Azure、Docker、Lambda 和 AWS 上部署
  • 读取条形码和 QR 码
  • 将 OCR 输出为 XHTML
  • 将 OCR 导出为可搜索的 PDF 文档
  • 支持多线程
  • 125 种国际语言,全部通过 NuGet 或 OcrData 文件管理
  • 提取图像、坐标、统计数据和字体。 不仅仅是文本。

  • 可用于在商业和专有应用程序中重新发布 Tesseract OCR。

*IronOCR 在处理现实世界中的图像和不完美的文档(如照片或可能存在数字噪声或瑕疵的低分辨率扫描件)时表现出色。

其他 免费 OCR .NET平台的库,如其他.NET Tesseract API和网络服务,在这些实际使用案例中的表现并不理想。

OCR with Tesseract 5 - Start Coding in C#

下面的代码示例显示了使用 C# 或 VB .NET 从图像中读取文本是多么容易。

OneLiner

:path=/static-assets/ocr/content-code-examples/get-started/get-started-1.cs
string Text = new IronTesseract().Read(@"img\Screenshot.png").Text;
Dim Text As String = (New IronTesseract()).Read("img\Screenshot.png").Text
VB   C#

可配置的 Hello World

:path=/static-assets/ocr/content-code-examples/get-started/get-started-2.cs
using IronOcr;

IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();

// Add multiple images
input.LoadImage("images/sample.jpeg");

OcrResult result = ocr.Read(input);
Console.WriteLine(result.Text);
Imports IronOcr

Private ocr As New IronTesseract()
Private OcrInput As using

' Add multiple images
input.LoadImage("images/sample.jpeg")

Dim result As OcrResult = ocr.Read(input)
Console.WriteLine(result.Text)
VB   C#

C## PDF OCR

同样的方法也可用于从任何 PDF 文档中提取文本。

:path=/static-assets/ocr/content-code-examples/get-started/get-started-3.cs
using IronOcr;

IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();

// We can also select specific PDF page numbers to OCR
input.LoadPdf("example.pdf", Password: "password");

OcrResult result = ocr.Read(input);
Console.WriteLine(result.Text);

// 1 page for every page of the PDF
Console.WriteLine($"{result.Pages.Length} Pages");
Imports IronOcr

Private ocr As New IronTesseract()
Private OcrInput As using

' We can also select specific PDF page numbers to OCR
input.LoadPdf("example.pdf", Password:= "password")

Dim result As OcrResult = ocr.Read(input)
Console.WriteLine(result.Text)

' 1 page for every page of the PDF
Console.WriteLine($"{result.Pages.Length} Pages")
VB   C#

多页 TIFF 文件的 OCR 识别

:path=/static-assets/ocr/content-code-examples/get-started/get-started-4.cs
using IronOcr;

IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();
var pageindices = new int[] { 1, 2 };
input.LoadImageFrames("multi-frame.tiff", pageindices);
OcrResult result = ocr.Read(input);
Console.WriteLine(result.Text);
Imports IronOcr

Private ocr As New IronTesseract()
Private OcrInput As using
Private pageindices = New Integer() { 1, 2 }
input.LoadImageFrames("multi-frame.tiff", pageindices)
Dim result As OcrResult = ocr.Read(input)
Console.WriteLine(result.Text)
VB   C#

条形码和 QR

IronOCR 的一个独特功能是,它可以在扫描文本的同时读取文档中的条形码和 QR 码。OcrResult.OcrBarcode` 类的实例可为开发人员提供每个扫描条形码的详细信息。

:path=/static-assets/ocr/content-code-examples/get-started/get-started-5.cs
using IronOcr;

IronTesseract ocr = new IronTesseract();
ocr.Configuration.ReadBarCodes = true;

using OcrInput input = new OcrInput();
input.LoadImage("img/Barcode.png");

OcrResult Result = ocr.Read(input);
foreach (var Barcode in Result.Barcodes)
{
    // type and location properties also exposed
    Console.WriteLine(Barcode.Value);
}
Imports IronOcr

Private ocr As New IronTesseract()
ocr.Configuration.ReadBarCodes = True

Using input As New OcrInput()
	input.LoadImage("img/Barcode.png")
	
	Dim Result As OcrResult = ocr.Read(input)
	For Each Barcode In Result.Barcodes
		' type and location properties also exposed
		Console.WriteLine(Barcode.Value)
	Next Barcode
End Using
VB   C#

对图像的特定区域进行 OCR 识别

IronOCR&##39 的所有扫描和读取方法都能准确指定我们希望从页面的哪一部分或哪几页读取文本。这在我们查看标准化表格时非常有用,可以节省大量时间并提高效率。

要使用裁剪区域,我们需要向 System.Drawing 添加系统引用,以便使用 System.Drawing.Rectangle 对象。

:path=/static-assets/ocr/content-code-examples/get-started/get-started-6.cs
using IronOcr;

IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();

// Dimensions are in pixel
var contentArea = new System.Drawing.Rectangle() { X = 215, Y = 1250, Height = 280, Width = 1335 };

input.LoadImage("document.png", contentArea);

OcrResult result = ocr.Read(input);
Console.WriteLine(result.Text);
Imports IronOcr

Private ocr As New IronTesseract()
Private OcrInput As using

' Dimensions are in pixel
Private contentArea = New System.Drawing.Rectangle() With {
	.X = 215,
	.Y = 1250,
	.Height = 280,
	.Width = 1335
}

input.LoadImage("document.png", contentArea)

Dim result As OcrResult = ocr.Read(input)
Console.WriteLine(result.Text)
VB   C#

低质量扫描的 OCR

IronOCR OcrInput 类可以修复一般魔方无法读取的扫描结果。

:path=/static-assets/ocr/content-code-examples/get-started/get-started-7.cs
using IronOcr;

IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();
var pageindices = new int[] { 1, 2 };
input.LoadImageFrames(@"img\Potter.tiff", pageindices);

// fixes digital noise and poor scanning
input.DeNoise();

// fixes rotation and perspective
input.Deskew();

OcrResult result = ocr.Read(input);
Console.WriteLine(result.Text);
Imports IronOcr

Private ocr As New IronTesseract()
Private OcrInput As using
Private pageindices = New Integer() { 1, 2 }
input.LoadImageFrames("img\Potter.tiff", pageindices)

' fixes digital noise and poor scanning
input.DeNoise()

' fixes rotation and perspective
input.Deskew()

Dim result As OcrResult = ocr.Read(input)
Console.WriteLine(result.Text)
VB   C#

将 OCR 结果导出为可搜索的 PDF 文件

:path=/static-assets/ocr/content-code-examples/get-started/get-started-8.cs
using IronOcr;

IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();
input.Title = "Quarterly Report";
input.LoadImage("image1.jpeg");
input.LoadImage("image2.png");
var pageindices = new int[] { 1, 2 };
input.LoadImageFrames("image3.gif", pageindices);

OcrResult result = ocr.Read(input);
result.SaveAsSearchablePdf("searchable.pdf");
Imports IronOcr

Private ocr As New IronTesseract()
Private OcrInput As using
input.Title = "Quarterly Report"
input.LoadImage("image1.jpeg")
input.LoadImage("image2.png")
Dim pageindices = New Integer() { 1, 2 }
input.LoadImageFrames("image3.gif", pageindices)

Dim result As OcrResult = ocr.Read(input)
result.SaveAsSearchablePdf("searchable.pdf")
VB   C#

TIFF 到可搜索 PDF 的转换

:path=/static-assets/ocr/content-code-examples/get-started/get-started-9.cs
using IronOcr;

IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();
var pageindices = new int[] { 1, 2 };
input.LoadImageFrames("example.tiff", pageindices);
ocr.Read(input).SaveAsSearchablePdf("searchable.pdf");
Imports IronOcr

Private ocr As New IronTesseract()
Private OcrInput As using
Private pageindices = New Integer() { 1, 2 }
input.LoadImageFrames("example.tiff", pageindices)
ocr.Read(input).SaveAsSearchablePdf("searchable.pdf")
VB   C#

将 OCR 结果导出为 HTML

:path=/static-assets/ocr/content-code-examples/get-started/get-started-10.cs
using IronOcr;

IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();
input.Title = "Html Title";
input.LoadImage("image1.jpeg");

OcrResult Result = ocr.Read(input);
Result.SaveAsHocrFile("results.html");
Imports IronOcr

Private ocr As New IronTesseract()
Private OcrInput As using
input.Title = "Html Title"
input.LoadImage("image1.jpeg")

Dim Result As OcrResult = ocr.Read(input)
Result.SaveAsHocrFile("results.html")
VB   C#

OCR 图像增强过滤器

IronOCR 为 OcrInput 对象提供独特的过滤器,以提高 OCR 性能。

图像增强代码示例

:path=/static-assets/ocr/content-code-examples/get-started/get-started-11.cs
using IronOcr;

IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();
input.LoadImage("LowQuality.jpeg");

// fixes digital noise and poor scanning
input.DeNoise();

// fixes rotation and perspective
input.Deskew();

OcrResult result = ocr.Read(input);
Console.WriteLine(result.Text);
Imports IronOcr

Private ocr As New IronTesseract()
Private OcrInput As using
input.LoadImage("LowQuality.jpeg")

' fixes digital noise and poor scanning
input.DeNoise()

' fixes rotation and perspective
input.Deskew()

Dim result As OcrResult = ocr.Read(input)
Console.WriteLine(result.Text)
VB   C#

OCR 图像过滤器列表

IronOCR 内置的用于提高 OCR 性能的输入过滤器包括

  • OcrInput.Rotate( 双学位) - 顺时针旋转图像若干度。 如需逆时针旋转,请使用负数。
  • OcrInput.Binarize() - 该图像滤镜可将每个像素变成黑色或白色,没有中间色。可在文字与背景对比度非常低的情况下提高 OCR 性能。
  • OcrInput.ToGrayScale() - 该图像滤镜可将每个像素转化为灰度阴影。 不太可能提高 OCR 的准确性,但可以提高速度
  • OcrInput.Contrast() 自动增加对比度。 该过滤器通常可以提高低对比度扫描的 OCR 速度和准确性。
  • OcrInput.DeNoise() - 消除数字噪声。 该滤波器只能用于预计会产生噪音的地方。
  • OcrInput.Invert() - 倒置每种颜色。例如:白变黑:黑变白。
  • OcrInput.Dilate() 高级形态学。 Dilation 为图像中的物体边界添加像素。与 Erode 相反
  • OcrInput.Erode() 高级形态学。 侵蚀 删除对象边界上的像素与 "放大 "相反
  • OcrInput.Deskew() - 旋转图像,使其方向正确且正交。这对 OCR 非常有用,因为 Tesseract 对倾斜扫描的容忍度可低至 5 度。
  • OcrInput.EnhanceResolution - 增强低质量图像的分辨率。 由于 OcrInput.MinimumDPIOcrInput.TargetDPI 会自动捕捉并解析低分辨率输入,因此通常不需要此过滤器。
  • EnhanceResolution 是一个能自动检测低分辨率图像的设置。 (低于 275 dpi) OCR 库会自动放大图像,然后锐化所有文字,以便 OCR 库能完美读取。虽然这一操作本身比较耗时,但通常可以缩短对图像进行 OCR 操作的总体时间。
  • 语言 IronOCR 支持 22 种国际语言包,语言设置可用于选择一种或多种语言,以应用于 OCR 操作。
  • **策略*** IronOCR 支持两种策略。我们可以选择对文档进行快速而不太准确的扫描,或者使用高级策略,即通过查看句子中单词之间的统计关系,使用一些人工智能模型来自动提高 OCR 文本的准确性。
  • 颜色空间是一个设置,我们可以选择灰度或彩色 OCR。一般来说,灰度是最佳选择。不过,有时当文本或背景的色调相似但颜色差异很大时,全彩色空间会提供更好的结果。
  • DetectWhiteTextOnDarkBackgrounds.一般来说,所有 OCR 库都希望在白色背景上看到黑色文本。此设置允许 IronOCR 自动检测底片或带有白色文本的深色页面,并读取它们。
  • InputImageType. 此项设置允许开发人员指导 OCR 库是查看完整文档还是截图等片段。
  • RotateAndStraighten 是一项高级设置,它使 IronOCR 具备独特的能力,不仅可以读取旋转过的文档,还可以读取包含透视图的文档,例如文本文档的照片。
  • **阅读条形码***是一项有用的功能,它允许 IronOCR 在阅读文本时自动阅读页面上的条形码和二维码,而不会增加大量额外的时间负担。

  • ColorDepth. 该设置决定 OCR 库将使用每个像素多少位来确定颜色的深度。较高的颜色深度可以提高 OCR 质量,但也会增加 OCR 操作完成所需的时间。

125 个语言包

IronOCR 通过以 DLL 形式发布的语言包支持 125 种国际语言,这些语言包可以 从本网站下载或也可从 NuGet 软件包管理器.

语言包括德语、法语、英语、中文、日语等。 专业语言包包括护照 MRZ、MICR 支票、金融数据、车牌等。 您还可以使用任何 tesseract".traineddata "文件,包括您自己创建的文件。

语言示例

:path=/static-assets/ocr/content-code-examples/get-started/get-started-12.cs
using IronOcr;

// PM> Install IronOcr.Languages.Arabic
IronTesseract ocr = new IronTesseract();
ocr.Language = OcrLanguage.Arabic;

using OcrInput input = new OcrInput();

var pageindices = new int[] { 1, 2 };
input.LoadImageFrames("img/arabic.gif", pageindices);
// Add image filters if needed
// In this case, even thought input is very low quality
// IronTesseract can read what conventional Tesseract cannot.
OcrResult result = ocr.Read(input);
// Console can't print Arabic on Windows easily.
// Let's save to disk instead.
result.SaveAsTextFile("arabic.txt");
Imports IronOcr

' PM> Install IronOcr.Languages.Arabic
Private ocr As New IronTesseract()
ocr.Language = OcrLanguage.Arabic

Using input As New OcrInput()
	
	Dim pageindices = New Integer() { 1, 2 }
	input.LoadImageFrames("img/arabic.gif", pageindices)
	' Add image filters if needed
	' In this case, even thought input is very low quality
	' IronTesseract can read what conventional Tesseract cannot.
	Dim result As OcrResult = ocr.Read(input)
	' Console can't print Arabic on Windows easily.
	' Let's save to disk instead.
	result.SaveAsTextFile("arabic.txt")
End Using
VB   C#

多种语言示例

还可以同时使用多种语言进行 OCR 识别。 这确实有助于获取 Unicode 文档中的英语元数据和网址。

:path=/static-assets/ocr/content-code-examples/get-started/get-started-13.cs
using IronOcr;

// PM> Install IronOcr.Languages.ChineseSimplified
IronTesseract ocr = new IronTesseract();
ocr.Language = OcrLanguage.ChineseSimplified;

// We can add any number of languages
ocr.AddSecondaryLanguage(OcrLanguage.English);

using OcrInput input = new OcrInput();
input.LoadPdf("multi-language.pdf");
OcrResult result = ocr.Read(input);
result.SaveAsTextFile("results.txt");
Imports IronOcr

' PM> Install IronOcr.Languages.ChineseSimplified
Private ocr As New IronTesseract()
ocr.Language = OcrLanguage.ChineseSimplified

' We can add any number of languages
ocr.AddSecondaryLanguage(OcrLanguage.English)

Using input As New OcrInput()
	input.LoadPdf("multi-language.pdf")
	Dim result As OcrResult = ocr.Read(input)
	result.SaveAsTextFile("results.txt")
End Using
VB   C#

详细 OCR 结果对象

IronOCR 会为每次 OCR 操作返回一个 OCR 结果对象。一般来说,开发人员只使用该对象的文本属性来获取从图像中扫描出的文本。然而,OCR 结果 DOM 要比这先进得多。

:path=/static-assets/ocr/content-code-examples/get-started/get-started-14.cs
using IronOcr;

IronTesseract ocr = new IronTesseract();

// Must be set to true to read barcode
ocr.Configuration.ReadBarCodes = true;
using OcrInput input = new OcrInput();
var pageindices = new int[] { 1, 2 };
input.LoadImageFrames(@"img\sample.tiff", pageindices);

OcrResult result = ocr.Read(input);
var pages = result.Pages;
var words = pages[0].Words;
var barcodes = result.Barcodes;
// Explore here to find a massive, detailed API:
// - Pages, Blocks, Paraphaphs, Lines, Words, Chars
// - Image Export, Fonts Coordinates, Statistical Data, Tables
Imports IronOcr

Private ocr As New IronTesseract()

' Must be set to true to read barcode
ocr.Configuration.ReadBarCodes = True
Using input As New OcrInput()
	Dim pageindices = New Integer() { 1, 2 }
	input.LoadImageFrames("img\sample.tiff", pageindices)
	
	Dim result As OcrResult = ocr.Read(input)
	Dim pages = result.Pages
	Dim words = pages(0).Words
	Dim barcodes = result.Barcodes
	' Explore here to find a massive, detailed API:
	' - Pages, Blocks, Paraphaphs, Lines, Words, Chars
	' - Image Export, Fonts Coordinates, Statistical Data, Tables
End Using
VB   C#

性能

IronOCR 开箱即用,无需调整性能或大量修改输入图像。

速度惊人:与之前的版本相比,IronOcr.2020 + 的速度提高了 10 倍,错误率降低了 250%。

了解更多

要了解有关 C#、VB、F# 或其他 .NET 语言中 OCR 的更多信息,请 阅读我们的社区教程中提供了如何使用 IronOCR 的实际例子,并展示了如何充分利用该库的细微差别。

完整的 .NET开发人员的应用程序接口参考 也可以使用。