'NoneType' 对象没有属性 'to_dict'

'NoneType' object has no attribute 'to_dict'

我是 python 的新手,我正在开发带有 RaspberryPi 和护照眼模块的护照扫描仪。最后它打印出扫描图像的结果。但是,如果扫描仪无法检测到 MRZ 代码,我也想打印结果。

我尝试了一些东西但无法弄清楚,这里有一个简短的例子:

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'

编辑:当错误是 'NoneType' object has no attribute 'to_dict'

时,有没有办法打印

来自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

看起来像您正在使用的图像 returns None。在第 2 行中,当您在 None 上调用 to_dict() 时,它会抛出异常 'NoneType' object has no attribute 'to_dict'

这可以按如下方式解决

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

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