Home > Software engineering >  How to print prediction by category inside a loop
How to print prediction by category inside a loop

Time:09-17

I'm having a problem with printing pictures with labels of prediction in my project.

i = 1
for image in DATADIR:
    prediction = model.predict
    ([prepare(r'MY_DIR\manual_testing\{}.jpg'.format(i))])
    img = mpimg.imread(r'MY_DIR\manual_testing\{}.jpg'.format(i))
    imgplot = plt.imshow(img)
    plt.show()
    print(CATEGORIES[int(prediction[0][0])])
    i  = 1

Here MY_DIR replaces the actual directory. I'm having the following error: TypeError: 'method' object is not subscriptable

I don't really understand what should be changed. If I try to put print('Hello world') in the for loop everything is working.

p.s. If you have an example of how to make the output look more beautiful you are welcome.

Thank you in advance.

CodePudding user response:

I found the solution:

i = 1
for item in os.listdir(DATADIR):
    prediction = model.predict([prepare(r'MY_DIR\manual_testing\{}.jpg'.format(i))])
    img = mpimg.imread(r'MY_DIR\manual_testing\{}.jpg'.format(i))
    imgplot = plt.imshow(img)
    plt.show()

    print(CATEGORIES[int(prediction[0][0])])
    i  = 1 
  • Related