如何使用IronOCR读取照片
在处理大量文档时,尤其是像TIFF文件这样的扫描图像,手动提取文本可能既耗时,又容易出现人为错误。 这就是光学字符识别(OCR)的作用所在,提供了一种自动化的方法,将图像中的文本准确地转换为数字数据。 OCR 技术可以处理图像的复杂性,例如扫描文档或照片,并将其转换为可搜索、可编辑的文本。 这不仅加快了文档处理速度,还确保了与人工转录相比更准确的数据提取。
使用OCR处理TIFF等格式,由于其大小、色深或压缩,可能难以读取,使企业和开发人员能够快速数字化和管理大量数据。 使用像 IronOCR 的 ReadPhoto
功能这样的 OCR 解决方案,开发人员可以从图像中提取文本,甚至可以执行高级操作,例如搜索关键字或将扫描的数据转换为可搜索的 PDF。 这项技术对于处理法律文件、档案或收据等行业尤其有用,因为高效的数据检索至关重要。
在本教程中,我们将简要提供一个输入示例,说明如何使用ReadPhoto
以及如何操作结果对象。 以及开发人员可能会发现他们使用ReadPhoto
而不是IronOCR的标准Read
的情况。
如何使用IronOCR读取照片
- 下载用于读取照片的C#库
- 导入图像进行处理
- 根据图像类型使用适当的导入方法
- 使用
ReadPhoto
方法从图像中提取数据 - 访问OcrPhotoResult属性以查看和操作提取的数据
立即在您的项目中开始使用IronOCR,并享受免费试用。
要使用此功能,您还必须安装IronOcr.Extensions.AdvancedScan包。
读取照片示例
使用IronOCR读取高质量的照片格式(例如tiff
和gif
)相对简单。 首先,我们创建一个新变量并将其指定为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)
输出
文本:从OCR输入中提取的文本。
置信度:“double” 属性,指示每个字符的平均统计准确性置信度,1 为最高,0 为最低。
TextRegions:属性“TextRegions”的列表,指示OCR文本及其在输入中的位置。 在上面的示例中,我们打印了帧号以及包含文本的矩形。
ReadPhoto
与Read
的区别
这个readPhoto
方法与标准read
方法的主要区别在于它所接受的结果对象和文件格式。 LoadImageFrame
专门仅支持tiff
和gif
,而不支持Jpeg
之类的格式,这有几个原因。
Tiff和Jpg图像的比较
Tiff 作为一种文件格式是无损的,通常用于将多页和多帧压缩为单一格式。 它通常用于高质量的多图像存储(例如法律文件、医学图像)。 它比标准的 jpg 格式复杂得多,因此需要不同的方法来完整提取其中的文本。
此外,Tiff 图像仅使用不同的压缩方式,因此 IronOCR 需要使用专门的方法来解码文本。
以下是 TIFF 和 JPG 的比较的进一步细分。
Feature | TIFF (Tagged Image File Format) | JPG/JPEG (Joint Photographic Experts Group) |
---|---|---|
Compression | Lossless or uncompressed (preserves quality) | Lossy compression (reduces quality for smaller file size) |
File Size | Large (due to high quality and optional lack of compression) | Smaller, optimized for web use and fast loading |
Image Quality | High (ideal for professional use, retains all details) | Lower (due to lossy compression, some quality is sacrificed) |
Color Depth | Supports high color depth (up to 16-bit or 32-bit per channel) | 24-bit color (16.7 million colors) |
Use Case | Professional photography, publishing, scanning, archiving | Web images, social media, everyday photos |
Transparency | Supports transparency and alpha channels | Does not support transparency |
Editing | Good for multiple edits (no quality loss with resaving) | Quality degrades with repeated edits and saves |
Compatibility | Widely supported in professional software | Universally supported across all platforms and devices |
Animation | Does not support animation | Does not support animation |
Metadata | Stores extensive metadata (EXIF, layers, etc.) | Stores EXIF metadata but is more limited |
不同场景
开发人员需要在生产中考虑每个用例,以进一步优化并使他们的应用程序高效运行。 虽然readPhoto
适用于如上所述的复杂图像,例如Tiff
,但结果的处理速度会较慢。 另一方面,jpg
可能质量较低,但操作通常会更快。 然而,图像质量(如存在噪点)会导致OCR的置信度较低。
在OcrPhotoResults
或任何使用接口IOcrResult
的类中使用confidence
属性,可以让您了解结果的准确性,从而允许开发人员根据需要进行测试、重复和优化。
开发人员应在效率和准确性之间找到一个微妙的平衡,以确保图像达到一定的阈值以保持一致性。