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

clautsick
  • 37
  • 5
  • Hey, you are defining x_test with x_test, you didnt define him before, you have to declare and define before you are using him as a parameter in keras method – Mark Kapilyan Feb 01 '23 at 20:12
  • Hey, thanks but I have no idea how should I define it to make it work? I want to split the dataset that I created into x train and x test but I dont know how :( – clautsick Feb 01 '23 at 20:23
  • Notice that you added all images to train, what you want to do is split it like they did in this issue https://stackoverflow.com/questions/3674409/how-to-split-partition-a-dataset-into-training-and-test-datasets-for-e-g-cros – Mark Kapilyan Feb 01 '23 at 20:29
  • Also run the keras function you used after splitting the same way you used on both test and train – Mark Kapilyan Feb 01 '23 at 20:38
  • I get what you're saying and normally i dont have problem with splitting the data but with images it's just!!!!! i cant seem to do it as i keep getting errors :/ – clautsick Feb 01 '23 at 20:58
  • Keep editing your issue then with every error you get if i were near a computer i would've helped more, maybe tomorrow if you didnt solve it yet:) – Mark Kapilyan Feb 01 '23 at 21:22

1 Answers1

1

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
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437