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?
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.
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!
You probably get the error because your video path may be wrong in a way. Be sure your path is completely correct.
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
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.
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.
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
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!
来源:https://stackoverflow.com/questions/39833796/opencv-nonetype-object-has-no-attribute-shape