NameError: name 'image_array' is not defined
im getting "NameError: name 'image_array' is not defined" error from this code. can anyone help?
from imageai.Classification import ImageClassification import os
execution_path = os.getcwd()
prediction = ImageClassification() prediction.setModelTypeAsResNet50() prediction.setModelPath(os.path.join(execution_path, "resnet50_imagenet_tf.2.0.h5")) prediction.loadModel()
predictions, probabilities = prediction.classifyImage(image_array, result_count=5 , input_type="array" ) for eachPrediction, eachProbability in zip(predictions, probabilities): print(eachPrediction , " : " , eachProbability)
First Tip: Learn the simple Error codes from python
Second Tip : Add these Lines to your code
from PIL import Image
import numpy as np
image_array = np.assaray(Image.open("PATH_TO_THE_IMAGE_YOU_WANT_TO_PREDICT_ON")
1 Question, or more like a prediction. You are new to python programming?
If so, you should start learning the main error messages from python, like a "Type Error" or a "Syntax Error" or the "Name Error".
Btw a quick google search can help much faster, an easier. This comment should not stop you from programming, it is just that these Libraries are so easy to use that even a person which new to this topic is able to use them, which leads to so many issue calls that are easily solved by a quick google search.
So if you have any other question, you can ask me, but for you to improve yourself it would be better to try to solve the issue by your self with a quick search, and I am for 100% sure the "Name Error" has been solved somewhere on sites like Stack Overflow or other coding websites. Maybe not the exact same one, but those Errors always have the "same" cause and solution.
First Tip: Learn the simple Error codes from python
Second Tip : Add these Lines to your code
from PIL import Image import numpy as np image_array = np.assaray(Image.open("PATH_TO_THE_IMAGE_YOU_WANT_TO_PREDICT_ON")1 Question, or more like a prediction. You are new to python programming?
If so, you should start learning the main error messages from python, like a "Type Error" or a "Syntax Error" or the "Name Error".
Btw a quick google search can help much faster, an easier. This comment should not stop you from programming, it is just that these Libraries are so easy to use that even a person which new to this topic is able to use them, which leads to so many issue calls that are easily solved by a quick google search.
So if you have any other question, you can ask me, but for you to improve yourself it would be better to try to solve the issue by your self with a quick search, and I am for 100% sure the "Name Error" has been solved somewhere on sites like Stack Overflow or other coding websites. Maybe not the exact same one, but those Errors always have the "same" cause and solution.
ok thanks. I'm pretty much an absolute beginner. i'm trying to just use the image prediction on a personal project i know basics of running python but the code scrambles my mind.
where exactly in the code should i put the code you suggested?
from imageai.Classification import ImageClassification
from PIL import Image
import numpy as np
import os
image_array = np.assaray(Image.open("PATH_TO_THE_IMAGE_YOU_WANT_TO_PREDICT_ON")
execution_path = os.getcwd()
prediction = ImageClassification()
prediction.setModelTypeAsResNet50()
prediction.setModelPath(os.path.join(execution_path, "resnet50_imagenet_tf.2.0.h5"))
prediction.loadModel()
predictions, probabilities = prediction.classifyImage(image_array, result_count=5 , input_type="array" )
for eachPrediction, eachProbability in zip(predictions, probabilities):
print(eachPrediction , " : " , eachProbability)
This is the modified version, just coy it, and it should run it.
If you want, I can try to explain to you what the code makes.
from imageai.Classification import ImageClassification from PIL import Image import numpy as np import os image_array = np.assaray(Image.open("PATH_TO_THE_IMAGE_YOU_WANT_TO_PREDICT_ON") execution_path = os.getcwd() prediction = ImageClassification() prediction.setModelTypeAsResNet50() prediction.setModelPath(os.path.join(execution_path, "resnet50_imagenet_tf.2.0.h5")) prediction.loadModel() predictions, probabilities = prediction.classifyImage(image_array, result_count=5 , input_type="array" ) for eachPrediction, eachProbability in zip(predictions, probabilities): print(eachPrediction , " : " , eachProbability)This is the modified version, just coy it, and it should run it.
i tried the code and got this
" execution_path = os.getcwd() ^ SyntaxError: invalid syntax"
i feel like im missing something obvious sometimes lol
what python version you running ??
what python version you running ??
3.7.6
Is there a space in front of the line ?
Is there a space in front of the line ?
the code is the one you replied with.
from imageai.Classification import ImageClassification from PIL import Image import numpy as np import os
image_array = np.assaray(Image.open("PATH_TO_THE_IMAGE_YOU_WANT_TO_PREDICT_ON") execution_path = os.getcwd()
prediction = ImageClassification() prediction.setModelTypeAsResNet50() prediction.setModelPath(os.path.join(execution_path, "resnet50_imagenet_tf.2.0.h5")) prediction.loadModel()
predictions, probabilities = prediction.classifyImage(image_array, result_count=5 , input_type="array" ) for eachPrediction, eachProbability in zip(predictions, probabilities): print(eachPrediction , " : " , eachProbability)
from PIL import Image
import numpy as np
import os
image_array = np.assaray(Image.open("PATH_TO_THE_IMAGE_YOU_WANT_TO_PREDICT_ON"))
execution_path = os.getcwd()
prediction = ImageClassification()
prediction.setModelTypeAsResNet50()
prediction.setModelPath(os.path.join(execution_path, "resnet50_imagenet_tf.2.0.h5"))
prediction.loadModel()
predictions, probabilities = prediction.classifyImage(image_array, result_count=5 , input_type="array" )
for eachPrediction, eachProbability in zip(predictions, probabilities):
print(eachPrediction , " : " , eachProbability)
@ovladuk I found the issue- I missed a closing bracket in this line this code above should run now. :smile:
image_array = np.assaray(Image.open("PATH_TO_THE_IMAGE_YOU_WANT_TO_PREDICT_ON"))
this is a screenshot of the code using sublime text

If you hover over the yellow bar on the left side it would show you like I said before that I missed a closing bracket
from PIL import Image import numpy as np import os image_array = np.assaray(Image.open("PATH_TO_THE_IMAGE_YOU_WANT_TO_PREDICT_ON")) execution_path = os.getcwd() prediction = ImageClassification() prediction.setModelTypeAsResNet50() prediction.setModelPath(os.path.join(execution_path, "resnet50_imagenet_tf.2.0.h5")) prediction.loadModel() predictions, probabilities = prediction.classifyImage(image_array, result_count=5 , input_type="array" ) for eachPrediction, eachProbability in zip(predictions, probabilities): print(eachPrediction , " : " , eachProbability)@ovladuk I found the issue- I missed a closing bracket in this line this code above should run now.
image_array = np.assaray(Image.open("PATH_TO_THE_IMAGE_YOU_WANT_TO_PREDICT_ON"))
this is the fixed code
from PIL import Image import numpy as np import os image_array = np.assaray(Image.open("PATH_TO_THE_IMAGE_YOU_WANT_TO_PREDICT_ON")) execution_path = os.getcwd() prediction = ImageClassification() prediction.setModelTypeAsResNet50() prediction.setModelPath(os.path.join(execution_path, "resnet50_imagenet_tf.2.0.h5")) prediction.loadModel() predictions, probabilities = prediction.classifyImage(image_array, result_count=5 , input_type="array" ) for eachPrediction, eachProbability in zip(predictions, probabilities): print(eachPrediction , " : " , eachProbability)@ovladuk I found the issue- I missed a closing bracket in this line this code above should run now.
image_array = np.assaray(Image.open("PATH_TO_THE_IMAGE_YOU_WANT_TO_PREDICT_ON"))this is the fixed code
thanks for the help i kind of gave up with that code cause for some reason the model just didn't want to work. must be corrupt somehow.
from PIL import Image import numpy as np import os image_array = np.assaray(Image.open("PATH_TO_THE_IMAGE_YOU_WANT_TO_PREDICT_ON")) execution_path = os.getcwd() prediction = ImageClassification() prediction.setModelTypeAsResNet50() prediction.setModelPath(os.path.join(execution_path, "resnet50_imagenet_tf.2.0.h5")) prediction.loadModel() predictions, probabilities = prediction.classifyImage(image_array, result_count=5 , input_type="array" ) for eachPrediction, eachProbability in zip(predictions, probabilities): print(eachPrediction , " : " , eachProbability)@ovladuk I found the issue- I missed a closing bracket in this line this code above should run now.
image_array = np.assaray(Image.open("PATH_TO_THE_IMAGE_YOU_WANT_TO_PREDICT_ON"))this is the fixed co
from PIL import Image import numpy as np import os image_array = np.assaray(Image.open("PATH_TO_THE_IMAGE_YOU_WANT_TO_PREDICT_ON")) execution_path = os.getcwd() prediction = ImageClassification() prediction.setModelTypeAsResNet50() prediction.setModelPath(os.path.join(execution_path, "resnet50_imagenet_tf.2.0.h5")) prediction.loadModel() predictions, probabilities = prediction.classifyImage(image_array, result_count=5 , input_type="array" ) for eachPrediction, eachProbability in zip(predictions, probabilities): print(eachPrediction , " : " , eachProbability)@ovladuk I found the issue- I missed a closing bracket in this line this code above should run now.
image_array = np.assaray(Image.open("PATH_TO_THE_IMAGE_YOU_WANT_TO_PREDICT_ON"))this is the fixed code
scratch the last post i sent. i got it to predict the image but it doesn't predict the other images in the folder.
By the way, you can just delete a comment, and please stop comment each comment again if it is not necessary thy.
** here is the Solution for your Problem***
from imageai.Classification import ImageClassification
from PIL import Image
import numpy as np
import os
execution_path = "Path_to_youre_folder"
output = open("predictions.txt","w") # opens a txt file
prediction = ImageClassification()
prediction.setModelTypeAsResNet50()
prediction.setModelPath("Path_to_your_Model")
prediction.loadModel()
for file in os.listdir(execution_path): # loops trough all the files in the given folder
image_array = np.assaray(Image.open(execution_path+"/"+file))
predictions, probabilities = prediction.classifyImage(image_array, result_count=5 , input_type="array" )
output.write(f"\n{file}\n") # writes the current image_name in to the txt_file
for eachPrediction, eachProbability in zip(predictions, probabilities): # loops trough all the given predictions. Writes them into the txt file and prints them out
print(eachPrediction , " : " , eachProbability)
output.write(f"{eachPrediction} {eachProbability}\n") # writes the current prediction with the score of the prediction
output.close()
from imageai.Classification import ImageClassification from PIL import Image import numpy as np import os
execution_path = "Path_to_youre_folder" output = open("predictions.txt","w") # opens a txt file
prediction = ImageClassification() prediction.setModelTypeAsResNet50() prediction.setModelPath("Path_to_your_Model") prediction.loadModel()
for file in os.listdir(execution_path): # loops trough all the files in the given folder
image_array = np.assaray(Image.open(execution_path+"/"+file)) predictions, probabilities = prediction.classifyImage(image_array, result_count=5 , input_type="array" ) output.write(f"\n{file}\n") # writes the current image_name in to the txt_file for eachPrediction, eachProbability in zip(predictions, probabilities): # loops trough all the given predictions. Writes them into the txt file and prints them out print(eachPrediction , " : " , eachProbability) output.write(f"{eachPrediction} {eachProbability}\n") # writes the current prediction with the score of the predictionoutput.close()
sorry i was just getting frustrated because it wasnt working. ive tried multiple different versions of code with no success.
thanks it worked! i had to change "assaray" to "asarray" but it worked after that. thanks for your help!