在 C# 中使用快速 OCR 配置提高性能
IronOCR 的快速配置使用 EnglishFast 语言模式并关闭条码读取等不必要的功能,加快 OCR 处理,对准确性的影响很小。如下文对 10 张图像的基准测试所示,处理耗时缩短约 17%。这种优化适合时限严格的大批量处理。
IronOCR 开箱即用。如果更注重速度,可以使用 IronOCR 的快速配置。与标准 OCR 配置相比,快速配置可显著提高扫描速度,对准确性的影响很小。
本文演示了如何使用快速配置,并比较 IronOCR 的快速配置和标准配置在基准测试中的表现。 无论您是在处理已扫描文档、PDF还是图像,这些优化措施都能显著提高您应用程序的性能。
如何设置 OCR 快速配置
- 通过NuGet安装OCR库以设置
IronOcr - 初始化 OCR 引擎
- 将
Language设置为EnglishFast - 将
ReadBarCodes属性设置为false - 加载图像并提取文本
快速配置的关键是 Language 属性。将 Language 属性设为 OcrLanguage.EnglishFast 可提高识别速度,准确性可能略有下降。这样,IronOCR 可以更快地批量识别文本,尤其适合时限严格的关键业务应用。
使用快速语言模式时,还可以关闭 ReadBarCodes 等不必要的功能以进一步提速。让 IronOCR 自动检测页面分割方式,即可简化配置。有关高级选项,请参阅 Tesseract 详细配置指南。
下面的代码示例处理了以下输入图像:
我应该使用什么输入格式?

快速配置需要什么代码?
-
1使用 IronOCR 安装 NuGet Package Manager
-
2复制并运行这段代码。
/* :path=/static-assets/ocr/content-code-examples/how-to/ocr-fast-configuration.cs */ using IronOcr; using System; var ocrTesseract = new IronTesseract(); // Fast Dictionary ocrTesseract.Language = OcrLanguage.EnglishFast; // Turn off unneeded options ocrTesseract.Configuration.ReadBarCodes = false; // Assume text is laid out neatly in an orthogonal document ocrTesseract.Configuration.PageSegmentationMode = TesseractPageSegmentationMode.Auto; using var ocrInput = new OcrInput(); ocrInput.LoadImage("image.png"); var ocrResult = ocrTesseract.Read(ocrInput); Console.WriteLine(ocrResult.Text);Imports IronOcr Imports System Dim ocrTesseract = New IronTesseract() ' Fast Dictionary ocrTesseract.Language = OcrLanguage.EnglishFast ' Turn off unneeded options ocrTesseract.Configuration.ReadBarCodes = False ' Assume text is laid out neatly in an orthogonal document ocrTesseract.Configuration.PageSegmentationMode = TesseractPageSegmentationMode.Auto Using ocrInput As New OcrInput() ocrInput.LoadImage("image.png") Dim ocrResult = ocrTesseract.Read(ocrInput) Console.WriteLine(ocrResult.Text) End UsingImports IronOcr Imports System Dim ocrTesseract = New IronTesseract() ' Fast Dictionary ocrTesseract.Language = OcrLanguage.EnglishFast ' Turn off unneeded options ocrTesseract.Configuration.ReadBarCodes = False ' Assume text is laid out neatly in an orthogonal document ocrTesseract.Configuration.PageSegmentationMode = TesseractPageSegmentationMode.Auto Using ocrInput As New OcrInput() ocrInput.LoadImage("image.png") Dim ocrResult = ocrTesseract.Read(ocrInput) Console.WriteLine(ocrResult.Text) End Using -
3部署到您的生产环境中进行测试
今天就用免费试用在您的项目中开始使用IronOCR
输出结果是什么?

