0

我一直试图确定为什么,当我运行程序时,它说未定义所选图像,而当我查看代码时似乎已定义。

有问题的错误是:

NameError: name 'selectedimage' is not defined

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

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
4

2 回答 2

0

if 条件if st.sidebar.button("Identify"):失败,因此selectedimage没有被声明,因此您面临selectedimage = np.array(selectedimage) / 255.0在线错误

如果你if condition是对的,那么检查st.sidebar.button("Identify"). 这将是False

于 2020-01-27T04:57:27.097 回答
0

如果您的 if 子句评估为 False,那么selectedimage将是未定义的。看起来您可能想在该 if 子句下移动整个其余逻辑,因为这完全取决于单击的按钮:

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
于 2020-01-29T23:09:00.430 回答