Skip to content
Advertisement

Attribute Error : none type object has no attribute ‘ shape ‘

I keep encountering an attribute error trying to read some frames from a piCamera on a raspberry pi

Here is the error:

Traceback (most recent call last):
  File "/home/pi/ball-tracking/ball_tracking.py", line 48, in <module>
    frame = imutils.resize(frame, width=600)
  File "/usr/local/lib/python2.7/dist-packages/imutils/convenience.py", line 45, in resize
    (h, w) = image.shape[:2]
AttributeError: 'NoneType' object has no attribute 'shape'


if not args.get("video", False):
    camera = cv2.VideoCapture(0)

else:
    camera = cv2.VideoCapture(args["video"])

while True:
    # grab the current frame
    (grabbed, frame) = camera.read()


    if args.get("video") and not grabbed:
        break

   

Advertisement

Answer

It seems that frame was returned as None in this line as if you camera couldn’t read an image:

(grabbed, frame) = camera.read()

Then, when resizing a None object, the program blows up as we described in the error message AttributeError: 'NoneType' object has no attribute 'shape':

frame = imutils.resize(frame, width=600)

As discussed in this thread, some camera drivers may return False, None in the first frame. A possible workaround would be to verify whether grabbed is False and ignore this frame.

while True:
    grabbed, frame = camera.read()

    if not grabbed:
        continue

    # the rest of the program
Advertisement