Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NameError: name 'image_array' is not defined #681

Open
ovladuk opened this issue Jun 26, 2021 · 17 comments
Open

NameError: name 'image_array' is not defined #681

ovladuk opened this issue Jun 26, 2021 · 17 comments

Comments

@ovladuk
Copy link

ovladuk commented Jun 26, 2021

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)

@ekesdf
Copy link

ekesdf commented Jun 26, 2021

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.

@ovladuk
Copy link
Author

ovladuk commented Jun 26, 2021

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?

@ekesdf
Copy link

ekesdf commented Jun 26, 2021

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
Copy link

ekesdf commented Jun 26, 2021

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

@ovladuk
Copy link
Author

ovladuk commented Jun 26, 2021

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

@ekesdf
Copy link

ekesdf commented Jun 26, 2021

what python version you running ??

@ovladuk
Copy link
Author

ovladuk commented Jun 26, 2021

what python version you running ??

3.7.6

@ekesdf
Copy link

ekesdf commented Jun 26, 2021

Is there a space in front of the line ?

@ovladuk
Copy link
Author

ovladuk commented Jun 26, 2021

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)

@ekesdf
Copy link

ekesdf commented Jun 26, 2021

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

@ovladuk
Copy link
Author

ovladuk commented Jun 26, 2021

this is a screenshot of the code using sublime text

array

@ekesdf
Copy link

ekesdf commented Jun 26, 2021

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
Copy link

ekesdf commented Jun 26, 2021

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

@ovladuk
Copy link
Author

ovladuk commented Jun 27, 2021

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
Copy link
Author

ovladuk commented Jun 27, 2021

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.

@ekesdf
Copy link

ekesdf commented Jun 27, 2021

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

@ovladuk
Copy link
Author

ovladuk commented Jun 27, 2021

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants