OpenCV NoneType object has no attribute shape

205,286

Solution 1

It means that somewhere a function which should return a image just returned None and therefore has no shape attribute. Try "print img" to check if your image is None or an actual numpy object.

Solution 2

I faced the same problem today, please check for the path of the image as mentioned by cybseccrypt. After imread, try printing the image and see. If you get a value, it means the file is open.

Code:

img_src = cv2.imread('/home/deepak/python-workout/box2.jpg',0)
print img_src

Hope this helps!

Solution 3

You probably get the error because your video path may be wrong in a way. Be sure your path is completely correct.

Solution 4

Hope this helps anyone facing same issue

To know exactly where has occurred, since the running program doesn't mention it as a error with line number

'NoneType' object has no attribute 'shape'

Make sure to add assert after loading the image/frame

For image

image = cv2.imread('myimage.png')
assert not isinstance(image,type(None)), 'image not found'

For video

cap = cv2.VideoCapture(0)

    while(cap.isOpened()):

        # Capture frame-by-frame
        ret, frame = cap.read()
        if ret:
            assert not isinstance(frame,type(None)), 'frame not found'

Helped me solve a similar issue, in a long script

Solution 5

I have also met this issue and wasted a lot of time debugging it.

First, make sure that the path you provide is valid, i.e., there is an image in that path.

Next, you should be aware that Opencv doesn't support image paths which contain unicode characters (see ref). If your image path contains Unicode characters, you can use the following code to read the image:

import numpy as np
import cv2

# img is in BGR format if the underlying image is a color image
img = cv2.imdecode(np.fromfile(im_path, dtype=np.uint8), cv2.IMREAD_UNCHANGED)
Share:
205,286
user3748265
Author by

user3748265

Updated on July 09, 2022

Comments

  • user3748265
    user3748265 almost 2 years

    Hello I'm working on Raspberry Pi with OpenCV. I want to try a tutorial which is ball tracking in link http://www.pyimagesearch.com/2015/09/14/ball-tracking-with-opencv/

    But when I compile it, i get an error: 'NoneType' object has no attribute 'shape'.

    What should I do?

  • sɐunıɔןɐqɐp
    sɐunıɔןɐqɐp over 5 years
    (This post does not seem to provide a quality answer to the question. Please either edit your answer and improve it, or just post it as a comment to the question.)
  • joanis
    joanis over 2 years
    This answer has been provided many times already. Please avoid posting duplicate answers.