简体   繁体   中英

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. 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. 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

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM