高级阅读的 OCR 配置
IronOCR提供先进的扫描阅读方法,如ReadPhoto,超越了标准OCR。 这些方法由IronOcr.Extensions.AdvancedScan包提供支持。 为了微调这些方法如何处理文本,IronOCR公开了TesseractConfiguration类,让开发者可以完全控制字符白名单、黑名单、条形码检测、数据表读取等。
这篇文章涵盖了用于高级阅读的TesseractConfiguration属性和在真实场景中配置OCR的实际示例。
快速入门:将 OCR 输出限制为字符白名单
在调用WhiteListCharacters。 任何不在白名单中的字符都会被静默地从结果中删除,从而消除噪声而无需任何后期处理。
-
使用 NuGet 包管理器安装 https://www.nuget.org/packages/IronOcr
-
复制并运行这段代码。
var result = new IronTesseract() { Configuration = new TesseractConfiguration { WhiteListCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789- " } }.Read(new OcrInput("image.png")); Console.WriteLine(result.Text); -
部署到您的生产环境中进行测试
通过免费试用立即在您的项目中开始使用IronOCR
如何配置 OCR 以实现高级阅读
- 从NuGet安装IronOCR
- 安装IronOCR包
- 配置
TesseractConfiguration属性,例如WhiteListCharacters和ReadBarCodes - 使用
OcrInput加载输入图像 - 使用
ReadPhoto、ReadLicensePlate或ReadPassport等高级方法读取图像。
Tesseract配置属性
TesseractConfiguration类提供了自定义OCR行为的以下属性。 这些通过IronTesseract.Configuration进行设置。
| 属性 | 翻译类型 | 说明 |
|---|---|---|
WhiteListCharacters |
string | OCR 输出仅识别此字符串中存在的字符,所有其他字符都将被排除。 |
BlackListCharacters |
string | 该字符串中的字符将被主动忽略并从 OCR 输出中删除。 |
ReadBarCodes |
bool | 在 OCR 处理过程中启用或禁用文档中的条形码检测。 |
ReadDataTables |
bool | 使用 Tesseract 启用或禁用文档中的表格结构检测。 |
PageSegmentationMode |
Tesseract页面分割模式 | 决定 Tesseract 如何分割输入图像。选项包括AutoOsd 、 Auto 、 SingleBlock 、 SingleLine 、 SingleWord等。 |
RenderSearchablePdf |
bool | 启用后,OCR 输出可以保存为带有不可见文本层的可搜索 PDF。 |
RenderHocr |
bool | 启用后,OCR 输出将包含 hOCR 数据,以便进一步处理或导出。 |
TesseractVariables |
Dictionary<string, object> | 提供对低级Tesseract配置变量的直接访问,以进行精细控制。 |
TesseractVariables字典更进一步,公开了数百个底层Tesseract引擎参数,以备高层属性不足时使用。
以下示例演示了每个属性组,首先是字符白名单。
设置车牌字符白名单
WhiteListCharacters的一个常见用例是将OCR输出限制为只能包含车牌上可能出现的字符:大写字母、数字、连字符和空格。 这样可以消除噪声,提高准确性,方法是告诉引擎忽略预期字符集之外的所有内容。
输入
以下车辆注册记录包含大写文本、小写文本、特殊符号(#``|, *),以及标点符号。
, and*`来补充白名单。
:path=/static-assets/ocr/content-code-examples/how-to/ocr-configurations-for-advanced-reading.cs
using IronOcr;
// Initialize the Tesseract OCR engine
IronTesseract ocr = new IronTesseract();
ocr.Configuration = new TesseractConfiguration
{
// Whitelist only characters that appear on license plates
WhiteListCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789- ",
// Blacklist common noise characters
BlackListCharacters = "`~@#$%&*",
};
var ocrInput = new OcrInput();
// Load the input image
ocrInput.LoadImage("advanced-input.png");
// Perform OCR on the input image with ReadPhoto method
var results = ocr.ReadPhoto(ocrInput);
// Print the filtered text result to the console
Console.WriteLine(results.Text);
Imports IronOcr
' Initialize the Tesseract OCR engine
Dim ocr As New IronTesseract()
ocr.Configuration = New TesseractConfiguration With {
' Whitelist only characters that appear on license plates
.WhiteListCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789- ",
' Blacklist common noise characters
.BlackListCharacters = "`~@#$%&*"
}
Dim ocrInput As New OcrInput()
' Load the input image
ocrInput.LoadImage("advanced-input.png")
' Perform OCR on the input image with ReadPhoto method
Dim results = ocr.ReadPhoto(ocrInput)
' Print the filtered text result to the console
Console.WriteLine(results.Text)
输出
白名单过滤的效果在结果中清晰可见:
- "车牌:ABC-1234"变为"P ABC-1234" 。 小写字母"late:"被省略,而车牌号码则完全保留。
- "VIN: 1HGBH41JXMN109186"变为"VIN 1HGBH41JXMN109186" 。 冒号被省略,但大写的车辆识别码和完整号码被保留。
- "Owner: john.doe@email.com"变为"O" 。 所有小写电子邮件地址和标点符号均被删除。
-地区:CA-90210 | "5区"变为"R CA-90210 Z 5" 。 管道(
|) and hash (#)被移除,而大写字母和数字保留。 - *"费用:$125.00 + 税"变为"F 12500"** 。 美元符号、小数点、Plus号和小写字母"tax"都被移除。
- "Ref: ~record_v2^final"变为"R 2" 。 波浪号(
^)和所有小写字符被剔除。
同样的BlackListCharacters方法适用于任何文档类型,不仅仅是车牌。 下一节将展示如何扩展读取操作,以便在同一次读取过程中检测条形码和表格结构。
条形码和数据表读取配置
IronOCR可以检测文档中的条形码、结构化表格以及文本。 这些特性通过TesseractConfiguration进行控制:
:path=/static-assets/ocr/content-code-examples/how-to/ocr-configurations-for-advanced-reading-3.cs
IronTesseract ocr = new IronTesseract();
ocr.Configuration = new TesseractConfiguration
{
// Enable barcode detection within documents
ReadBarCodes = true,
// Enable table structure detection
ReadDataTables = true,
};
Dim ocr As New IronTesseract()
ocr.Configuration = New TesseractConfiguration With {
.ReadBarCodes = True, ' Enable barcode detection within documents
.ReadDataTables = True ' Enable table structure detection
}
- ReadBarCodes:设置为
true时,IronOCR将扫描文档中的条形码和文本。 设置为false以跳过条形码检测并加快处理速度,适用于不期望条形码的情况。 - ReadDataTables:设置为
true时,Tesseract会尝试检测并保留文档中的表格结构。 这对于发票、报告和其他表格文档非常有用。
这些选项可以与BlackListCharacters结合使用,以精确控制从复杂文档中提取的内容。
过滤和检测控制着提取的内容,而布局解释则是另一个需要考虑的问题。 下一节介绍如何选择适合文档类型的PageSegmentationMode。
控制页面分段模式
PageSegmentationMode告诉Tesseract在识别前如何对输入图像进行分割。 为给定布局选择错误的模式会导致引擎误读或完全跳过文本。
| 模式 | 使用案例 |
|---|---|
AutoOsd |
自动版面分析,具备方向和脚本检测功能 |
Auto |
自动布局分析(不带 OSD)(默认) |
SingleColumn |
假设图像为单列文本。 |
SingleBlock |
假设图像是由单个均匀的文本块组成。 |
SingleLine |
假设图像是一行文字。 |
SparseText |
尽可能多地查找文本,顺序不限。 |
对于包含单行内容的标签或横幅,SingleLine消除多块分析并提高速度和准确性。
输入
SHIPPING LABEL: TRK-2024-XR9-001。
:path=/static-assets/ocr/content-code-examples/how-to/ocr-configurations-for-advanced-reading-4.cs
IronTesseract ocr = new IronTesseract();
ocr.Configuration = new TesseractConfiguration
{
PageSegmentationMode = TesseractPageSegmentationMode.SingleLine,
};
using OcrInput input = new OcrInput();
input.LoadImage("single-line-label.png");
OcrResult result = ocr.Read(input);
Console.WriteLine(result.Text);
Imports IronOcr
Dim ocr As New IronTesseract()
ocr.Configuration = New TesseractConfiguration With {
.PageSegmentationMode = TesseractPageSegmentationMode.SingleLine
}
Using input As New OcrInput()
input.LoadImage("single-line-label.png")
Dim result As OcrResult = ocr.Read(input)
Console.WriteLine(result.Text)
End Using
对于文本位置不规则的扫描页面,Auto恢复更多内容。
输入
receipt-scan.png是一个Corner Market热敏收据,包含四行项目(咖啡、松饼、果汁、麦片棒)、虚线分隔、小计、税金和总计。 在这种布局中,固定块分割会遗漏不同水平位置的条目。
:path=/static-assets/ocr/content-code-examples/how-to/ocr-configurations-for-advanced-reading-5.cs
IronTesseract ocr = new IronTesseract();
ocr.Configuration = new TesseractConfiguration
{
PageSegmentationMode = TesseractPageSegmentationMode.SparseText,
};
using OcrInput input = new OcrInput();
input.LoadImage("receipt-scan.png");
OcrResult result = ocr.Read(input);
Console.WriteLine(result.Text);
Imports IronTesseract
Dim ocr As New IronTesseract()
ocr.Configuration = New TesseractConfiguration With {
.PageSegmentationMode = TesseractPageSegmentationMode.SparseText
}
Using input As New OcrInput()
input.LoadImage("receipt-scan.png")
Dim result As OcrResult = ocr.Read(input)
Console.WriteLine(result.Text)
End Using
布局分割已根据文档类型进行了调整,下一步是控制下游处理的输出格式。
生成可搜索的 PDF 和 hOCR 输出
RenderHocr控制IronOCR生成的输出格式,以配合纯文本结果。
RenderSearchablePdf在原始图像上嵌入一层不可见的文本层,生成PDF,用户可以搜索和复制文本,而扫描图像仍可见。 这是文档归档工作流程的标准输出格式。
输入
scanned-document.pdf是一封单页的商业信函,来自IronOCR Solutions Ltd.(日期为2024年3月15日,参考号DOC-2024-OCR-0315)。 结果保存为searchable-output.pdf。
:path=/static-assets/ocr/content-code-examples/how-to/ocr-configurations-for-advanced-reading-6.cs
IronTesseract ocr = new IronTesseract();
ocr.Configuration = new TesseractConfiguration
{
RenderSearchablePdf = true,
};
using OcrInput input = new OcrInput();
input.LoadPdf("scanned-document.pdf");
OcrResult result = ocr.Read(input);
result.SaveAsSearchablePdf("searchable-output.pdf");
Imports IronTesseract
Dim ocr As New IronTesseract()
ocr.Configuration = New TesseractConfiguration With {
.RenderSearchablePdf = True
}
Using input As New OcrInput()
input.LoadPdf("scanned-document.pdf")
Dim result As OcrResult = ocr.Read(input)
result.SaveAsSearchablePdf("searchable-output.pdf")
End Using
输出
输出结果是一个 PDF 文件,它看起来与输入文件完全相同,但其中包含一个隐藏的文本层。 打开searchable-output.pdf并使用Ctrl+F验证嵌入的文本是否可搜索和复制。
RenderHocr生成一个hOCR文档,HTML文件将文本内容与每个单词的边框坐标一起编码。 当下游工具需要精确的词语定位时,例如编辑引擎或文档布局分析,这非常有用。
输入
document-page.png是一个文档页面,标题为"2024年第一季度总结",包括两个段落的财务数据,涵盖收入、运营成本和增长驱动因素。 结果保存为output.html。
:path=/static-assets/ocr/content-code-examples/how-to/ocr-configurations-for-advanced-reading-7.cs
IronTesseract ocr = new IronTesseract();
ocr.Configuration = new TesseractConfiguration
{
RenderHocr = true,
};
using OcrInput input = new OcrInput();
input.LoadImage("document-page.png");
OcrResult result = ocr.Read(input);
result.SaveAsHocrFile("output.html");
Imports IronTesseract
Dim ocr As New IronTesseract()
ocr.Configuration = New TesseractConfiguration With {
.RenderHocr = True
}
Using input As New OcrInput()
input.LoadImage("document-page.png")
Dim result As OcrResult = ocr.Read(input)
result.SaveAsHocrFile("output.html")
End Using
输出
output.html为每个识别的单词以及其边框坐标进行编码。 在浏览器中打开文件以检查 hOCR 结构,或将其传递给下游工具进行布局分析或编辑。
如果您需要从单个读取调用中获得所有三种输出格式(纯文本、可搜索 PDF 和 hOCR),则可以同时启用这两个标志。
这些输出标志与读取的语言无关,包括非拉丁字母。下一节将介绍如何对日语文本应用字符过滤。
国际文档的Unicode字符过滤
对于包含中文、日文或韩文的国际文档,BlackListCharacters属性适用于Unicode字符。 这样可以将输出限制为特定脚本,例如日语仅输出平假名和片假名。
输入
该文档包含标题 (テsuto)、混合平假名和片假名以及浊音变体 (プ、で) 的日语句子、包含列入黑名单的噪音符号 (★、■) 和汉字 (価格) 的价格线、包含另一个列入黑名单的符号 (§) 的备注行、更多汉字 (购入)、其他浊音标记变体 (プ、デ) 和基本片假名(メモ,ール)。 白名单仅允许平假名、片假名、数字和常用日语标点符号通过; 这三个噪声符号已被明确列入黑名单。
平假名和片假名的Unicode字符范围以字符串字面量传递至BlackListCharacters。
:path=/static-assets/ocr/content-code-examples/how-to/ocr-configurations-for-advanced-reading-jp.cs
using IronOcr;
using System.IO;
IronTesseract ocr = new IronTesseract();
ocr.Configuration = new TesseractConfiguration
{
// Whitelist only Hiragana, Katakana, numbers, and common Japanese punctuation
WhiteListCharacters = "あいうえおかきくけこさしすせそたちつてとなにぬねのはひふへほまみむめもやゆよらりるれろわをん" +
"アイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤユヨラリルレロワヲン" +
"0123456789、。?!()¥ー",
// Blacklist common noise/symbols you want to ignore
BlackListCharacters = "★■§",
};
var ocrInput = new OcrInput();
// Load Japanese input image
ocrInput.LoadImage("jp.png");
// Perform OCR on the input image with ReadPhoto method
var results = ocr.ReadPhoto(ocrInput);
// Write the text result directly to a file named "output.txt"
File.WriteAllText("output.txt", results.Text);
// You can add this line to confirm the file was saved:
Console.WriteLine("OCR results saved to output.txt");
Imports IronOcr
Imports System.IO
Dim ocr As New IronTesseract()
ocr.Configuration = New TesseractConfiguration With {
.WhiteListCharacters = "あいうえおかきくけこさしすせそたちつてとなにぬねのはひふへほまみむめもやゆよらりるれろわをん" &
"アイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤユヨラリルレロワヲン" &
"0123456789、。?!()¥ー",
.BlackListCharacters = "★■§"
}
Dim ocrInput As New OcrInput()
' Load Japanese input image
ocrInput.LoadImage("jp.png")
' Perform OCR on the input image with ReadPhoto method
Dim results = ocr.ReadPhoto(ocrInput)
' Write the text result directly to a file named "output.txt"
File.WriteAllText("output.txt", results.Text)
' You can add this line to confirm the file was saved:
Console.WriteLine("OCR results saved to output.txt")
输出
完整的过滤输出以文本文件的形式提供: jp-output.txt 。
因为白名单只包含基本的平假名和片假名字符,所以像プ(pu)和デ(de)这样的派生声标变体被删除了。 像価格(价格)和購入(购买)这样的汉字也被排除在外,因为它们不在白名单字符集中。 黑名单符号如§被主动移除,无论白名单如何。
接下来我该去哪里?
现在您已经了解如何配置IronOCR以适应高级阅读场景,请探索以下内容:
- 读取特定类型的文件,例如护照和车牌 -条形码和二维码读取作为独立的 OCR 用例
- 从处理结果中导出 hOCR 和可搜索的 PDF 文件
如需用于生产,请务必获取许可证以去除水印并使用全部功能。
常见问题解答
IronOCR 中的 TesseractConfiguration 是什么?
IronOCR 中的 TesseractConfiguration 允许用户自定义 OCR 设置,从而实现字符白名单、BarCode 识别和多语言支持等高级识别功能。
如何在 IronOCR 中设置字符白名单?
在 IronOCR 中,您可以通过 TesseractConfiguration 设置字符白名单,从而指定 OCR 引擎应识别的字符,这对于读取车牌号等任务非常有用。
IronOCR 能读取 BarCode 和数据表吗?
是的,IronOCR 可以通过调整 TesseractConfiguration 属性中的特定设置,配置为读取 BarCode 和数据表,从而实现精确的 OCR 数据提取。
IronOCR 是否支持中文、日语和韩语等国际语言?
IronOCR 通过其多语言 TesseractConfiguration 选项支持多种国际语言,包括中文、日语和韩语。
在 IronOCR 中使用高级 OCR 配置有哪些优势?
利用 IronOCR 中的高级 OCR 配置,可实现更准确、更高效的文本识别,并支持特定语言的文本识别和结构化数据提取等专业任务。
能否针对特定的 OCR 任务对 IronOCR 进行优化?
是的,IronOCR 可以通过配置字符白名单、启用 BARCODE 或表格识别等设置,针对特定的 OCR 任务进行优化,从而提升针对特定应用的性能。
如何在 IronOCR 中启用多语言支持?
要在 IronOCR 中启用多语言支持,您可以在 TesseractConfiguration 中调整语言设置,从而使 OCR 引擎能够识别多种语言的文本。
什么是字符白名单,它们在 IronOCR 中如何使用?
IronOCR 中的字符白名单是 OCR 引擎配置为识别的特定字符列表,非常适合读取数字或特定文本模式等针对性任务。
IronOCR 能否用于读取结构化数据格式?
是的,IronOCR 可以配置为读取和处理 BARCODE 和表格等结构化数据格式,为各种数据提取需求提供多功能的 OCR 能力。
IronOCR 提供了哪些用于高级文本识别的配置选项?
IronOCR 提供字符白名单、多语言支持和 BarCode 识别等配置选项,以增强高级文本识别能力,满足特定需求。

