konstantint / PassportEye

Extraction of machine-readable zone information from passports, visas and id-cards via OCR
MIT License
374 stars 109 forks source link

MRZ decoding fails with AttributeError: 'NoneType' object has no attribute 'shape' in image.py+68 #59

Open petaflot opened 2 years ago

petaflot commented 2 years ago

following some example, I put together this code:

#!/usr/bin/python                                                                                                                                                                                                                             

# original source: https://qiita.com/PoodleMaster/items/0afbce4be7e442e75be6                                                                                                                                                                           
import cv2                                                                                                                                                                                                                                    
#from pyzbar.pyzbar import decode, ZBarSymbol                                                                                                                                                                                                 
from passporteye import read_mrz                                                                                                                                                                                                              

VIDEO_DEVICE = 0        # can be an index or device path, eg /dev/video1                                                                                                                                                                      

font = cv2.FONT_HERSHEY_SIMPLEX                                                                                                                                                                                                               

def readMRZ( _set = None ):                                                                                                                                                                                                                   
        cap = cv2.VideoCapture(VIDEO_DEVICE, cv2.CAP_DSHOW)                                                                                                                                                                                   
        cap.open(VIDEO_DEVICE)                                                                                                                                                                                                                
        print("device open")                                                                                                                                                                                                                  

        try:                                                                                                                                                                                                                                  
                while cap.isOpened():                                                                                                                                                                                                         
                        print("reading...")                                                                                                                                                                                                   
                        ret, frame = cap.read()                                                                                                                                                                                               

                        print("processing...")                                                                                                                                                                                                
                        if ret:                                                                                                                                                                                                               
                                value = read_mrz(frame)                                                                                                                                                                                       

                                if value:                                                                                                                                                                                                     
                                        print(value)

                        if cv2.waitKey(1) & 0xFF == ord('q'):                                                                                                                                                                                 
                                break                                                                                                                                                                                                         
        except KeyboardInterrupt:                                                                                                                                                                                                             
                return                                                                                                                                                                                                                        
        finally:                                                                                                                                                                                                                              
                cap.release()                                                                                                                                                                                                                 

if __name__ == '__main__':                                                                                                                                                                                                                    
        from time import sleep                                                                                                                                                                                                                
        while True:                                                                                                                                                                                                                           
                print(readMRZ())                                                                                                                                                                                                              
                sleep(1)               

the code loops quietly until I show a document with MRZ i front of the camera... then it crashes:

Traceback (most recent call last):
  File "/opt/porte_auto_engrenage/webcam_mrz.py", line 40, in <module>
    print(readMRZ())
  File "/opt/porte_auto_engrenage/webcam_mrz.py", line 25, in readMRZ
    value = read_mrz(frame)
  File "/usr/local/lib/python3.9/dist-packages/passporteye/mrz/image.py", line 355, in read_mrz
    mrz = p.result
  File "/usr/local/lib/python3.9/dist-packages/passporteye/mrz/image.py", line 343, in result
    return self['mrz_final']
  File "/usr/local/lib/python3.9/dist-packages/passporteye/util/pipeline.py", line 102, in __getitem__
    self._compute(key)
  File "/usr/local/lib/python3.9/dist-packages/passporteye/util/pipeline.py", line 109, in _compute
    self._compute(d)
  File "/usr/local/lib/python3.9/dist-packages/passporteye/util/pipeline.py", line 109, in _compute
    self._compute(d)
  File "/usr/local/lib/python3.9/dist-packages/passporteye/util/pipeline.py", line 109, in _compute
    self._compute(d)
  [Previous line repeated 1 more time]
  File "/usr/local/lib/python3.9/dist-packages/passporteye/util/pipeline.py", line 111, in _compute
    results = self.components[cname](*inputs)
  File "/usr/local/lib/python3.9/dist-packages/passporteye/mrz/image.py", line 68, in __call__
    scale_factor = self.max_width / float(img.shape[1])
AttributeError: 'NoneType' object has no attribute 'shape'

I tried to debug it myself, but got stuck failing to understand the relationship between pipeline.py line 111 in _compute and image.py line 68 in call.

on line 111 and just before the crash, inputs is [None].

So far, I haven't been able to successfully decode a single MRZ

WIhen calling read_mrz('100_id-rou.jpg') with an image path instead of reading from the cam,

or with

f = open('100_id-rou.jpg','rb')
mrz = read_mrz(f)