这是从上方图像中提取的文本。OCR 引擎准确识别了原文,同时保留了原有格式和结构。对此类清晰、对比度高的文本,快速配置可获得良好的识别效果。
快速配置与标准配置相比如何?
为衡量实际效果,我们对标准配置和快速配置进行了基准测试。测试使用 10 张样本图像,每张包含数段文字,用于比较两种配置的性能,并展示快速配置在速度和识别准确性之间的取舍。
对于标准配置,我们使用默认设置初始化 IronTesseract,不启用任何用于提速的属性。 这种基准方法类似于我们的性能跟踪指南,展示了如何实时监控OCR操作。
以下是我们用于测试的输入样本。这些图像代表了在处理多页文档或批量操作时可能遇到的典型文档场景。
我如何运行基准测试?
using IronOcr;
using System;
using System.Diagnostics;
using System.IO;
// --- Tesseract Engine Setup ---
var ocrTesseract = new IronTesseract();
ocrTesseract.Language = OcrLanguage.EnglishFast;
ocrTesseract.Configuration.ReadBarCodes = false;
ocrTesseract.Configuration.PageSegmentationMode = TesseractPageSegmentationMode.Auto;
// --- 1. Define folder and get files ---
string folderPath = @"images"; // IMPORTANT: Set this to your image directory
string filePattern = "*.png"; // Change to "*.jpg", "*.bmp", etc. as needed
string outputFilePath = "ocr_results.txt"; // The new results file
// Get all image files in the directory
var imageFiles = Directory.GetFiles(folderPath, filePattern);
Console.WriteLine($"Found {imageFiles.Length} total images to process...");
Console.WriteLine($"Results will be written to: {outputFilePath}");
// --- 2. Start timer and process images, writing to file ---
// Open the output file *before* the loop for efficiency
using (StreamWriter writer = new StreamWriter(outputFilePath))
{
var stopwatch = Stopwatch.StartNew();
foreach (var file in imageFiles)
{
string fileName = Path.GetFileName(file);
using var ocrInput = new OcrInput();
ocrInput.LoadImage(file);
var ocrResult = ocrTesseract.Read(ocrInput);
// Check if any text was actually found
if (!string.IsNullOrEmpty(ocrResult.Text))
{
// Write to Console
Console.WriteLine($"--- Text found in: {fileName} ---");
Console.WriteLine(ocrResult.Text.Trim());
Console.WriteLine("------------------------------------------");
// Write to File
writer.WriteLine($"--- Text found in: {fileName} ---");
writer.WriteLine(ocrResult.Text.Trim());
writer.WriteLine("------------------------------------------");
writer.WriteLine(); // Add a blank line for readability
}
else
{
// Write to Console
Console.WriteLine($"No text found in: {fileName}");
// Write to File
writer.WriteLine($"No text found in: {fileName}");
writer.WriteLine();
}
}
stopwatch.Stop();
// --- 3. Print and write final benchmark summary ---
string lineSeparator = "\n========================================";
string title = "Batch OCR Processing Complete";
string summary = $"Fast configuration took {stopwatch.Elapsed.TotalSeconds:F2} seconds";
// Write summary to Console
Console.WriteLine(lineSeparator);
Console.WriteLine(title);
Console.WriteLine("========================================");
Console.WriteLine(summary);
// Write summary to File
writer.WriteLine(lineSeparator);
writer.WriteLine(title);
writer.WriteLine("========================================");
writer.WriteLine(summary);
if (imageFiles.Length > 0)
{
string avgTime = $"Average time per image: {(stopwatch.Elapsed.TotalSeconds / (double)imageFiles.Length):F3} seconds";
Console.WriteLine(avgTime);
writer.WriteLine(avgTime);
}
}
Console.WriteLine($"\nSuccessfully saved results to {outputFilePath}");using IronOcr;
using System;
using System.Diagnostics;
using System.IO;
// --- Tesseract Engine Setup ---
var ocrTesseract = new IronTesseract();
ocrTesseract.Language = OcrLanguage.EnglishFast;
ocrTesseract.Configuration.ReadBarCodes = false;
ocrTesseract.Configuration.PageSegmentationMode = TesseractPageSegmentationMode.Auto;
// --- 1. Define folder and get files ---
string folderPath = @"images"; // IMPORTANT: Set this to your image directory
string filePattern = "*.png"; // Change to "*.jpg", "*.bmp", etc. as needed
string outputFilePath = "ocr_results.txt"; // The new results file
// Get all image files in the directory
var imageFiles = Directory.GetFiles(folderPath, filePattern);
Console.WriteLine($"Found {imageFiles.Length} total images to process...");
Console.WriteLine($"Results will be written to: {outputFilePath}");
// --- 2. Start timer and process images, writing to file ---
// Open the output file *before* the loop for efficiency
using (StreamWriter writer = new StreamWriter(outputFilePath))
{
var stopwatch = Stopwatch.StartNew();
foreach (var file in imageFiles)
{
string fileName = Path.GetFileName(file);
using var ocrInput = new OcrInput();
ocrInput.LoadImage(file);
var ocrResult = ocrTesseract.Read(ocrInput);
// Check if any text was actually found
if (!string.IsNullOrEmpty(ocrResult.Text))
{
// Write to Console
Console.WriteLine($"--- Text found in: {fileName} ---");
Console.WriteLine(ocrResult.Text.Trim());
Console.WriteLine("------------------------------------------");
// Write to File
writer.WriteLine($"--- Text found in: {fileName} ---");
writer.WriteLine(ocrResult.Text.Trim());
writer.WriteLine("------------------------------------------");
writer.WriteLine(); // Add a blank line for readability
}
else
{
// Write to Console
Console.WriteLine($"No text found in: {fileName}");
// Write to File
writer.WriteLine($"No text found in: {fileName}");
writer.WriteLine();
}
}
stopwatch.Stop();
// --- 3. Print and write final benchmark summary ---
string lineSeparator = "\n========================================";
string title = "Batch OCR Processing Complete";
string summary = $"Fast configuration took {stopwatch.Elapsed.TotalSeconds:F2} seconds";
// Write summary to Console
Console.WriteLine(lineSeparator);
Console.WriteLine(title);
Console.WriteLine("========================================");
Console.WriteLine(summary);
// Write summary to File
writer.WriteLine(lineSeparator);
writer.WriteLine(title);
writer.WriteLine("========================================");
writer.WriteLine(summary);
if (imageFiles.Length > 0)
{
string avgTime = $"Average time per image: {(stopwatch.Elapsed.TotalSeconds / (double)imageFiles.Length):F3} seconds";
Console.WriteLine(avgTime);
writer.WriteLine(avgTime);
}
}
Console.WriteLine($"\nSuccessfully saved results to {outputFilePath}");Imports IronOcr
Imports System
Imports System.Diagnostics
Imports System.IO
' --- Tesseract Engine Setup ---
Dim ocrTesseract As New IronTesseract()
ocrTesseract.Language = OcrLanguage.EnglishFast
ocrTesseract.Configuration.ReadBarCodes = False
ocrTesseract.Configuration.PageSegmentationMode = TesseractPageSegmentationMode.Auto
' --- 1. Define folder and get files ---
Dim folderPath As String = "images" ' IMPORTANT: Set this to your image directory
Dim filePattern As String = "*.png" ' Change to "*.jpg", "*.bmp", etc. as needed
Dim outputFilePath As String = "ocr_results.txt" ' The new results file
' Get all image files in the directory
Dim imageFiles = Directory.GetFiles(folderPath, filePattern)
Console.WriteLine($"Found {imageFiles.Length} total images to process...")
Console.WriteLine($"Results will be written to: {outputFilePath}")
' --- 2. Start timer and process images, writing to file ---
' Open the output file *before* the loop for efficiency
Using writer As New StreamWriter(outputFilePath)
Dim stopwatch = Stopwatch.StartNew()
For Each file In imageFiles
Dim fileName As String = Path.GetFileName(file)
Using ocrInput As New OcrInput()
ocrInput.LoadImage(file)
Dim ocrResult = ocrTesseract.Read(ocrInput)
' Check if any text was actually found
If Not String.IsNullOrEmpty(ocrResult.Text) Then
' Write to Console
Console.WriteLine($"--- Text found in: {fileName} ---")
Console.WriteLine(ocrResult.Text.Trim())
Console.WriteLine("------------------------------------------")
' Write to File
writer.WriteLine($"--- Text found in: {fileName} ---")
writer.WriteLine(ocrResult.Text.Trim())
writer.WriteLine("------------------------------------------")
writer.WriteLine() ' Add a blank line for readability
Else
' Write to Console
Console.WriteLine($"No text found in: {fileName}")
' Write to File
writer.WriteLine($"No text found in: {fileName}")
writer.WriteLine()
End If
End Using
Next
stopwatch.Stop()
' --- 3. Print and write final benchmark summary ---
Dim lineSeparator As String = vbLf & "========================================"
Dim title As String = "Batch OCR Processing Complete"
Dim summary As String = $"Fast configuration took {stopwatch.Elapsed.TotalSeconds:F2} seconds"
' Write summary to Console
Console.WriteLine(lineSeparator)
Console.WriteLine(title)
Console.WriteLine("========================================")
Console.WriteLine(summary)
' Write summary to File
writer.WriteLine(lineSeparator)
writer.WriteLine(title)
writer.WriteLine("========================================")
writer.WriteLine(summary)
If imageFiles.Length > 0 Then
Dim avgTime As String = $"Average time per image: {(stopwatch.Elapsed.TotalSeconds / CDbl(imageFiles.Length)):F3} seconds"
Console.WriteLine(avgTime)
writer.WriteLine(avgTime)
End If
End Using
Console.WriteLine(vbLf & $"Successfully saved results to {outputFilePath}")Imports IronOcr
Imports System
Imports System.Diagnostics
Imports System.IO
' --- Tesseract Engine Setup ---
Dim ocrTesseract As New IronTesseract()
ocrTesseract.Language = OcrLanguage.EnglishFast
ocrTesseract.Configuration.ReadBarCodes = False
ocrTesseract.Configuration.PageSegmentationMode = TesseractPageSegmentationMode.Auto
' --- 1. Define folder and get files ---
Dim folderPath As String = "images" ' IMPORTANT: Set this to your image directory
Dim filePattern As String = "*.png" ' Change to "*.jpg", "*.bmp", etc. as needed
Dim outputFilePath As String = "ocr_results.txt" ' The new results file
' Get all image files in the directory
Dim imageFiles = Directory.GetFiles(folderPath, filePattern)
Console.WriteLine($"Found {imageFiles.Length} total images to process...")
Console.WriteLine($"Results will be written to: {outputFilePath}")
' --- 2. Start timer and process images, writing to file ---
' Open the output file *before* the loop for efficiency
Using writer As New StreamWriter(outputFilePath)
Dim stopwatch = Stopwatch.StartNew()
For Each file In imageFiles
Dim fileName As String = Path.GetFileName(file)
Using ocrInput As New OcrInput()
ocrInput.LoadImage(file)
Dim ocrResult = ocrTesseract.Read(ocrInput)
' Check if any text was actually found
If Not String.IsNullOrEmpty(ocrResult.Text) Then
' Write to Console
Console.WriteLine($"--- Text found in: {fileName} ---")
Console.WriteLine(ocrResult.Text.Trim())
Console.WriteLine("------------------------------------------")
' Write to File
writer.WriteLine($"--- Text found in: {fileName} ---")
writer.WriteLine(ocrResult.Text.Trim())
writer.WriteLine("------------------------------------------")
writer.WriteLine() ' Add a blank line for readability
Else
' Write to Console
Console.WriteLine($"No text found in: {fileName}")
' Write to File
writer.WriteLine($"No text found in: {fileName}")
writer.WriteLine()
End If
End Using
Next
stopwatch.Stop()
' --- 3. Print and write final benchmark summary ---
Dim lineSeparator As String = vbLf & "========================================"
Dim title As String = "Batch OCR Processing Complete"
Dim summary As String = $"Fast configuration took {stopwatch.Elapsed.TotalSeconds:F2} seconds"
' Write summary to Console
Console.WriteLine(lineSeparator)
Console.WriteLine(title)
Console.WriteLine("========================================")
Console.WriteLine(summary)
' Write summary to File
writer.WriteLine(lineSeparator)
writer.WriteLine(title)
writer.WriteLine("========================================")
writer.WriteLine(summary)
If imageFiles.Length > 0 Then
Dim avgTime As String = $"Average time per image: {(stopwatch.Elapsed.TotalSeconds / CDbl(imageFiles.Length)):F3} seconds"
Console.WriteLine(avgTime)
writer.WriteLine(avgTime)
End If
End Using
Console.WriteLine(vbLf & $"Successfully saved results to {outputFilePath}")这段基准测试代码演示了以下做法:
- 批量处理:代码在一次操作中处理多张图像。多线程 OCR 示例也展示了类似的做法,并说明如何通过并行处理进一步提速。两项设置可以配合使用:快速配置可能略微降低准确性,以加快识别;调低
MaxDegreeOfParallelism可减少内存占用,但会降低吞吐量。 - 性能测量:使用
Stopwatch类能够提供精确到毫秒的时间测量,这对于比较不同的配置至关重要。 - 结果记录:控制台和文件输出都确保您可以稍后分析结果并验证不同配置之间的准确性差异。
性能可以提高多少?
| 模式 | 总时间 | 平均耗时/图像 | 较标准模式节省的时间 | 较标准模式的准确性提升 |
|---|---|---|---|---|
| 标准 | 10.40秒 | 1.040秒 | 基准 | 基准 |
| 快速 | 8.60秒 | 0.860秒 | +17.31%(耗时更短) | +0%(相同) |
基准测试显示,快速配置显著缩短了处理时间。标准模式处理同一批 10 张图像共耗时 10.40 秒,快速配置仅耗时 8.60 秒,节省了 17.31% 的时间。对于这些清晰、对比度高的样本,两种配置输出的文本完全相同。处理质量较低或噪声较多的输入时,EnglishFast 可能会降低识别准确性。
要验证结果,您可以下载快速配置的文本输出和标准配置的文本输出。
我应该何时使用快速配置?
快速配置尤其适用于以下场景:
- 大量文档处理,数千页文档需要快速处理
- 实时应用程序,响应时间至关重要
- Web 应用程序,需要及时响应用户操作
- 批量处理系统,运行时间紧迫
对于涉及多种语言、低质量扫描或如车牌或护照等特殊文档类型的复杂场景,您可能希望使用标准配置以确保最大限度的准确性。
只需修改几个属性即可切换 IronOCR 配置,使应用程序适应不同的性能要求,无需大幅改动代码。
常见问题
在C#中使用IronOCR快速配置的主要好处是什么?
使用IronOCR快速配置的主要好处是性能提升约17%,如基准测试所示。这是在对准确性产生最小影响的情况下实现的,使其非常适合速度至关重要的大批量OCR任务。
IronOCR的EnglishFast模式如何提高OCR处理速度?
IronOCR的EnglishFast模式通过简化语言处理优先考虑速度。它启用了更快的文本提取,但在准确性上的微小折衷,对于清晰的高对比度文本来说几乎可以忽略不计。
为什么应该在快速配置中禁用不必要的功能如条形码读取?
在IronOCR的快速配置中,禁用不必要的功能如条形码读取可以减少处理的开销,允许OCR引擎专注于文本提取,从而加速整个过程。
哪些类型的应用程序从IronOCR的快速配置中受益最大?
从快速配置中受益最大的应用程序包括大批量文档处理、实时系统、需要快速响应的Web应用程序以及时间紧迫的批处理系统。
IronOCR在通过快速配置提高速度的同时如何保持准确性?
IronOCR通过使用EnglishFast模式优化语言处理,并在信息提取过程中专注于文本区域,确保即使在更快的速度下也能准确读取高对比度文本。
设置IronOCR的快速配置涉及哪些步骤?
设置IronOCR的快速配置:通过NuGet安装OCR库,初始化OCR引擎,将Language设置为EnglishFast,禁用ReadBarCodes属性,然后继续加载和处理图像。
IronOCR的快速配置能否高效处理批处理?
是的,IronOCR的快速配置旨在高效处理批处理,允许在一次操作中处理多个图像,由于每个图像的计算负荷减少,速度得以提高。
基准测试如何体现快速配置的性能?
使用一组样本图像进行基准测试显示,快速配置可以比标准模式快17%地处理图像,展示了其卓越的速度而不影响文本输出的准确性。
何时应优先选择标准OCR配置而不是快速配置?
在需要高准确性的场景中,如处理低质量扫描、识别多种语言或处理护照、车牌等专业化文档时,应使用标准OCR配置。
在IronOCR中切换快速和标准配置是否容易?
是的,IronOCR允许通过更改几个属性轻松切换配置,实现快速适应不同的性能需求而无需复杂的代码调整。
Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。