Click here to Skip to main content
15,843,623 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I need to extract MRZ from passport in our project.Could not able to crop MRZ dynamically from given passport

What I have tried:

from easyocr import Reader
import argparse
import cv2

def cleanup_text(text):

    return ''.join([(c if ord(c) < 128 else '') for c in text]).strip()

ap = argparse.ArgumentParser()
ap.add_argument('-i', '--image', required=True,
                help="path to input image to be OCR'd")
ap.add_argument('-l', '--langs', type=str, default='en',
                help='comma separated list of languages to OCR')
ap.add_argument('-g', '--gpu', type=int, default=-1,
                help='whether or not GPU should be used')
args = vars(ap.parse_args())

langs = args['langs'].split(',')
print ("[INFO] OCR'ing with the following languages: {}".format(langs))

image = cv2.imread(args['image'])

print ("[INFO] OCR'ing input image...")
reader = Reader(langs, gpu=args['gpu'] > 0)
results = reader.readtext(image)

for (bbox, text, prob) in results:

    print ('[INFO] {:.4f}: {}'.format(prob, text))

    (tl, tr, br, bl) = bbox
    tl = (int(tl[0]), int(tl[1]))
    tr = (int(tr[0]), int(tr[1]))
    br = (int(br[0]), int(br[1]))
    bl = (int(bl[0]), int(bl[1]))

    text = cleanup_text(text)
    cv2.rectangle(image, tl, br, (0, 255, 0), 2)
        (tl[0], tl[1] - 10),
        (0, 255, 0),

cv2.imshow('Image', image)
Richard MacCutchan 9-Oct-20 6:02am    
What is the problem?
nagarjunanethaji 9-Oct-20 7:51am    
I could not able to extract Upper and Lower MRZ data
OriginalGriff 9-Oct-20 8:08am    
He's trying to read information from driving licences, passports, and so on.
I suspect nefarious deeds would be done, if only he had the skills and knowledge ... :D
Dave Kreskowiak 9-Oct-20 13:31pm    
Well, in this case, the MRZ is the "Machine Readable Zone", so he's good there. He's not going to be able to decipher what all of the data means 'cause not all of it is publicly documented, but the basic stuff is there, like the holders name.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900