如何使用IronOCR提取护照数据

查克尼特·宾
查克尼特·宾
2024年八月7日
更新 2025年二月16日
分享:
This article was translated from English: Does it need improvement?
Translated
View the article in English

在机场柜台值机和安检等应用和系统中,工作人员每天都要处理大量护照,因此拥有一个能够准确提取旅客关键信息的可靠系统对于确保高效、简化的出入境流程至关重要。

IronOCR 是一款可靠的工具,可以毫不费力地从护照中提取和读取数据。 通过简单调用ReadPassport方法,该过程变得简单。

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

第一步:
green arrow pointer

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

提取护照数据示例

举例来说,我们将使用一张护照图片作为输入,来展示 IronOCR 的功能。 加载图像后,您可以利用ReadPassport方法来识别和提取护照信息。 此方法返回一个OcrPassportResult对象,其中包含GivenNamesCountryPassportNumberSurnameDateOfBirthDateOfExpiry等属性。 PassportInfo 对象的所有成员都是字符串。

请注意

  • 该方法目前仅适用于英文版护照。
  • 使用高级扫描在 .NET Framework 上需要项目运行在 x64 架构上。

护照输入

图片样本

代码

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

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

using var inputPassport = new OcrInput();

inputPassport.LoadImage("passport.jpg");

// Perform OCR
OcrPassportResult result = ocr.ReadPassport(inputPassport);

// Output passport information
Console.WriteLine(result.PassportInfo.GivenNames);
Console.WriteLine(result.PassportInfo.Country);
Console.WriteLine(result.PassportInfo.PassportNumber);
Console.WriteLine(result.PassportInfo.Surname);
Console.WriteLine(result.PassportInfo.DateOfBirth);
Console.WriteLine(result.PassportInfo.DateOfExpiry);
Imports IronOcr
Imports System

' Instantiate OCR engine
Private ocr = New IronTesseract()

Private inputPassport = New OcrInput()

inputPassport.LoadImage("passport.jpg")

' Perform OCR
Dim result As OcrPassportResult = ocr.ReadPassport(inputPassport)

' Output passport information
Console.WriteLine(result.PassportInfo.GivenNames)
Console.WriteLine(result.PassportInfo.Country)
Console.WriteLine(result.PassportInfo.PassportNumber)
Console.WriteLine(result.PassportInfo.Surname)
Console.WriteLine(result.PassportInfo.DateOfBirth)
Console.WriteLine(result.PassportInfo.DateOfExpiry)
$vbLabelText   $csharpLabel

输出

结果输出

然后,我们访问从OcrPassportResult对象获得的PassportInfo数据成员。

  • GivenNames: PassportInfo 的一个属性,以字符串形式返回护照输入的给定名字。 这相当于 MRZ 数据行的第一行,位置从 4 到 44。
  • 国家PassportInfo 的一个属性,返回护照输入的国家作为字符串。 这相当于 MRZ 数据行的第一行,位置从 2 到 3。 返回的字符串将拼出发行国的全名,而不是缩写。 在我们的例子中,USA 返回美利坚合众国。
  • PassportNumberPassportInfo 的一个属性,将护照输入的名字返回为字符串。 这相当于 MRZ 数据行的第二行,位置从 1 到 9。
  • 姓氏PassportInfo 的一个属性,以字符串形式返回护照输入的姓氏。 这相当于 MRZ 数据行的第一行,位置从 4 到 44。
  • DateOfBirthPassportInfo 的一个属性,返回护照输入的出生日期,格式为 YYYY-MM-DD 的字符串。 这相当于 MRZ 数据行的第二个位置 14 至 19。
  • DateOfExpiry: PassportInfo 的一个属性成员,以字符串格式返回护照输入的有效期,格式为 YYYY-MM-DD。 这相当于 MRZ 数据行的第二个位置 22 至 27。

了解 MRZ 信息

IronOCR 读取符合(国际民用航空组织)标准的护照底部两行中的 MRZ 信息。ICAO。 MRZ 数据由两行数据组成,每组位置包含唯一的信息。 下面是一个简表,说明哪些信息与行文索引相对应; 对于所有异常和唯一标识符,请参阅ICAO文档标准。

示例输入:

MRZ位置

第一行

Position Field Description
1Document TypeTypically 'P' for passport
2-3Issuing CountryThree-letter country code (ISO 3166-1 alpha-3)
4-44Surname and Given NamesSurname followed by '<<' and then given names separated by '<'

第二行

Position Field Description
1-9Passport NumberUnique passport number
10Check Digit (Passport Number)Check digit for the passport number
11-13NationalityThree-letter nationality code (ISO 3166-1 alpha-3)
14-19Date of BirthDate of birth in YYMMDD format
20Check Digit (Date of Birth)Check digit for the date of birth
21SexGender ('M' for male, 'F' for female, 'X' for unspecified)
22-27Date of ExpiryExpiry date in YYMMDD format
28Check Digit (Date of Expiry)Check digit for the date of expiry
29-42Personal NumberOptional personal number (usually national ID number)
43Check Digit (Personal Number)Check digit for the personal number
44Check Digit (Composite)Overall check digit

调试

我们还可以通过获取从护照图像中提取的原始文本以及置信度来验证 IronOCR 的结果,从而确认提取的信息是否准确。 使用上面的示例,我们可以访问 OcrPassportResult 对象的 ConfidenceText 属性。

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

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

using var inputPassport = new OcrInput();

inputPassport.LoadImage("passport.jpg");

// Perform OCR
OcrPassportResult result = ocr.ReadPassport(inputPassport);

// Output Confidence level and raw extracted text
Console.WriteLine(result.Confidence);
Console.WriteLine(result.Text);
Imports IronOcr
Imports System

' Instantiate OCR engine
Private ocr = New IronTesseract()

Private inputPassport = New OcrInput()

inputPassport.LoadImage("passport.jpg")

' Perform OCR
Dim result As OcrPassportResult = ocr.ReadPassport(inputPassport)

' Output Confidence level and raw extracted text
Console.WriteLine(result.Confidence)
Console.WriteLine(result.Text)
$vbLabelText   $csharpLabel

控制台输出

调试
  • 置信度OcrPassportResultConfidence 属性是一个浮点数,表示OCR统计精确度置信度,是每个字符的平均值。 如果护照图像模糊或包含其他信息,则浮动率会降低。 其中,"1 "表示信心最高,"0 "表示信心最低,"1 "表示信心最大,"0 "表示信心最小。
  • 文本OcrPassportResultText 属性包含从护照图像中提取的原始未解析文本。 开发人员可以将其用于单元测试,以验证提取的护照图像文本。
查克尼特·宾
软件工程师
Chaknith 负责 IronXL 和 IronBarcode 的工作。他在 C# 和 .NET 方面拥有深厚的专业知识,帮助改进软件并支持客户。他从用户互动中获得的洞察力,有助于提升产品、文档和整体体验。