简体   繁体   中英

Errow while loading the image data set from computer

path1 = 'C:\\Users\\klaud\\Desktop\\images\\'
all_images = \[\]
subjects = os.listdir(path1)
numberOfSubject = len(subjects)
print('Number of Subjects: ', numberOfSubject)
    for number1 in range(0, numberOfSubject):  # numberOfSubject
    path2 = (path1 + subjects\[number1\] + '/')
    sequences = os.listdir(path2)
    numberOfsequences = len(sequences)
        for number2 in range(4, numberOfsequences):
        path3 = path2 + sequences\[number2\]
        img = cv2.imread(path3, 0)
        img = img.reshape(512, 512, 1)
        all_images.append(img)
x_train = np.array(all_images)
x_test = keras.utils.np_utils.to_categorical(x_test)

but last line code is reflecting an error:

x_test= keras.utils.to_categorical(x_test) NameError: name 'x_test' is not defined

What am I doing wrong? I wanted to load my own dataset instead of the mnist.load_data().

I was able to reproduce your problem and modified your code to solve it, like this:

import os
import keras
import cv2
import numpy as np

path1 = 'C:\\Users\\markk\\Desktop\\Photos\\wedding'
all_images = []
subjects = os.listdir(path1)
# It already holding all the picture names
print(subjects)
numberOfSubject = len(subjects)
print('Number of Subjects: ', numberOfSubject)
for number1 in range(0, numberOfSubject):  # numberOfSubject
    path2 = (path1 + '\\' + subjects[number1] )
    img = cv2.imread(path2, cv2.IMREAD_COLOR)
    all_images.append(img)
# Already np array check in the print:    
print(all_images)
all_images = np.random.rand(100,5)
np.random.shuffle(all_images)
x_train, x_test = all_images[:80,:], all_images[80:,:]
x_test = keras.utils.np_utils.to_categorical(x_test)

Note There are better ways to write this code like using the python for looping features as in setting the running python path in path1 and iterating over images in the first place and not using indexs for example:

os.chdir(path1)

for picture in subjects:
   bla bla

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