ImageAI icon indicating copy to clipboard operation
ImageAI copied to clipboard

NameError: name 'image_array' is not defined

Open ovladuk opened this issue 4 years ago • 17 comments

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)

ovladuk avatar Jun 26 '21 14:06 ovladuk

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.

ekesdf avatar Jun 26 '21 18:06 ekesdf

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?

ovladuk avatar Jun 26 '21 19:06 ovladuk

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.

ekesdf avatar Jun 26 '21 19:06 ekesdf

If you want, I can try to explain to you what the code makes.

ekesdf avatar Jun 26 '21 19:06 ekesdf

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

ovladuk avatar Jun 26 '21 19:06 ovladuk

what python version you running ??

ekesdf avatar Jun 26 '21 19:06 ekesdf

what python version you running ??

3.7.6

ovladuk avatar Jun 26 '21 19:06 ovladuk

Is there a space in front of the line ?

ekesdf avatar Jun 26 '21 19:06 ekesdf

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)

ovladuk avatar Jun 26 '21 19:06 ovladuk

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"))

ekesdf avatar Jun 26 '21 19:06 ekesdf

this is a screenshot of the code using sublime text

array

ovladuk avatar Jun 26 '21 19:06 ovladuk

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

ekesdf avatar Jun 26 '21 19:06 ekesdf

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

ekesdf avatar Jun 26 '21 19:06 ekesdf

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.

ovladuk avatar Jun 27 '21 12:06 ovladuk

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.

ovladuk avatar Jun 27 '21 13:06 ovladuk

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()

ekesdf avatar Jun 27 '21 13:06 ekesdf

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()

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!

ovladuk avatar Jun 27 '21 14:06 ovladuk