简体   繁体   English

如何从python中的不同文件夹和子文件夹加载图像

[英]how to load images from different folders and subfolders in python

I am developing a CNN in a animal classification dataset, which are separated into 2 folders, and the 2 folders involve another subfolders...there are four layers of this structure, and now I want to load them and convert them to n-dimension-arrays to feed to tensorflow, the names of these folders are the labels. 我正在动物分类数据集中开发CNN,该数据被分成2个文件夹,并且这2个文件夹包含另一个子文件夹...此结构有四层,现在我想加载它们并将其转换为n维-arrays馈送到tensorflow,这些文件夹的名称是标签。 I hope that someone can help me with some concrete codes or some useful materials. 我希望有人可以为我提供一些具体的代码或有用的材料。 Thank you very much in advance! 提前非常感谢您!

Here I will give some examples: Anisopleura Libellulidae Leach, 1815 Trithemis aurora Zygoptera Calopterygidae Selys, 1850 Calopteryx splendens the aurora and splendens are the labels of this problem, and they are also the name of fifth floor subfolders, the images are stored in these folders. 在这里,我将举一些例子:Anisopleura Libellulidae Leach,1815年Trithemis aurora Zygoptera Calopterygidae Selys,1850 Calopteryx锦绣此问题的标签,它们也是五楼子文件夹的名称,图像存储在这些文件夹中。 C:\\Users\\Seth\\Desktop\\dragonfly\\Anisopleura\\Libellulidae Leach, 1815\\Pseudothemis\\zonata C:\\ Users \\ Seth \\ Desktop \\ dragonfly \\ Anisopleura \\ Libellulidae Leach,1815 \\ Pseudothemis \\ zonata

this is a path. 这是一条路。

I using openface library for face recognition, In this library iterImgs is method that gives list of you all images under a Directory 我使用openface库进行人脸识别,在该库中,iterImgs是为您提供目录下所有图像列表的方法

For detail iterImgs 有关详细信息

from openface.data import iterImgs

imgs = list(iterImgs("Directory path"))

print imgs    # print all images in Directory path also in Tree

or another way is defined a vailed extension 或以其他方式定义了有效扩展

vailed_ext = [".jpg",".png"]
import os 
f_list = []
def Test2(rootDir):     
    for lists in os.listdir(rootDir): 
        path = os.path.join(rootDir, lists) 
        filename, file_extension = os.path.splitext(path) 
        if file_extension in vailed_ext:
            print path          
            f_list.append[path]
        if os.path.isdir(path): 
           Test2(path)

Test2("/home/")
print f_list

os.walk() is what you are looking for. os.walk()是您要寻找的。

import os

# traverse root directory, and list directories as dirs and files as files
for root, dirs, files in os.walk("."):
    path = root.split(os.sep)
    print((len(path) - 1) * '---', os.path.basename(root))
    for file in files:
        print(len(path) * '---', file)

This code will allow you to parse recursively all folders and subfolders. 此代码将允许您递归解析所有文件夹和子文件夹。 You get the name of the subfolder(labels in your case) and all files in the file variable. 您将获得子文件夹的名称(在您的情况下为标签)以及file变量中的所有文件。

The next work for you is then to maybe create a dictionnary (or numpy multi-dimensional array) to store for each label (or subfolder) the features of your image. 然后,您的下一个工作是创建字典(或numpy多维数组)以为每个标签(或子文件夹)存储图像的特征。

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

相关问题 如何从python中的不同文件夹和子文件夹加载数据(图像) - How to load data(images) from different folders and subfolders in python Python:如何保存文件夹和子文件夹中的文件? - Python: How to save files from folders and subfolders? 从不同的子文件夹加载用于图像分类的图像数据 - Load Images Data for Images Classification from different subfolders 如何从不同文件夹加载图像和文本标签以进行 CNN 回归 - How to load images and text labels for CNN regression from different folders Python 将图像从 URL 保存到具有自定义名称和结构的不同文件夹中 - Python save images from URL to different folders with custom name and structure 如何将处理后的图像保存到python文件夹中的其他文件夹中? - How to save processed images to different folders within a folder in python? 如何使用循环将图像保存在python-opencv的不同文件夹中 - How to use a loop to save images in different folders in python-opencv 如何从 Python 中的不同文件夹导入模块? - How to import modules from different folders in Python? 在python中从ftp文件夹和子文件夹读取所有文件 - Reading all files from ftp folders and subfolders in python 我们如何从两个不同的文件夹中读取相应的图像? - How can we read corresponding images from two different folders?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM