如何使用 IronOCR 提取護照資料
在像是機場的櫃檯報到和安檢入境等應用程式和系統中,代理需每日處理大量護照,擁有一套可靠的系統來準確提取關於旅客的重要任務關鍵資訊,對於確保高效且順暢的入境過程至關重要。
IronOCR 是一款可靠的工具,可以輕鬆提取和閱讀護照上的數據。 透過簡單地調用 ReadPassport
方法,該過程變得簡單明瞭。
如何使用 IronOCR 提取護照資料
- 下載用於讀取護照的 C# 庫
- 導入護照圖像以進行讀取
- 確保文件僅包含護照照片,沒有頁眉或頁腳。
- 使用
读护照
從影像中提取資料的方法 - 訪問 OcrPassportResult 查看並進一步操作提取的護照數據的屬性
立即在您的專案中使用IronOCR,並享受免費試用。
若要使用此功能,您還需要安裝IronOcr.Extension.AdvancedScan套件。
護照數據提取範例
作為範例,我們將使用護照圖片作為輸入來展示 IronOCR 的功能。 在使用 OcrInput 載入圖像後,您可以使用 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。 - Surname:
PassportInfo
的一個屬性,將護照輸入的姓氏作為字符串返回。 這對應於第一行 MRZ 數據,位置從 4 至 44。 - DateOfBirth:
PassportInfo
的一個屬性,返回護照輸入的出生日期,格式為 YYYY-MM-DD 的字串。 這對應於第二行 MRZ 資料,位置 14 到 19。 - DateOfExpiry:
PassportInfo
的屬性成員,返回護照輸入的到期日期,格式為 YYYY-MM-DD 的字串。 這對應於第二個MRZ數據行,位置22到27。
了解機讀區資訊
IronOCR 讀取任何符合標準的護照底部兩行中的 MRZ 信息。(國際民用航空組織) ICAO. MRZ資料由兩行數據組成,每組位置包含獨特的信息。 以下是一個簡短的表格,其中信息對應於行的索引; 所有異常和唯一識別碼,請參閱ICAO文檔標準。
示例護照輸入:
第一列
位置 | 欄位 | 描述 |
---|---|---|
文件類型 | 通常是「P」代表護照。 | |
-3 | 發行國家 | 三字母國家代碼(ISO 3166-1 alpha-3) |
-44 | 姓氏及名字 | 姓氏後接著「<<' and then given names separated by '<' |
第二行
位置 | 欄位 | 描述 |
---|---|---|
-9 | 護照號碼 | 護照號碼唯一性 |
0 | 校验码(护照号码) | 護照號碼的檢查碼 |
1-13 | 國籍 | 三字母國籍代碼(ISO 3166-1 alpha-3) |
4-19 | 出生日期 | 出生日期以YYMMDD格式顯示 |
0 | 校验码(出生日期) | 出生日期的校驗碼 |
1 | 性別 | 性別('M' 代表男性,'F' 代表女性,'X' 代表未指定) |
2-27 | 到期日 | 以 YYMMDD 格式的到期日期 |
8 | 校驗碼(到期日) | 檢查到期日的校驗碼 |
9-42 | 個人號碼 | 選填個人號碼(通常為國民身分證號碼) |
3 | 檢查數字(個人號碼) | 個人號碼的檢查數字 |
4 | 校验位(复合) | 整體檢查碼 |
除錯
我們還可以通過從護照圖像中獲取原始提取的文本和信心水平來驗證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代表最低且最沒有信心。 OcrPassportResult
的Text
屬性包含從護照圖像提取的原始未解析文本。 開發人員可以在單元測試中使用此功能來驗證護照圖像中提取的文本。