Home > OS >  TypeError: unhashable type: 'numpy.ndarray' Python3.9 image classification using tensorflo
TypeError: unhashable type: 'numpy.ndarray' Python3.9 image classification using tensorflo

Time:12-21

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

[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

CodePudding user response:

You're trying to access classes variable on line 49

sign = classes[pred]

classes is of type 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.

You're treating classes like a dictionary by accessing it's values with a key and not with an index.

  • Related