如何解决'name xxx is not defined'错误



我正在尝试按照此链接中的说明进行操作:https://towardsdatascience.com/image-detection-from-scratch-in-keras-f314872006c9

到目前为止,我可以用自己的图像进行设置:

def read_and_process_images(train_images):
X = [] #images
Y = [] #labels
for image in train_images:
X.append(cv2.resize(cv2.imread(image, cv2.imread_grayscale), (nrows, ncolumns), interpolation=cv2.inter_cubic)) #read the images
#get the labels
if 'Pass' in image:
Y.append(1)
elif 'Fail' in image:
Y.append(0)
return X, Y

我得到以下错误:

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-47-cb61405b8d99> in <module>()
4 
5 for image in train_images:
----> 6     X.append(cv2.resize(cv2.imread(image, cv2.imread_grayscale), (nrows, ncolumns), interpolation=cv2.inter_cubic)) #read the images
7     #get the labels
8     if 'Pass' in image:
NameError: name 'X' is not defined

不确定我该去哪里。提前谢谢。

因为Python关心其代码块的缩进,所以它将for循环视为函数之外的单独代码块。如果您从for开始缩进所有内容,直到返回为止,它应该是固定的。

相关内容

最新更新