‘NoneType’ object has no attribute ‘to_dict’

Tags: , , ,



I’am quite new to python and I am developing a passport scanner with a RaspberryPi and the passport eye module. In the end it prints outs the outcome of the scanned image. However, I also want to print an outcome if the scanner isn’t able tot detect a MRZ code.

I tried some things but couldn’t figure it out, here is a example in short:

mrz = read_mrz("test.jpg")
mrz_data = mrz.to_dict()

if mrz.to_dict == None:
 print("Invalid document")

else:
 print(mrz_data["names"])

Error in mrz_data = mrz.to_dict() AttributeError: ‘NoneType’ object has no attribute ‘to_dict’

EDIT: Is there a way to print when the error is ‘NoneType’ object has no attribute ‘to_dict’

Answer

From the official docs

The returned object (unless it is None, which means no ROI was detected) contains the fields extracted from the MRZ along with some metainformation

Looks like the image that you are using returns None. In line 2 when you call the to_dict() on None, it throws the exception 'NoneType' object has no attribute 'to_dict'

This can be fixed as follows

if mrz == None:
 print("Invalid document")

else:
 print(mrz.to_dict()["names"])


Source: stackoverflow