Passport OCR Python

The Python OCR SDK supports the passport API for extracting data from passports.

from mindee import Client, documents

# Init a new client
mindee_client = Client(api_key="my-api-key")

# Load a file from disk
input_doc = mindee_client.doc_from_path("/path/to/the/file.ext")

# Parse the document as an Invoice by passing the appropriate type
api_response = input_doc.parse(documents.TypePassportV1)

print(api_response.document)

Using this sample fake passport below, we are going to illustrate how to extract the data that we want using the SDK.
fake passport

Passport Data Structure

The passport object JSON data structure consists of:

Document Level Prediction

For document level prediction, we construct the document class by combining the different pages in a single document.
This method used for creating a single passport object from multiple pages relies on field confidence scores.

Basically, we iterate over each page, and for each field, we keep the one that has the highest probability.

For example, if you send a three-page passport, the document level will provide you with one name, one country code, and so on.

print(api_response.document)

Output:

-----Passport data-----
Filename: passport.jpeg
Full name: HENERT PUDARSAN
Given names: HENERT
Surname: PUDARSAN
Country: GBR
ID Number: 707797979
Issuance date: 2012-04-22
Birth date: 1995-05-20
Expiry date: 2017-04-22
MRZ 1: P<GBRPUDARSAN<<HENERT<<<<<<<<<<<<<<<<<<<<<<<
MRZ 2: 7077979792GBR9505209M1704224<<<<<<<<<<<<<<00
MRZ: P<GBRPUDARSAN<<HENERT<<<<<<<<<<<<<<<<<<<<<<<7077979792GBR9505209M1704224<<<<<<<<<<<<<<00
----------------------

Page level prediction

We create the document class by iterating over each page one by one. Each page in the pdf is treated as a unique page.

For example, if you send a three-page passport, the page-level prediction will provide you with three names, three-countries codes, and so on.

print(passport_data.pages)

Raw HTTP Response

Contains the full Mindee API HTTP response object in JSON format

receipt_data.http_response # full HTTP request object

Extracted Fields

Each passport object contains a set of different fields. Each field contains the four following attributes:

  • value (Str or Float depending on the field type): corresponds to the field value. Set to None if the field was not extracted.
  • probability (Float): the confidence score of the field prediction.
  • bounding_box (Array[Float]): contains the relative vertices coordinates of the bounding box containing the field in the image.
    If the field is not written, the bbox is an empty array.
  • reconstructed (Bool): True if the field was reconstructed using other fields.

Additional Attributes

Depending on the field type specified, additional attributes can be extracted from the passport object.

Using the above example, the following are the basic fields that can be extracted.

Birth Information

  • birth_date (string): Passport's owner date of birth.
# To get the passport's owner date of birth
birth_date = passport_data.document.birth_date.value
print("DOB: ", birth_date)
  • birth_place (string): Passport owner birthplace.
# To get the passport's owner
birth_place = passport_data.document.birth_place.value
print("birthplace: ", birth_place)

Country

# To get the passport country code
country_code = passport_data.document.country.value
print("passport country code: ", country_code)

Date

  • expiry_date (string): Passport expiry date in ISO format (yyyy-mm-dd).
# To get the passport expiry date
expiry_date = passport_data.document.expiry_date.value
print("expires: ", expiry_date)

Gender

  • gender (string): Passport's owner gender (M / F).
# To get the passport's owner gender (string among {"M", "F"}
gender = passport_data.document.gender.value
print("gender: ", gender)

Given Names

  • given_names (string): List of passport's owner given names.
# To get the list of names
given_names = passport_data.document.given_names
print("Given names: ")
# Loop on each given name
for given_name in given_names:
   # To get the name string
   name = given_name.value
print(name)

ID

  • id_number (string): Passport identification number.
# To get the passport id number (string)
id_number = passport_data.document.id_number.value
print("passport number: ", id_number)

Issuance Date

  • issuance_date (string): Passport date of issuance in ISO format (yyyy-mm-dd).
# To get the passport date of issuance
issuance_date = passport_data.document.issuance_date.value
print("issued: ", issuance_date)

Machine Readable Zone

  • mrz1 (string): Passport first line of machine-readable zone.
# To get the passport  first line of machine readable zone (string)
mrz1 = passport_data.document.mrz1.value
print("mrz1: ", mrz1)
  • mrz2 (string): Passport second line of machine-readable zone.
# To get the passport full machine-readable zone (string)
mrz2 = passport_data.document.mrz2.value
print("mrz2: ", mrz2)
  • mrz (string): Reconstructed passport full machine readable zone from mrz1 and mrz2.
# To get the passport full machine readable zone (string)
mrz = passport_data.document.mrz
print("mrz: ", mrz)

Surname

  • surname (string): Passport's owner surname.
# To get the passport's owner surname
surname = passport_data.document.surname.value
print("surname: ", surname)

Questions?

Join our Slack