如何修复'AttributeError:'NoneType'对象在python中没有属性'get'错误

时间:2019-06-21 13:27:28

标签: python opencv image-processing

我尝试使用一个代码来计算运动对象的距离,该代码使用两个网络摄像机的视频源进行计算。我遇到了这个错误

  

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

当我运行代码时。我该如何解决?

def next(self,black=True,wait=0):
    # black frame default
    if black:
        frame = self.black_frame

    # no frame default
    else:
        frame = None

    # get from buffer (fail if empty)
    try:
        frame = self.buffer.get(timeout=wait)
        self.frames_returned += 1
    except queue.Empty:
        print('Queue Empty!')
        #print(traceback.format_exc())
        pass

    # done
    return frame

2 个答案:

答案 0 :(得分:0)

您得到的错误说明了一切,

     frame = self.buffer.get(timeout=wait)

缓冲区为空,或者您没有在构造函数中定义缓冲区,或者在此函数之前将其设置为None

答案 1 :(得分:0)

您可以添加一个if else块:

if self.buffer: # This will test if the buffer is not None
    # Do access the get method. 
else: 
    # Say buffer is None or do something else. 

更明确地:

if self.buffer is not None:
    # Do access the get method. 
else: 
    # Say buffer is None or do something else.