Home > Net >  KeyError: '1' when creating a list of training data
KeyError: '1' when creating a list of training data

Time:06-17

i have a list of image as my dataset in trainpath folder. Under the trainpath folder, there is 2 folders, named by the class names (0 and 1). And under each folder, there is images data. And i try to create a list of training data using that dataset.

code = {'distracted':0 ,'not-distracted':1}

X_train = []
y_train = []
for folder in  os.listdir(trainpath) : 
    files = gb.glob(pathname= str( trainpath  '//'   folder   '/*.jpg'))
    for file in files: 
        image = cv2.imread(file)
        image_array = cv2.resize(image , (s,s))
        X_train.append(list(image_array))
        y_train.append(code[folder])

But, i got this error:

KeyError                                  Traceback (most recent call last)
<ipython-input-74-5016fe834a0a> in <module>()
      7         image_array = cv2.resize(image , (s,s))
      8         X_train.append(list(image_array))
----> 9         y_train.append(code[folder])

KeyError: '1'

CodePudding user response:

You need to put your folder names as keys, and an empty list as value :

code = {'0':0 ,'1':1}
  • Related