简体   繁体   中英

Numpy array declaration error

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM