Passport OCR Node.js

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

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

Quick Start

import { Client,  PassportResponse } from "mindee";

// Init a new client
const mindeeClient = new Client({ apiKey: "my-api-key" });

// Load a file from disk and parse it
const doc = mindeeClient.docFromPath("./4a16b1d-passport_pic.jpg");
const respPromise = doc.parse(PassportResponse);

// Print a summary of the parsed data
respPromise.then((resp) => {
  if (resp.document === undefined) return;

  console.log(resp.document.toString());
});

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
----------------------

Field Objects

Each Field object contains at a minimum the following attributes:

  • value (string or number depending on the field type):
    Corresponds to the field value. Can be null if no value was extracted.
  • confidence (Float):
    The confidence score of the field prediction.
  • bbox (Array< Array< Float > >):
    Contains exactly 4 relative vertices coordinates (points) of a right rectangle containing the field in the document.
  • polygon (Array< Array< Float > >):
    Contains the relative vertices coordinates (points) of a polygon containing the field in the image.
  • reconstructed (Boolean):
    True if the field was reconstructed or computed using other fields.

Extracted Fields

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

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

Orientation

  • orientation (Orientation): The orientation field is only available at the page level as it describes whether the page image should be rotated to be upright.

If the page requires rotation for correct display, the orientation field gives a prediction among these 3 possible outputs:

  • 0 degrees: the page is already upright
  • 90 degrees: the page must be rotated clockwise to be upright
  • 270 degrees: the page must be rotated counterclockwise to be upright
// Show the orientation of each page
resp.pages.forEach((page) => {
  console.log(page.orientation.value);
});

Birth Place

  • birthPlace (Field): Passport owner birthplace.
// To get the passport's owner
const birthPlace = resp.document.birthPlace.value

Country

// To get the passport country code
const country = resp.document.country.value

Dates

Date fields:

  • contain the date_object attribute, which is a standard Ruby date object
  • can contain the raw attribute, which is the textual representation found on the document.
  • have a value attribute which is the ISO 8601 representation of the date, regardless of the raw contents.

The following date fields are available:

  • expiry_date: Passport expiry date.
// To get the passport expiry date
const expiryDate = resp.document.expiryDate.value
  • issuance_date: Passport date of issuance.
// To get the passport date of issuance
const issuanceDate = resp.document.issuanceDate.value
  • birth_date: Passport's owner date of birth.
// To get the passport's owner date of birth
const birthDate = resp.document.birthDate.value

Gender

  • gender (Field): Passport owner's gender (M / F).
// To get the passport owner gender, string among (M, F)
const gender = resp.document.gender.value

Given Names

  • givenNames (Array< Field >): List of passport owner's given names.
// To get the list of names
resp.document.givenNames.forEach((name) => {
  console.log(name.value);
});

ID

  • id_number (Field): Passport identification number.
// To get the passport id number (string)
const idNumber = resp.document.idNumber.value

Machine Readable Zone

  • mrz1 (Field): Passport first line of machine-readable zone.
// To get the passport  first line of machine readable zone (string)
const mrz1 = resp.document.mrz1.value
  • mrz2 (Field): Passport second line of machine-readable zone.
// To get the passport full machine-readable zone (string)
const mrz2 = resp.document.mrz2.value
  • mrz (Field): Reconstructed passport full machine-readable zone from mrz1 and mrz2.
// To get the passport full machine readable zone (string)
const mrz = resp.document.mrz.value

Surname

  • surname (Field): Passport's owner surname.
// To get the passport's owner surname
const surname = resp.document.surname.value

 

Questions?
Slack Logo IconSlack Logo Icon  Join our Slack


Did this page help you?