[python] OpenCV NoneType object has no attribute shape

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?

This question is related to python opencv raspberry-pi2

The answer is


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!


I also face the same issue "OpenCV NoneType object has no attribute shape" and i solve this by changing the image location. I also use the PyCharm IDE. Currently my image location and class file in the same folder. enter image description here


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


I just meet a same problem. I solve it by updating the newest version of OpenCV. It works well with me. Hope it is also ok with you.


I work with artificially created images,i.e. I create them by myself and then train a neural network on them to perform a certain task. So, I created these images, saved them, but when I tried to open them ( with cv2.imread(...)), I got this error.It turned out that when saving artificially created images you need to add dtype=np.uint8. That resolved the issue for me!


try to handle the error, its an attribute error given by OpenCV

try:
    img.shape
    print("checked for shape".format(img.shape))
except AttributeError:
    print("shape not found")
    #code to move to next frame

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)

I had this issue with cap = cv2.VideoCapture(0). I changed this to cap = cv2.VideoCapture(1) and then it worked. Since it wasn't linked to the right webcam it was returning nothing. Maybe this will help good luck.


I had the same problem. I had another program open that was using my laptop's camera. So I closed that program, and then everything worked. I found this answer by checking https://howto.streamlabs.com/streamlabs-obs-9/black-screen-when-using-video-capture-device-elgato-hd-60s-9508.


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.


This is because the path of image is wrong or the name of image you write is incorrect .

how to check? first try to print the image using print(img) if it prints 'None' that means you have given wrong image path correct that path and try again.


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