首页 > 解决方案 > 如何解决“未定义名称 xxx”错误

问题描述

我正在尝试按照此链接中的说明进行操作: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

不知道我需要从这里去哪里。提前致谢。

标签: pythonscripting

解决方案


因为 Python 关心其代码块的缩进,所以它将您的for循环视为函数之外的单独代码块。如果您从 for start 到 return 缩进所有内容,则应该修复它。


推荐阅读