从 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包管理器安装

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

  • 工具 ->
  • NuGet 包管理器 ->
  • 软件包管理器控制台
Install-Package IronOcr

查看NuGet上的IronOCR以获取有关版本更新和安装的更多信息。

针对不同平台,还有其他IronOCR NuGet 包可用:

下载 IronOCR .ZIP

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

.NET Framework 4.0+ 安装说明:

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

    • System.Configuration

    • System.Drawing

    • System.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 包含到您的项目中。
  • 然后添加程序集引用到:

    • System.Configuration

    • System.Drawing

    • System.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、桌面、控制台和服务器应用程序兼容
  • 无需 Exe 文件或 C++ 代码即可使用
  • 完整的PDF OCR支持
  • 要对几乎任何图片文件或PDF执行OCR
  • 完整的 .NET Core、Standard 和 Framework 支持
  • 在 Windows、Mac、Linux、Azure、Docker、Lambda、AWS 上部署
  • 读取条形码和二维码
  • 将 OCR 导出为 XHTML
  • 将 OCR 导出为可搜索的 PDF 文档
  • 多线程支持
  • 125种国际语言,均通过NuGet或OcrData文件管理。
  • 提取图像、坐标、统计数据和字体。 不仅仅是文本。
  • 可用于在商业和专有应用程序内重新分发Tesseract OCR。

    IronOCR 在处理真实世界图像和不完美文档时表现出色,例如照片或可能具有数字噪声或缺陷的低分辨率扫描。

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

    使用 Tesseract 5 进行 OCR - 在 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
$vbLabelText   $csharpLabel

可配置的 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)
$vbLabelText   $csharpLabel

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")
$vbLabelText   $csharpLabel

多页 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)
$vbLabelText   $csharpLabel

条形码和QR代码

IronOCR的一个独特功能是它在扫描文本的同时可以识别文档中的条形码和二维码。 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
$vbLabelText   $csharpLabel

图像特定区域的OCR处理

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

要使用裁剪区域,我们需要添加对 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)
$vbLabelText   $csharpLabel

低质量扫描的OCR

IronOCR OcrInput 类可以修复普通 Tesseract 无法读取的扫描件。

: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)
$vbLabelText   $csharpLabel

将 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")
$vbLabelText   $csharpLabel

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")
$vbLabelText   $csharpLabel

将 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")
$vbLabelText   $csharpLabel

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)
$vbLabelText   $csharpLabel

OCR 图像过滤器列表

用于提高IronOCR性能的输入过滤器包括:

  • OcrInput.Rotate( double degrees) - 将图像顺时针旋转指定角度。 要逆时针旋转,请使用负数。
  • OcrInput.Binarize() - 此图像滤镜将每个像素变为纯黑或纯白,没有中间色调。 可能提高OCR性能,用于文本与背景对比度非常低的情况。
  • OcrInput.ToGrayScale() - 此图像滤镜将每个像素转换为灰度。 不太可能提高OCR精度,但可以提高速度。
  • OcrInput.Contrast() - 自动增加对比度。 此过滤器通常能提高低对比度扫描中的OCR速度和准确性。
  • OcrInput.DeNoise() - 去除数字噪声。该滤镜只应在预期有噪声的情况下使用。
  • OcrInput.Invert() - 反转每种颜色。 例如,白变黑:黑变白。
  • OcrInput.Dilate() - 高级形态学。 扩张在图像中将像素添加到对象的边界。 侵蚀的反义词
  • OcrInput.Erode() - 高级形态学。 侵蚀 去除物体边界上的像素,与膨胀相反
  • OcrInput.Deskew() - 将图像旋转至正立并垂直。 这对OCR非常有用,因为Tesseract对倾斜扫描的容忍度可以低至5度。
  • OcrInput.EnhanceResolution - 增强低质量图像的分辨率。 此滤波器通常不需要,因为OcrInput.MinimumDPIOcrInput.TargetDPI将自动捕获并解决低分辨率输入。
  • EnhanceResolution 是一种设置,它将自动检测低分辨率图像(低于 275 dpi),并自动放大图像,然后锐化所有文本,以便 OCR 库能够完美地读取。 尽管这个操作本身是耗时的,但它通常会缩短对图像进行OCR操作的总时间。
  • 语言 IronOCR 支持 22 个国际语言包,可以使用语言设置选择一种或多种语言应用于 OCR 操作。
  • 策略 IronOCR 支持两种策略。 我们可以选择进行快速且不太准确的文档扫描,或者采用高级策略,使用一些人工智能模型通过观察句子中单词之间的统计关系来自动提高OCR文本的准确性。
  • ColorSpace 是一个设置,我们可以选择在灰度或彩色下进行 OCR。 通常,灰度是最佳选择。 然而,有时当文本或背景具有相似的色调但非常不同的颜色时,全彩色空间将提供更好的结果。
  • DetectWhiteTextOnDarkBackgrounds。 通常,所有的OCR库都希望看到白色背景上的黑色文本。 此设置允许IronOCR自动检测负片或带有白色文字的深色页面,并进行识别。
  • InputImageType。 此设置允许开发人员指导OCR库分析完整文档或片段(例如屏幕截图)。
  • RotateAndStraighten 是一个高级设置,它使 IronOCR 具备独特的能力,能够读取不仅旋转的文件,还能读取可能包含透视的文件,例如文本文件的照片。
  • ReadBarcodes 是一个有用的功能,使 IronOCR 能够在读取文本的同时自动读取页面上的条形码和 QR 码,而不会增加大量额外的时间负担。
  • ColorDepth。 此设置决定了OCR库将使用多少比特每像素来确定颜色的深度。 更高的色深可能会提高OCR的质量,但也会延长OCR操作的完成时间。

125 语言包

IronOCR通过语言包支持125种国际语言,这些语言包作为DLL分发,可以从本网站下载,也可以从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
$vbLabelText   $csharpLabel

多种语言示例

也可以同时使用多种语言进行光学字符识别。这真的可以帮助获取Unicode文档中的英语元数据和URL。

: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
$vbLabelText   $csharpLabel

详细的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
$vbLabelText   $csharpLabel

性能

IronOCR 无需进行性能调优或大幅修改输入图像即可开箱即用。

速度惊人:IronOcr.2020 + 的速度比以前的版本快达10倍,错误率减少了超过250%。

了解更多

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

完整的.NET开发人员API参考也可用。