Passport OCR Ruby

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

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

sample passport

Quick Start

require 'mindee'

# Init a new client, specifying an API key
mindee_client = 'my-api-key')

# Send the file
result = mindee_client.doc_from_path('/path/to/the/file.ext').parse(Mindee::Prediction::PassportV1)

# Print a summary of the document prediction in RST format
puts result.inference.prediction


: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


Each prediction object contains a set of different fields.
Each Field object contains at a minimum the following attributes:

  • value (String or Float depending on the field type): corresponds to the field value. Can be nil if no value was extracted.
  • confidence (Float): the confidence score of the field prediction.
  • bounding_box (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.


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

Using the above sample, the following are the basic fields that can be extracted:

Birth Place

birth_place (Field): Passport owner birthplace.

puts result.inference.prediction.birth_place.value


country (Field): Passport country in ISO 3166-1 alpha-3 code format (3-letter code).



Date fields:

  • contain the date_object attribute, which is a standard Ruby date object
  • have a value attribute which is the ISO 8601 representation of the date.

The following date fields are available:

expiry_date: Passport expiry date.

puts result.inference.prediction.expiry_date.value

issuance_date: Passport date of issuance.

puts result.inference.prediction.issuance_date.value

birth_date: Passport's owner date of birth.

puts result.inference.prediction.birth_date.value


gender (Field): Passport owner's gender (M / F).

puts result.inference.prediction.gender.value


given_names (Array< Field >): List of passport owner's given names.

result.inference.prediction.given_names.each do |name|
  puts name

surname (Field): Passport's owner surname.

puts result.inference.prediction.surname.value


id_number (Field): Passport identification number.

puts result.inference.prediction.id_number.value

Machine-Readable Zone

mrz1 (Field): Passport first line of machine-readable zone.

puts result.inference.prediction.mrz1.value

mrz2 (Field): Passport second line of machine-readable zone.

puts result.inference.prediction.mrz2.value

mrz (Field): Reconstructed passport full machine-readable zone from mrz1 and mrz2.

puts result.inference.prediction.mrz.value


Join our Slack