python - 如何解决“未定义名称 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
不知道我需要从这里去哪里。提前致谢。
解决方案
因为 Python 关心其代码块的缩进,所以它将您的for
循环视为函数之外的单独代码块。如果您从 for start 到 return 缩进所有内容,则应该修复它。
推荐阅读
- php - 发送 cURL 请求 php 时 cookie 休息
- ansible - Ansible oc 模块内联定义
- javascript - 未找到隧道 URL,回退到 LAN URL
- java - 在 Java 中从 Excel 与 DB 中读取数据
- mysql - MySQL- 类似 Timeline 的 Twitter 数据库设计
- mysql - 每 5 分钟更新一次值
- sql-server - 如何设计我的数据仓库来处理迟到的数据源?
- flutter - Flutter保留英雄内部CustomScrollView的滚动位置
- angular - 'MatTableDataSource' 类型上不存在属性 'loading$'
- r - 如何从 R extrafontdb 中注销已删除的字体