首页 > 解决方案 > os.listdir 显示 25000 个输入,但只能加载 10685 个文件

问题描述

import os
import numpy as np
from keras.preprocessing.image import load_img, img_to_array

filePath = 'PetImages/'
photos, labels = list(), list()

width = 100
height = 100
counter = 0


for folder in os.listdir(filePath):
    main = os.listdir(filePath + folder)
    for file in os.listdir(filePath + folder):
        print(filePath + folder)
        counter = counter + 1
        try:
            if folder == 'Cat':
                output = 1.0
            else:
                output = 0.0
            photo = load_img(filePath + folder + '/' + file, target_size=(width,height))
            photo = img_to_array(photo)
            photos.append(photo)
            labels.append(output)
        except:
            break
    
photos = np.asarray(photos)
labels = np.asarray(labels)
print(photos.shape, labels.shape)

#np.save('CatDogPhotos.npy', photos)
#np.save('CatDogLabels.npy', labels)

输出: (10685, 100, 100, 3) (10685,)

我正在尝试加载典型的猫/狗数据集。但是,使用此代码,我只能加载 10685 张照片的数组,而不是全部 25000 张图像。

我无法弄清楚为什么会这样。任何人都可以帮助对此发表评论吗?非常感谢!

标签: pythonlistdir

解决方案


推荐阅读