Python OpenCV NoneType 对象没有属性形状

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39833796/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 22:46:54  来源:igfitidea点击:

OpenCV NoneType object has no attribute shape

pythonopencvraspberry-pi2

提问by user3748265

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/

您好,我正在使用 OpenCV 开发 Raspberry Pi。我想尝试一个教程,它是链接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'.

但是当我编译它时,我收到一个错误:'NoneType' 对象没有属性 'shape'。

What should I do?

我该怎么办?

回答by asc11

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.

这意味着应该返回图像的函数在某处返回 None ,因此没有 shape 属性。尝试“打印 img”以检查您的图像是 None 还是实际的 numpy 对象。

回答by Deepak V

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.

我今天遇到了同样的问题,请检查cybseccrypt提到的图像路径。imread 后,尝试打印图像并查看。如果您得到一个值,则表示该文件已打开。

Code:

代码:

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

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

Hope this helps!

希望这可以帮助!

回答by aysebilgegunduz

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

您可能会收到错误消息,因为您的视频路径可能在某种程度上是错误的。确保您的路径完全正确。

回答by Sir Tesla

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

尝试处理错误,它是 OpenCV 给出的属性错误

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

回答by sachinsaini

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.

如何检查?首先尝试使用 print(img) 打印图像,如果它打印“无”,则意味着您提供了错误的图像路径,更正该路径,然后重试。

回答by Son Tran Hoang

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.

我只是遇到同样的问题。我通过更新最新版本的 OpenCV 来解决它。它对我很有效。希望你也没事。

回答by Santhosh Dhaipule Chandrakanth

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'

'NoneType' object has no attribute 'shape'

Make sure to add assertafter loading the image/frame

确保在加载assert后添加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

帮我解决了一个类似的问题,在一个很长的脚本中

回答by Khelina Fedorchuk

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 artificiallycreatedimages you need to add dtype=np.uint8. That resolved the issue for me!

我使用人工创建的图像,即我自己创建它们,然后在它们上训练神经网络以执行特定任务。所以,我创建了这些图像,保存了它们,但是当我尝试打开它们时(使用 cv2.imread(...)),我得到了这个错误。原来,在保存人工创建的图像时,你需要添加dtype=np .uint8。这为我解决了问题!

回答by Md. Shofiulla

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

我也面临同样的问题“OpenCV NoneType 对象没有属性形状”,我​​通过更改图像位置来解决这个问题。我也使用 PyCharm IDE。目前我的图像位置和类文件在同一个文件夹中。在此处输入图片说明

回答by jdhao

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:

接下来,您应该知道Opencv 不支持包含 unicode 字符的图像路径(请参阅ref)。如果您的图片路径包含 Unicode 字符,您可以使用以下代码读取图片:

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)