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 but When I Compile It, I Get an Error: 'Nonetype' Object...
Hello I'm working on Raspberry Pi with OpenCV. I want to try a tutorial which is ball tracking in link
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.
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 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)
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.
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!
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.
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 .
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 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
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)
The main issue behind that is the link to your images, make sure you provide the correct path or put the image in the same folder of the code for a better and easier run. I solved the issue in this way.