I get

  File "/usr/local/lib/python3.9/dist-packages/skimage/io/_plugins/imageio_plugin.py", line 10, in imread                                                                                                                                     
    return np.asarray(imageio_imread(*args, **kwargs))                                                                                                                                                                                        
  File "/usr/local/lib/python3.9/dist-packages/imageio/core/functions.py", line 159, in imread                                                                                                                                                
    with imopen(uri, "ri", plugin=format) as file:                                                                                                                                                                                            
  File "/usr/local/lib/python3.9/dist-packages/imageio/core/imopen.py", line 277, in imopen                                                                                                                                                   
    raise err_type(err_msg)                                                                                                                                                                                                                   
ValueError: Could not find a backend to open `100_id-rou.jpg`` with iomode `ri`.

If Instead I simply write

f = open('100_id-rou.jpg')
mrz = read_mrz(f)

the result (with little surprise) is

  File "/usr/local/lib/python3.9/dist-packages/passporteye/util/pipeline.py", line 109, in _compute                                                                                                                                           
    self._compute(d)                                                                                                                                                                                                                          
  [Previous line repeated 2 more times]                                                                                                                                                                                                       
  File "/usr/local/lib/python3.9/dist-packages/passporteye/util/pipeline.py", line 111, in _compute                                                                                                                                           
    results = self.components[cname](*inputs)                                                                                                                                                                                                 
  File "/usr/local/lib/python3.9/dist-packages/passporteye/mrz/image.py", line 54, in __call__                                                                                                                                                
    return self._imread(self.file)                                                                                                                                                                                                            
  File "/usr/local/lib/python3.9/dist-packages/passporteye/mrz/image.py", line 37, in _imread                                                                                                                                                 
    img = skimage_io.imread(file, as_gray=self.as_gray, plugin='imageio')                                                                                                                                                                     
  File "/usr/local/lib/python3.9/dist-packages/skimage/io/_io.py", line 53, in imread                                                                                                                                                         
    img = call_plugin('imread', fname, plugin=plugin, **plugin_args)                                                                                                                                                                          
  File "/usr/local/lib/python3.9/dist-packages/skimage/io/manage_plugins.py", line 207, in call_plugin                                                                                                                                        
    return func(*args, **kwargs)                                                                                                                                                                                                              
  File "/usr/local/lib/python3.9/dist-packages/skimage/io/_plugins/imageio_plugin.py", line 10, in imread                                                                                                                                     
    return np.asarray(imageio_imread(*args, **kwargs))                                                                                                                                                                                        
  File "/usr/local/lib/python3.9/dist-packages/imageio/core/functions.py", line 159, in imread                                                                                                                                                
    with imopen(uri, "ri", plugin=format) as file:                                                                                                                                                                                            
  File "/usr/local/lib/python3.9/dist-packages/imageio/core/imopen.py", line 233, in imopen                                                                                                                                                   
    plugin_instance = config.plugin_class(request, **kwargs)                                                                                                                                                                                  
  File "/usr/local/lib/python3.9/dist-packages/imageio/config/plugins.py", line 108, in partial_legacy_plugin                                                                                                                                 
    return LegacyPlugin(request, legacy_plugin)                                                                                                                                                                                               
  File "/usr/local/lib/python3.9/dist-packages/imageio/core/legacy_plugin_wrapper.py", line 62, in __init__                                                                                                                                   
    if not self._format.can_read(request):                                                                                                                                                                                                    
  File "/usr/local/lib/python3.9/dist-packages/imageio/core/format.py", line 188, in can_read                                                                                                                                                 
    return self._can_read(request)                                                                                                                                                                                                            
  File "/usr/local/lib/python3.9/dist-packages/imageio/plugins/pillow_legacy.py", line 269, in _can_read                                                                                                                                      
    if request.firstbytes and accept(request.firstbytes):                                                                                                                                                                                     
  File "/usr/local/lib/python3.9/dist-packages/imageio/core/request.py", line 568, in firstbytes                                                                                                                                              
    self._read_first_bytes()                                                                                                                                                                                                                  
  File "/usr/local/lib/python3.9/dist-packages/imageio/core/request.py", line 588, in _read_first_bytes                                                                                                                                       
    self._firstbytes = read_n_bytes(f, N)                                                                                                                                                                                                     
  File "/usr/local/lib/python3.9/dist-packages/imageio/core/request.py", line 613, in read_n_bytes                                                                                                                                            
    bb += extra_bytes                                                                                                                                                                                                                         
TypeError: can't concat str to bytes 

However... since I haven't been able to decode anything I suppose I'm doing something wrong.. maybe someone has a clue?

furkankadioglu commented 1 year ago

Same here ✋