Smart and powerful OCR tools

OCR MRZ

What is MRZ?

MRZ is the machine-readable-zone with the data on the identity page, encoded in optical character recognition format. It is widely used in passports, vises, travel documents, identity cards, etc.

MRZ Sample

According ISO/IEC-7501, MRZ consists of two or three lines, every line consists of 30, 36 or 44 characters. Most data in MRZ is protected by checksum digits. You can read more information about MRZ format here: Machine-readable-passport

How to recognize MRZ data with Nicomsoft OCR?

NSOCR has a special zone type for that: BT_MRZ, so you can mark a zone as MRZ: Blk_SetType(BlkObj, BT_MRZ) and then recognize it. If you don't know MRZ position, you can create a MRZ zone that covers entire image, or you can set "Zoning/OneZone" option as "9" (9 is BT_MRZ constant) and NSOCR will create such zone automatically. So general C# code to process images that contains MRZ is the following:

//assume reference to NSOCR COM was added
using NSOCR_NameSpace; //Add NSOCR namespace from "NSOCR.cs" file
//...
int CfgObj, OcrObj, ImgObj, SvrObj, res;
NSOCRLib.NSOCRClass NsOCR = new NSOCRLib.NSOCRClass(); //create NSOCR COM object instance
NsOCR.Engine_InitializeAdvanced(out CfgObj, out OcrObj, out ImgObj); //initialize OCR engine, create objects and load configuration
NsOCR.Cfg_SetOption(CfgObj, TNSOCR.BT_DEFAULT, "Zoning/OneZone", "9"); //NSOCR must create BT_MRZ zone that covers entire page
res = NsOCR.Img_LoadFile(ImgObj, "c:\\sample.bmp"); //load some image for OCR
if (res > TNSOCR.ERROR_FIRST) {}; //insert error handler here
NsOCR.Svr_Create(CfgObj, TNSOCR.SVR_FORMAT_XML, out SvrObj); //create Saver object, output format is XML
NsOCR.Svr_NewDocument(SvrObj); //start new document
res = NsOCR.Img_OCR(ImgObj, TNSOCR.OCRSTEP_FIRST, TNSOCR.OCRSTEP_LAST, TNSOCR.OCRFLAG_NONE); //perform OCR
if (res > TNSOCR.ERROR_FIRST) {}; //insert error handler here
res = NsOCR.Svr_AddPage(SvrObj, ImgObj, TNSOCR.FMT_EXACTCOPY); //add OCR'ed image to output document
if (res > TNSOCR.ERROR_FIRST) {}; //insert error handler here
res = NsOCR.Svr_SaveToFile(SvrObj, "c:\\out.xml"); //save OCRed image to XML file
if (res > TNSOCR.ERROR_FIRST) {}; //insert error handler here
NsOCR.Engine_Uninitialize(); //release all created objects and uninitialize OCR engine

If MRZ is recognized successfully, output XML file contains parsed MRZ data, for example:

<MRZ LinesCnt="2" DocType="P" DocSubType="" Country="IDN" LastName="LENGKAP" Name="NAMA" DocNumber="X000000" Nationality="IDN" BirthDate="450817" ExpDate="160126" Sex="S" PersonalNumber="" OptionalData1="" OptionalData2="" DocNumCheck="9" DocNumberVerified="false" BirthDateCheck="9" BirthDateVerified="true" ExpDateCheck="9" ExpDateVerified="false" PersonalNumberCheck="9" PersonalNumberVerified="false" CompositeCheck="1" CompositeVerified="false" />

Detailed description of these fields:

Field Description
LinesCnt Number of lines in MRZ, can be 2 or 3
DocType One letter - the type of a document.
DocSubType One letter - the subtype of a document. Optional.
Country Issuing State or organization - the three-letter code.
LastName Last name.
Name First name.
DocNumber Document number, up to 9 characters, both letters and digits can be used.
Nationality Nationality - the three-letter code.
BirthDate Date of birth, format: YYMMDD.
ExpDate Document expiration date, format: YYMMDD.
Sex M or F.
PersonalNumber Personal number, up to 14 characters. Optional.
OptionalData1 Optional data #1 for 3-lines MRZ, up to 15 characters.
OptionalData2 Optional data #2 for 3-lines MRZ, up to 11 characters.
DocNumCheck The checksum digit for the document number.
DocNumberVerified "true" if the checksum for the document number matches the checksum digit. "false" if it does not.
BirthDateCheck The checksum digit for the birth date.
BirthDateVerified "true" if the checksum for the birth date matches the checksum digit. "false" if it does not.
ExpDateCheck The checksum digit for the expiration date.
ExpDateVerified "true" if the checksum for the expiration date matches the checksum digit. "false" if it does not.
PersonalNumberCheck The checksum digit for the personal number. Optional.
PersonalNumberVerified "true" if the checksum for the personal number matches the checksum digit. "false" if it does not.
CompositeCheck The composite checksum digit.
CompositeVerified "true" if the composite checksum for the document matches the composite checksum digit. "false" if it does not.