Home > Net >  "only integer scalar arrays can be converted to a scalar index" prediction_name = mapping[
"only integer scalar arrays can be converted to a scalar index" prediction_name = mapping[

Time:05-05

Can Someone help me fix this. I keep getting error since this is my last one code to running

Code:

from tkinter import messagebox 
from tkinter import *
from tkinter import simpledialog 
import tkinter
from tkinter import filedialog
from tkinter.filedialog import askopenfilename 
import numpy 
import keras 
import cv2
import matplotlib.pyplot as plt
from keras.models import load_model 
from keras import models
from keras import layers 
from tensorflow.keras import optimizers 
main = tkinter.Tk()
main.title("Alzeimers prediction") 
main.geometry("1300x1200")
mapping = ['MildDemented', 'ModerateDemented', 'NonDemented', 'VeryMildDemented'] 
def load():
    global model
    model = load_model('model_final.h5') 
    model.summary() 
    model.compile(loss='categorical_crossentropy',
                optimizer=optimizers.RMSprop(learning_rate=1e-4), 
                metrics=['acc'])

def upload():
    text.delete('1.0',END) 
    global filename
    filename = askopenfilename()
    text.insert(END,"File Uploaded: " str(filename) "\n") 
    
def imagepreprocess():
    global img4
    img3 = cv2.imread(filename)
    img3 = cv2.cvtColor(img3, cv2.COLOR_BGR2RGB) 
    img3 = cv2.resize(img3,(224,224))
    img4 = np.reshape(img3,[1,224,224,3]) 
    
def predict():
    disease = model.predict(img4) 
    prediction = disease[0] 
    prediction_name = mapping[prediction]
    text.insert(END,"Predicted output for uploaded Image: " str(prediction_name) "\n") 

font = ('times', 16, 'bold')
title = Label(main, text='Alzeimers Prediction From MRI Images') 
title.config(bg='dark salmon', fg='black')
title.config(font=font) 
title.config(height=3, width=120) 
title.place(x=0,y=5)
font1 = ('times', 14, 'bold')
upload = Button(main, text="Upload Image", command=upload) 
upload.place(x=700,y=100)
upload.config(font=font1)
process = Button(main, text="Image Pre-Processing", command=imagepreprocess) 
process.place(x=700,y=150)
process.config(font=font1)
ld = Button(main, text="Model Load", command=load)
ld.place(x=700,y=200) 
ld.config(font=font1)
pred = Button(main, text="Prediction", command=predict) 
pred.place(x=700,y=250)
pred.config(font=font1) 
font1 = ('times', 12, 'bold')
text=Text(main,height=30,width=80) 
scroll=Scrollbar(text) 
text.configure(yscrollcommand=scroll.set) 
text.place(x=10,y=100) 
text.config(font=font1) 
main.config(bg='tan1')
main.mainloop()

I change

prediction_name = mapping[prediction]

to this

mapping[[int(x) for x  in prediction]]

Error:

Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\WIN10\anaconda3\envs\keras_env\lib\tkinter_init_.py", line 1892, in call return self.func(*args) File "C:\Users\WIN10\AppData\Local\Temp\ipykernel_10416\3265545010.py", line 43, in predict prediction_name = mapping[prediction] TypeError: only integer scalar arrays can be converted to a scalar index

Then new error Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\WIN10\anaconda3\envs\keras_env\lib\tkinter_init_.py", line 1892, in call return self.func(*args) File "C:\Users\WIN10\AppData\Local\Temp\ipykernel_10416\3870737578.py", line 43, in predict mapping[[int(x) for x in prediction]] TypeError: list indices must be integers or slices, not list

Which code i need to convert elements

CodePudding user response:

The error states that you are trying to use a non-integer array as a way to index your mapping array. It is only possible to use interger arrays to do indexing, for example [1,0,1,2,4]. Here I think you have something like [1.0,0.0,1.0,2.0,4.0]. Try the following to convert your elements to integers instead of floats:

mapping[[int(x) for x  in prediction]]

in your predict function.

EDIT: Your mapping list cannot be indexed by a list of indices since the list object does not allow this kind of indexing. To solve the new error, you can convert it to a numpy array when you define it:

mapping = numpy.array(['MildDemented', 'ModerateDemented', 'NonDemented', 'VeryMildDemented'])

Numpy arrays allow the indexing by a list of indices.

Also, please do not forget to keep the prediction_name definition in the code you changed:

prediction_name = mapping[[int(x) for x  in prediction]]
  • Related