繁体   English   中英

从计算机加载图像数据集时出错

[英]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)

但最后一行代码反映了一个错误:

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

我究竟做错了什么? 我想加载我自己的数据集而不是 mnist.load_data()。

我能够重现您的问题并修改您的代码来解决它,如下所示:

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)

注意有更好的方法来编写此代码,例如使用 python 来实现循环功能,例如在 path1 中设置正在运行的 python 路径并首先遍历图像而不使用索引,例如:

os.chdir(path1)

for picture in subjects:
   bla bla

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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