python - 将图像从文件夹 python 导入到 numpy 数组列表

标签 python numpy machine-learning scikit-learn python-import

我有一个包含 10000 张图像的文件夹和 3 个子文件夹,每个文件夹包含不同数量的图像。我想导入这些图像的一小部分进行训练,每次我想选择一部分数据时,我都会手动选择有限的大小。 我已经有了这个 python 代码:

train_dir = 'folder/train/' # This folder contains 10.000 images and 3 subfolders , each folder contains different number of images

from tqdm import tqdm
def get_data(folder):
    """
    Load the data and labels from the given folder.
    """
    X = []
    y = []
    for folderName in os.listdir(folder):
        if not folderName.startswith('.'):
            if folderName in   ['Name1']:
                label = 0
            elif folderName in ['Name2']:
                label = 1
            elif folderName in ['Name3']:
                label = 2
            else:
                label = 4
            for image_filename in tqdm(os.listdir(folder + folderName)):
                img_file = cv2.imread(folder + folderName + '/' + image_filename)
                if img_file is not None:
                    img_file = skimage.transform.resize(img_file, (imageSize, imageSize, 1))
                    img_arr = np.asarray(img_file)
                    X.append(img_arr)
                    y.append(label)
    X = np.asarray(X) # Keras only accepts data as numpy arrays 
    y = np.asarray(y)
    return X,y


X_test, y_test= get_data(train_dir)
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X_test, y_test, test_size=0.2)

我想指定Size参数,以便我可以选择要导入的图像数量。从每个子文件夹导入的图像数量应该相等

最佳答案

您可以读取每个文件夹中的每个路径并将其存储在单独的列表中,并选择相同数量的路径。

folder1_files = []
for root, dirs, files in os.walk('path/folder1', topdown=False):
    for i in files:
        folder1_files.append("path/folder1/"+i)

选择:

train = folder1[:n] + folder2[:n] + folder3[:n]

n - 每个文件夹中的图像数量

关于python - 将图像从文件夹 python 导入到 numpy 数组列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54920645/

相关文章:

python - 关于如何预测 future 时间序列数据的建议

python - 分层KFold输出处理

python - 如何在 Windows XP 上安装 numpy 和 scipy

python - 如何使 math 和 numpy 模块中的现有函数支持用户定义的对象?

python - scikit-neuralnetwork 中神经网络的反向传播和结构

python - 使用python将excel数据导入到类实例中

python - 从 NumPy 数组继承的类如何更改其自身的值?

Python 将 var 初始化为 None?

python - 过滤包含搜索词列表的 csv 文件

python - 如何从 Python 字典中删除键?