【问题标题】:'NoneType' object has no attribute 'size' - how to face detection using mtcnn?“NoneType”对象没有“大小”属性 - 如何使用 mtcnn 进行人脸检测?
【发布时间】:2020-05-13 23:16:14
【问题描述】:

我正在尝试使用 pytorch 在神经网络(facenet 网络)中实时构建人脸识别,并使用 MTCNN 进行人脸检测 我试过这个来实时检测人脸(来自网络摄像头)但不起作用 读取帧然后通过 mtcnn 检测器

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()

但它会引发这个错误:

这是整个回溯http://dpaste.com/0HR58RQ

AttributeError: 'NoneType' 对象没有属性 'size'

这个问题有解决方案吗?是什么导致了这个错误?谢谢你的建议

【问题讨论】:

  • 我几乎可以肯定您实际上并没有阅读图像。尝试查看 retretframe 发生的事情,它们可能是空的
  • 你能发布回溯吗?
  • 你能在每次迭代时打印frame吗?我想查看此回溯之前的最后一个值。
  • 它可以在没有 (boxes,_ = mtcnn.detect(frame) ) 行的情况下工作,用整个回溯 @steviestickman 更新帖子

标签: python neural-network pytorch face-detection face-recognition


【解决方案1】:

让我们再看看那个错误。

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

所以,您(或 mtcnn)在您的代码中某处尝试从 None 变量调用 size 属性。您正在使用以下命令将frame 传递给mtcnn

 boxes,_ = mtcnn.detect(frame)

这正是您看到该错误的地方。因为您将 None 变量传递给mtcnn。为了防止它,您可以在调用此方法之前阻止它。换句话说:

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

【讨论】:

  • 感谢修复,但我又遇到了一个错误(RuntimeError: number of dims don't match in permute)
猜你喜欢
  • 2018-06-09
  • 2021-07-27
  • 1970-01-01
  • 2019-03-05
  • 2012-08-27
  • 2020-11-29
  • 2020-01-05
  • 2022-07-17
相关资源
最近更新 更多