如何使用IronOCR提取护照数据
在机场柜台值机和安检等应用和系统中,工作人员每天都要处理大量护照,因此拥有一个能够准确提取旅客关键信息的可靠系统对于确保高效、简化的出入境流程至关重要。
IronOCR 是一款可靠的工具,可以毫不费力地从护照中提取和读取数据。 通过简单调用ReadPassport
方法,该过程变得简单。
如何使用IronOCR提取护照数据
- 下载用于读取护照的C#库
- 导入护照图像进行阅读
- 确保文件只包含护照图像,不包含页眉或页脚
- 使用
ReadPassport
方法从图像中提取数据 - 访问OcrPassportResult属性以查看和进一步处理提取的护照数据
立即在您的项目中开始使用IronOCR,并享受免费试用。
要使用此功能,您还必须安装IronOcr.Extensions.AdvancedScan包。
提取护照数据示例
举例来说,我们将使用一张护照图片作为输入,来展示 IronOCR 的功能。 加载图像后,您可以利用ReadPassport
方法来识别和提取护照信息。 此方法返回一个OcrPassportResult对象,其中包含GivenNames、Country、PassportNumber、Surname、DateOfBirth和DateOfExpiry等属性。 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)
输出

然后,我们访问从OcrPassportResult
对象获得的PassportInfo
数据成员。
- GivenNames:
PassportInfo
的一个属性,以字符串形式返回护照输入的给定名字。 这相当于 MRZ 数据行的第一行,位置从 4 到 44。 - 国家:
PassportInfo
的一个属性,返回护照输入的国家作为字符串。 这相当于 MRZ 数据行的第一行,位置从 2 到 3。 返回的字符串将拼出发行国的全名,而不是缩写。 在我们的例子中,USA 返回美利坚合众国。 - PassportNumber:
PassportInfo
的一个属性,将护照输入的名字返回为字符串。 这相当于 MRZ 数据行的第二行,位置从 1 到 9。 - 姓氏:
PassportInfo
的一个属性,以字符串形式返回护照输入的姓氏。 这相当于 MRZ 数据行的第一行,位置从 4 到 44。 - DateOfBirth:
PassportInfo
的一个属性,返回护照输入的出生日期,格式为 YYYY-MM-DD 的字符串。 这相当于 MRZ 数据行的第二个位置 14 至 19。 - DateOfExpiry:
PassportInfo
的一个属性成员,以字符串格式返回护照输入的有效期,格式为 YYYY-MM-DD。 这相当于 MRZ 数据行的第二个位置 22 至 27。
了解 MRZ 信息
IronOCR 读取符合(国际民用航空组织)标准的护照底部两行中的 MRZ 信息。ICAO。 MRZ 数据由两行数据组成,每组位置包含唯一的信息。 下面是一个简表,说明哪些信息与行文索引相对应; 对于所有异常和唯一标识符,请参阅ICAO文档标准。
示例输入:

第一行
Position | Field | Description |
---|---|---|
1 | Document Type | Typically 'P' for passport |
2-3 | Issuing Country | Three-letter country code (ISO 3166-1 alpha-3) |
4-44 | Surname and Given Names | Surname followed by '<<' and then given names separated by '<' |
第二行
Position | Field | Description |
---|---|---|
1-9 | Passport Number | Unique passport number |
10 | Check Digit (Passport Number) | Check digit for the passport number |
11-13 | Nationality | Three-letter nationality code (ISO 3166-1 alpha-3) |
14-19 | Date of Birth | Date of birth in YYMMDD format |
20 | Check Digit (Date of Birth) | Check digit for the date of birth |
21 | Sex | Gender ('M' for male, 'F' for female, 'X' for unspecified) |
22-27 | Date of Expiry | Expiry date in YYMMDD format |
28 | Check Digit (Date of Expiry) | Check digit for the date of expiry |
29-42 | Personal Number | Optional personal number (usually national ID number) |
43 | Check Digit (Personal Number) | Check digit for the personal number |
44 | Check Digit (Composite) | Overall check digit |
调试
我们还可以通过获取从护照图像中提取的原始文本以及置信度来验证 IronOCR 的结果,从而确认提取的信息是否准确。 使用上面的示例,我们可以访问 OcrPassportResult
对象的 Confidence
和 Text
属性。
: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)
控制台输出:

- 置信度:
OcrPassportResult
的Confidence
属性是一个浮点数,表示OCR统计精确度置信度,是每个字符的平均值。 如果护照图像模糊或包含其他信息,则浮动率会降低。 其中,"1 "表示信心最高,"0 "表示信心最低,"1 "表示信心最大,"0 "表示信心最小。 - 文本:
OcrPassportResult
的Text
属性包含从护照图像中提取的原始未解析文本。 开发人员可以将其用于单元测试,以验证提取的护照图像文本。