I want to loop over a batch of files in order to get 32 images of each sub-directory at a time (I cant load all images due to memory) e.g load img 1-32 of every dir use them and then load img 33-64 then 65-96 etc
My directory:
Rootdir
- dir1
- img 1
- img 2
- img...
- dir2
- img 5000001
- img 5000002
- img...
- dir3
- img 10000001
- img 10000002
- img...
So I would need to load img1,2,..,32, 5000001,...5000032, 1000001,...10000032 at first loop then img33,34,..,64, 5000033,...5000064, 1000033,...10000064 at second loop
Is there a way to do this properly?
I am trying using os.walk and it allows me to loop over my directory but I don't see how I can adapt this loop to my required 32 batches?
for dirName, subdirList, fileList in os.walk(rootdir):
print('Found directory: %s' % dirName)
for fname in sorted(fileList):
img_path = os.path.join(dirName, fname)
try:
img = load_img(img_path, target_size=None)
imgs.append(img)
except Exception as e:
print(str(e), fname, i)
#do something on imgs
EDIT
all of your comment get me stuff like that:
dir1/img1.jpg to dir1/img32.jpg then dir1/img33.jpg to dir1/img64.jpg then ...
then dir2/img1.jpg to dir1/img32.jpg then dir2/img33.jpg to dir2/img64.jpg then ...
then dir3/img1.jpg to dir3/img32.jpg then dir3/img33.jpg to dir3/img64.jpg :(
What I'm trying to achieve is:
Files of dir1 numero 1 to 32 + files of dir2 numero 1 to 32 + files of dir3 numero 1 to 32 then
Files of dir1 numero 33 to 64 + files of dir2 numero 33 to 64 + files of dir3 numero 33 to 64 in the same loop