簡體   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