简体   繁体   English

TypeError: unhashable type: 'numpy.ndarray' Python3.9 图像分类使用 tensorflow 和 keras

[英]TypeError: unhashable type: 'numpy.ndarray' Python3.9 image classification using tensorflow and keras

I try this sample code for image classification我尝试使用此示例代码进行图像分类

def show_classify_button(file_path):
    classify_btn = Button(top, text="Classify Image", command=lambda: classify(file_path), padx=10, pady=5)
    classify_btn.configure(background="#364156", foreground="white", font=('arial',10,'bold'))
    classify_btn.place(relx=0.79,rely=0.46)

def classify(file_path):
    image = Image.open(file_path)
    image = image.resize((32,32))
    image = numpy.expand_dims(image, axis=0)
    image = numpy.array(image)
    pred = model.predict([image])[0]
    sign = classes[pred]
    print(sign)
    label.configure(foreground='#011638')

the terminal pop this终端弹出这个

Traceback (most recent call last):
  line 39, in <lambda>
    classify_btn = Button(top, text="Classify Image", command=lambda: classify(file_path), padx=10, pady=5)
  line 49, in classify
    sign = classes[pred]
TypeError: unhashable type: 'numpy.ndarray'

I try to check the data from the pred with output我尝试使用 output 检查来自 pred 的数据

[30990.06  46435.57  17636.973 16334.658 15860.342 16765.371 26879.748
 14579.97  41989.523 34359.215]

im not sure why because the data is from set of an array我不知道为什么,因为数据来自一组数组

im new with this and im using python3.9 can someone help me我是新手,我正在使用 python3.9 有人可以帮助我吗

You're trying to access classes variable on line 49您正在尝试访问第 49 行的 classes 变量

sign = classes[pred]

classes is of type numpy.ndarray .类的类型是numpy.ndarray So you're trying to access an array at index pred but because pred is not an number it's raising a unhashable type: 'numpy.ndarray' error.因此,您尝试访问索引为pred的数组,但由于pred不是数字,因此引发了unhashable type: 'numpy.ndarray'错误。

You're treating classes like a dictionary by accessing it's values with a key and not with an index.您通过使用键而不是索引来访问类的值,从而将类视为字典。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM