Error message: An error occured! Try again. (Image Recognition)

Hello all,

I used the python code described in the tutorial “Train Image Recognition AI with 5 lines of code”. The training of my sample images worked fine but when I would like to do my first image recognition I receive the error message: ValueError: An error occured! Try again.

Attached please find the written code:

from imageai.Prediction.Custom import CustomImagePrediction
import os

execution_path = os.getcwd()

prediction = CustomImagePrediction()
prediction.setModelTypeAsResNet()

prediction.setModelPath(“model_ex-007_acc-0.250000.h5”)
prediction.setJsonPath(“model_class.json”)

prediction.loadModel(num_objects=7)

predictions, probabilities = prediction.predictImage(“test.jpg”, result_count=3)

for eachPrediction, eachProbability in zip(predictions, probabilities):

  • print(eachPrediction , " : " , eachProbability)*

The error message appears when I try to run the line starting with “predictions, probabilities…”.

The error message points to the following code line:

File “C:\ProgramData\Anaconda3\Lib\site-packages\imageai\Prediction\Custom_init_.py”, line 547, in predictImage
raise ValueError(“An error occured! Try again.”)

ValueError: An error occured! Try again.

As this error message is very unspecific I would like to ask you what could be reason that my script doesn’t work? Thank you very much for your feedback!

Best regards
Florian

Kindly install the latest version of ImageAI and try the code again. Let me know if problem persist and share the error message as well.

pip3 install imageai --upgrade

Thank you! Unfortunately I receive the same error message. It even doesn’t work if I use the model and json file you provided us in the tutorial.

Error message:

…_init_.py", line 783, in predictImage
raise ValueError(“An error occured! Try again.”)

ValueError: An error occured! Try again.

Now the shown line in the error message is 783 instead of 547 but this is probably due to the upgrade. When I have a look in the init.py file it is the following (same) code snippet:

            try:

                predictiondata = decode_predictions(prediction, top=int(result_count), model_json=self.jsonPath)

                for result in predictiondata:
                    prediction_results.append(str(result[0]))
                    prediction_probabilities.append(result[1] * 100)


            except:
                raise ValueError("An error occured! Try again.")

Please do the following:

  • View the contents of your model_class.json and ensure it contains the 7 items in your dataset

The model_class.json file is created and contains the seven items (starting from 0 to 6).