Wednesday, July 29, 2015

OCR UAE Emirates ID



In this post, i will share my experience in trying to OCR (extract textual information) from UAE Emirates ID. The id has MRZ lines (similar to machine readable passports etc) on the back side which makes this task easier. But there is a background image in the middle of MRZ which can cause some problems.



 I evaluated few proprietary APIs specifically Asprise Java SDK and Abbyy OCR Rest services. .


I tried scanned colored image, and black white images with different orientation.

Among these Abbyy API was better with fewer mistakes. But it suffered when the image orientation is not straight or the image is black and white.


The best result was shown by an Android app called Regula Document Reader. It worked for disoriented black and white images also.
Could not find their evaluation API though.


Here is my attempt using Tess4j. It works very good when the image is scanned (colored or grey scale but not black and white)

The algorithm to correct orientation is not part of Tesseract.

The method here i used is simple:
Change the image to grey scale
Increase contrast and brightness
Use white list of chars that can appear in MRZ
Than do OCR.
public class Test {
public static void main(String[] args) throws Exception {
File imageFile = new File("0501013286_4.tif");
BufferedImage img = ImageIO.read(imageFile);
BufferedImage greyScaleImg = ImageHelper.convertImageToGrayscale(img);

RescaleOp rescaleOp = new RescaleOp(3f, 15, null);
rescaleOp.filter(greyScaleImg, greyScaleImg);

Tesseract instance = Tesseract.getInstance();
instance.setTessVariable("load_system_dawg", "false");
instance.setTessVariable("load_freq_dawg", "false");
instance.setTessVariable("tessedit_char_whitelist",
"ABCDEFGHIJKLMNOPQRSTUVWXYZ</0123456789");

String result = instance.doOCR(greyScaleImg);


System.out.println(result);

}

}