Python AttributeError: 'NoneType' 对象没有属性 'shape'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28894060/
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 03:52:10  来源:igfitidea点击:

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

pythonopencv

提问by AK47

import numpy as np 
import cv2
from matplotlib import pyplot as plt

img = cv2.imread('AB.jpg')
mask = np.zeros(img.shape[:2] , np.uint8) 

bgdModel = np.zeros((1,65), np.float64)
fgdModel = np.zeros((1,65), np.float64)

rect = (300 , 120 , 470 , 350)

#this modifies mask
cv2.grabCut(img,mask,rect,bgdModel, fgdModel , 5 , cv2.GC_INIT_WITH_RECT)

#If mask==2 or mask==1 , mask2 get 0, otherwise it gets 1 as 'uint8' type
mask2 = np.where((mask==2) | (mask==0),0,1).astype('uint8')

#adding additional dimension for rgb to the mask, by default it gets 1
#multiply with input image to get the segmented image
img_cut = img*mask2[: , : , np.newaxis]

plt.subplot(211),plt.imshow(img)
plt.title('Input Image') , plt.xticks([]),plt.yticks([])
plt.subplot(212),plt.imshow(img_cut)
plt.title('Grab cut'), plt.xticks([]),plt.yticks([])
plt.show()

on compiling I get this error :

编译时出现此错误:

python img.py AB.jpg
Traceback (most recent call last):
File "img.py", line 6, in <module>
mask = np.zeros(img.shape[:2] , np.uint8) 
AttributeError: 'NoneType' object has no attribute 'shape'

回答by Emanuele Cipolla

First of all

首先

python img.py AB.jpg

will not work as you expect (load AB.jpg from the current directory). The file to load is hardcoded in line 6 of the provided example: to work as intended, it should be something like this:

不会按预期工作(从当前目录加载 AB.jpg)。要加载的文件在提供的示例的第 6 行中进行了硬编码:要按预期工作,它应该是这样的:

import sys
img = cv2.imread(sys.argv[1])

The error is returned because AB.jpg does not exist in the directory img.py is being run from (the current working directory), and there is no verification for a missing file before trying to read it.

返回错误是因为在运行 img.py 的目录(当前工作目录)中不存在 AB.jpg,并且在尝试读取之前没有验证丢失的文件。

回答by I.Newton

Answering, because the community brought it back. Adding my two objects (cents).

回答,因为社区把它带回来了。添加我的两个对象(美分)。

The only reason you see that error is because you are trying to get information or perform operations on an object that doesn't exist in the first place. To check, try printing the object. Like, adding -

您看到该错误的唯一原因是您试图获取信息或对最初不存在的对象执行操作。要检查,请尝试打印对象。喜欢,添加 -

print img      # such as this case
print contours # if you are working with contours and cant draw one
print frame    # if you are working with videos and it doesn't show

gives you a None. That means you haven't read it properly. Either the image name you gave does not exist or the path to it is wrong. If you find such an error here's the quick things to do-

给你一个 None。这意味着你没有正确阅读它。您提供的图像名称不存在或路径错误。如果你发现这样的错误这里是快速的事情做 -

  1. Check the path or bring your image to the working directory
  2. Check the name you gave is right (including the extension- .jpg, .png etc)
  3. Put the entire code in a if statement with the object and the code to proceed if true
  4. Will add more if suggested in the comments.
  1. 检查路径或将您的图像带到工作目录
  2. 检查您提供的名称是否正确(包括扩展名 - .jpg、.png 等)
  3. 将整个代码与对象和代码放在 if 语句中,如果为真则继续
  4. 如果在评论中提出建议,将添加更多。