1

I have written the following code to extract features from an image. The feature vector once extracted from the featex function, needs to be appended to a large feature 2D array containing the features of all images used for training. The code is as follows:

for dirs, path, files in os.walk("wallet_training/"):
    for filename in files:
            f=os.path.join("wallet_training",filename)
            I=Image.open("wallet_training/1(1).jpeg")
            I=imresize(I,(256,256))
            p=featex(I)
            features=np.vstack([features],[p])

print features.shape

It gives the following error:

NameError: name 'features' is not defined

Can someone help me why this error is coming, because as far as i remember variables in python dont need to be defined beforehand.

Thank you in advance.

2
  • 4
    You do need to assign variables before referring to them. How is Python supposed to know what features is?.. Commented Jan 26, 2014 at 13:39
  • 1
    You're trying to use it as a function argument in np.vstack before it is defined. Commented Jan 26, 2014 at 13:41

1 Answer 1

1

As other users suggest in the comments, you need to declare features.

Additionally, I suggest you to do use Python list to append data and then convert to numpy array:

features = [];
for dirs, path, files in os.walk("wallet_training/"):
    for filename in files:
            f=os.path.join("wallet_training",filename)
            I=Image.open("wallet_training/1(1).jpeg")
            I=imresize(I,(256,256))
            p=featex(I)
            features.append(p) #'features' is a Python list

features = np.array(features)#Now 'features' is an array
print features.shape
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.