首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么没有定义selectedimage?

为什么没有定义selectedimage?
EN

Stack Overflow用户
提问于 2020-01-27 12:53:03
回答 2查看 53关注 0票数 0

我一直在尝试确定为什么,当我运行程序时,当我查看代码时,它似乎是定义的,而选择的图像却没有定义。

有问题的错误是:

代码语言:javascript
复制
NameError: name 'selectedimage' is not defined

我正在使用streamlit,一个在线GUI生成器。

代码语言:javascript
复制
if st.sidebar.button("Identify"):
    selectedimage = "./AI_TESTING_DATA/" + imageselect
    selectedimage = Image.open(selectedimage).resize(IMAGE_SHAPE)

selectedimage = np.array(selectedimage) / 255.0

result = model.predict(selectedimage[np.newaxis, ...])

predicted_class = np.argmax(result[0], axis=-1)

labels_path = "./Product/labels.txt"
class_labels = np.array(open(labels_path).read().splitlines())

predicted_class_name = class_labels[predicted_class]

"It's a :" + predicted_class_name
EN

回答 2

Stack Overflow用户

发布于 2020-01-27 12:57:27

if条件if st.sidebar.button("Identify"):失败,因此未声明selectedimage,因此您在selectedimage = np.array(selectedimage) / 255.0行中遇到错误

如果您的if condition是正确的,那么检查st.sidebar.button("Identify")的值。它将是False

票数 0
EN

Stack Overflow用户

发布于 2020-01-30 07:09:00

如果If子句的计算结果为False,那么selectedimage将是未定义的。看起来您可能希望将整个逻辑转移到if-子句下,因为这完全取决于所单击的按钮:

代码语言:javascript
复制
if st.sidebar.button("Identify"):
    selectedimage = "./AI_TESTING_DATA/" + imageselect
    selectedimage = Image.open(selectedimage).resize(IMAGE_SHAPE)

    selectedimage = np.array(selectedimage) / 255.0

    result = model.predict(selectedimage[np.newaxis, ...])

    predicted_class = np.argmax(result[0], axis=-1)

    labels_path = "./Product/labels.txt"
    class_labels = np.array(open(labels_path).read().splitlines())

    predicted_class_name = class_labels[predicted_class]

    "It's a :" + predicted_class_name
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59925286

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档