[英]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
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是为您提供目录下所有图像列表的方法
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.