22

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?

user3748265
  • 379
  • 1
  • 2
  • 7

15 Answers15

26

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.

asc11
  • 419
  • 3
  • 7
11

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!

Flair
  • 2,609
  • 1
  • 29
  • 41
Deepak V
  • 299
  • 2
  • 15
7

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

6

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

aysebilgegunduz
  • 790
  • 12
  • 26
3

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)
jdhao
  • 24,001
  • 18
  • 134
  • 273
2

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
Sir Tesla
  • 320
  • 2
  • 10
1

Try to give a correct path to the image/video like this below. Also Opencv doesn't support image paths which contain unicode characters so try this code below

cv2.imread(r'path_to_image/video_with_file_extension', flag)
blackbishop
  • 30,945
  • 11
  • 55
  • 76
0

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.

sachinsaini
  • 53
  • 1
  • 10
0

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.

  • (This post does not seem to provide a [quality answer](https://stackoverflow.com/help/how-to-answer) to the question. Please either edit your answer and improve it, or just post it as a comment to the question.) – sɐunıɔןɐqɐp Oct 01 '18 at 07:17
0

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!

0

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

Md. Shofiulla
  • 2,135
  • 1
  • 13
  • 19
0

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.

yishairasowsky
  • 741
  • 1
  • 7
  • 21
0

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.

ah bon
  • 9,293
  • 12
  • 65
  • 148
cjHerold
  • 31
  • 4
0

I was getting this error when trying to find the size of a photo.

Exception has occurred: AttributeError 'NoneType' object has no attribute 'shape'

After googling for like 10 mins I came across this link OpenCV: Resolving NoneType errors by Adrian Rosebrock. In that article he mentioned

An invalid image path passed to cv2.imread

While this did fix my issue I hope it fixes yours. If not I hope you can find something of use on that page as well

ath0rus
  • 83
  • 1
  • 9
0

I have faced same error once i worked on my simple object tracking project. The Python AttributeError: 'NoneType' object has no attribute 'shape' occurs after passing an incorrect path to cv2.imread() because the path of image/video file is wrong or the name of image/video you passed is incorrect. To solve the error, make sure to specify the correct path. To check path exists or not try below code

import os

print(os.path.exists('your-file-path'))

I hope this may helpful to fix your code issue.
Kindly Click here for more details.

Saifu
  • 108
  • 1
  • 8