Search code examples
pythonneural-networkpytorchface-detectionface-recognition

'NoneType' object has no attribute 'size' - how to face detection using mtcnn?


i'm trying to build face recognition in real time in neural network(facenet network) using pytorch and face detection using MTCNN i've tried this for detecting faces in real time (from webcam) but doesnt work read frames then going through mtcnn detector

import cv2  
capture = cv2.VideoCapture(0)  
while(True):      
   ret, frame = capture.read()     
   frames_tracked = []      
   print('\rTracking frame: {}'.format(i + 1), end='')     
   boxes,_ = mtcnn.detect(frame)     
   frame_draw = frame.copy()     
   draw = ImageDraw.Draw(frame_draw)     
   for box in boxes:
       draw.rectangle(box.tolist(), outline=(255, 0, 0), width=6)          
       frames_tracked.append(frame_draw.resize((640, 360), Image.BILINEAR))      
   d = display.display(frames_tracked[0], display_id=True)     
   i = 1     
   try:        
      while True:
          d.update(frames_tracked[i % len(frames_tracked)]) 
          i += 1     
   except KeyboardInterrupt:
      pass  
   if cv2.waitKey('q') == 27:     
     break  

  capture.release() 
  cv2.destroyAllWindows()

but it will rise this error :

this is the entire traceback http://dpaste.com/0HR58RQ

AttributeError: 'NoneType' object has no attribute 'size'

is there a solution for this problem ? what caused this error? thanks for your advice


Solution

  • Let's take a look at that error again.

    AttributeError: 'NoneType' object has no attribute 'size'
    

    so, somewhere in your code you(or mtcnn) are trying to call size attribute from a None variable. you are passing frame to mtcnn using following command :

     boxes,_ = mtcnn.detect(frame)
    

    this is exactly where you see that error. because you are passing a None variable to mtcnn. in order to prevent it, you can prevent it before calling this method. in other words :

    ret, frame = capture.read()
    if frame == None:
        continue