如何使用IronOCR读取照片

2025年二月16日
更新 2025年二月16日
分享:
This article was translated from English: Does it need improvement?
Translated
View the article in English

在处理大量文档时,尤其是像TIFF文件这样的扫描图像,手动提取文本可能既耗时,又容易出现人为错误。 这就是光学字符识别(OCR)的作用所在,提供了一种自动化的方法,将图像中的文本准确地转换为数字数据。 OCR 技术可以处理图像的复杂性,例如扫描文档或照片,并将其转换为可搜索、可编辑的文本。 这不仅加快了文档处理速度,还确保了与人工转录相比更准确的数据提取。

使用OCR处理TIFF等格式,由于其大小、色深或压缩,可能难以读取,使企业和开发人员能够快速数字化和管理大量数据。 使用像 IronOCR 的 ReadPhoto 功能这样的 OCR 解决方案,开发人员可以从图像中提取文本,甚至可以执行高级操作,例如搜索关键字或将扫描的数据转换为可搜索的 PDF。 这项技术对于处理法律文件、档案或收据等行业尤其有用,因为高效的数据检索至关重要。

在本教程中,我们将简要提供一个输入示例,说明如何使用ReadPhoto以及如何操作结果对象。 以及开发人员可能会发现他们使用ReadPhoto而不是IronOCR的标准Read的情况。

立即在您的项目中开始使用IronOCR,并享受免费试用。

第一步:
green arrow pointer

要使用此功能,您还必须安装IronOcr.Extensions.AdvancedScan包。

读取照片示例

使用IronOCR读取高质量的照片格式(例如tiffgif)相对简单。 首先,我们创建一个新变量并将其指定为OcrInput,然后使用LoadImageFrame加载图像。 最后,我们使用 ReadPhoto 方法并获取结果。

请注意

  • 由于Tiff在单个图像中包含多个帧,因此需要frameNumber参数。 此外,索引从0开始,而不是从1开始。
  • 该方法目前仅适用于英语、中文、日语、韩语和拉丁字母。
  • 使用高级扫描在 .NET Framework 上需要项目运行在 x64 架构上。

输入

由于大多数浏览器不支持 TIFF 格式,您可以在这里下载 TIFF 输入。 要显示TIFF文件,我将把它转换为WEBP。

输入

代码

:path=/static-assets/ocr/content-code-examples/how-to/read-photo-read-photo.cs
using IronOcr;
using IronSoftware.Drawing;
using System;

// Instantiate OCR engine
var ocr = new IronTesseract();

using var inputPhoto = new OcrInput();
inputPhoto.LoadImageFrame("ocr.tiff", 0);

// Read photo
OcrPhotoResult result = ocr.ReadPhoto(inputPhoto);

// Index number refer to region order in the page
int number = result.TextRegions[0].FrameNumber;

// Extract the text in the first region
string textinregion = result.TextRegions[0].TextInRegion;

//Extract the co_ordinates of the first text region
Rectangle region = result.TextRegions[0].Region;

var output = $"Text in First Region: {textinregion}\n"
             + $"Text Region:\n"
             + $"Starting X: {region.X}\n"
             + $"Starting Y: {region.Y}\n"
             + $"Region Width: {region.Width}\n"
             + $"Region Height: {region.Height}\n"
             + $"Result Confidence: {result.Confidence}\n\n"
             + $"Full Scnned Photo Text: {result.Text}";

Console.WriteLine(output);
Imports Microsoft.VisualBasic
Imports IronOcr
Imports IronSoftware.Drawing
Imports System

' Instantiate OCR engine
Private ocr = New IronTesseract()

Private inputPhoto = New OcrInput()
inputPhoto.LoadImageFrame("ocr.tiff", 0)

' Read photo
Dim result As OcrPhotoResult = ocr.ReadPhoto(inputPhoto)

' Index number refer to region order in the page
Dim number As Integer = result.TextRegions(0).FrameNumber

' Extract the text in the first region
Dim textinregion As String = result.TextRegions(0).TextInRegion

'Extract the co_ordinates of the first text region
Dim region As Rectangle = result.TextRegions(0).Region

Dim output = $"Text in First Region: {textinregion}" & vbLf & $"Text Region:" & vbLf & $"Starting X: {region.X}" & vbLf & $"Starting Y: {region.Y}" & vbLf & $"Region Width: {region.Width}" & vbLf & $"Region Height: {region.Height}" & vbLf & $"Result Confidence: {result.Confidence}" & vbLf & vbLf & $"Full Scnned Photo Text: {result.Text}"

Console.WriteLine(output)
$vbLabelText   $csharpLabel

输出

输出

文本:从OCR输入中提取的文本。

置信度:“double” 属性,指示每个字符的平均统计准确性置信度,1 为最高,0 为最低。

TextRegions:属性“TextRegions”的列表,指示OCR文本及其在输入中的位置。 在上面的示例中,我们打印了帧号以及包含文本的矩形。


ReadPhotoRead的区别

这个readPhoto方法与标准read方法的主要区别在于它所接受的结果对象和文件格式。 LoadImageFrame专门仅支持tiffgif,而不支持Jpeg之类的格式,这有几个原因。

Tiff和Jpg图像的比较

Tiff 作为一种文件格式是无损的,通常用于将多页和多帧压缩为单一格式。 它通常用于高质量的多图像存储(例如法律文件、医学图像)。 它比标准的 jpg 格式复杂得多,因此需要不同的方法来完整提取其中的文本。

此外,Tiff 图像仅使用不同的压缩方式,因此 IronOCR 需要使用专门的方法来解码文本。

以下是 TIFF 和 JPG 的比较的进一步细分。

FeatureTIFF (Tagged Image File Format)JPG/JPEG (Joint Photographic Experts Group)
CompressionLossless or uncompressed (preserves quality)Lossy compression (reduces quality for smaller file size)
File SizeLarge (due to high quality and optional lack of compression)Smaller, optimized for web use and fast loading
Image QualityHigh (ideal for professional use, retains all details)Lower (due to lossy compression, some quality is sacrificed)
Color DepthSupports high color depth (up to 16-bit or 32-bit per channel)24-bit color (16.7 million colors)
Use CaseProfessional photography, publishing, scanning, archivingWeb images, social media, everyday photos
TransparencySupports transparency and alpha channelsDoes not support transparency
EditingGood for multiple edits (no quality loss with resaving)Quality degrades with repeated edits and saves
CompatibilityWidely supported in professional softwareUniversally supported across all platforms and devices
AnimationDoes not support animationDoes not support animation
MetadataStores extensive metadata (EXIF, layers, etc.)Stores EXIF metadata but is more limited

不同场景

开发人员需要在生产中考虑每个用例,以进一步优化并使他们的应用程序高效运行。 虽然readPhoto适用于如上所述的复杂图像,例如Tiff,但结果的处理速度会较慢。 另一方面,jpg可能质量较低,但操作通常会更快。 然而,图像质量(如存在噪点)会导致OCR的置信度较低。

OcrPhotoResults或任何使用接口IOcrResult的类中使用confidence属性,可以让您了解结果的准确性,从而允许开发人员根据需要进行测试、重复和优化。

开发人员应在效率和准确性之间找到一个微妙的平衡,以确保图像达到一定的阈值以保持一致性。