0

I want to load all of the images from the folder /img and /mask respectively. The data structure can be shown as follows:

data
    img
        0.png
        1.png
        2.png
        3.png
        ...
   mask
       label_0.png
       label_1.png
       label_2.png
       ...

Hopefully for help.

1
  • where do you want to load the images, do you want to load images from one folder to another? Commented Feb 27, 2019 at 5:51

2 Answers 2

1

If you want to load all the images from the two folders then you can try cv2

import cv2

img = []
for i in range(n): # n = number of images in img folder
    img_path = f'~data\img\{i}.png' # replace ~ with full path 
    img.append(cv2.imread(img_path))

for i in range(n): # n = number of images in mask folder
    img_path = f'~data\mask\lable_{i}.png' # replace ~ with full path
    img.append(cv2.imread(img_path))
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, by the way, can I use DataLoader or other class to load it and how? I want to input the images in multiple threads.
1

If your Python is 3.4+, You can get the file list which has the same extension name using pathlib.

from pathlib import Path
# *.png
result = list( Path('./data/').rglob('*.[pP][nN][gG]') )
print(result)

Outcome:

[PosixPath('data/img/0.png'),
 PosixPath('data/img/0.png'),
 PosixPath('data/img/0.png'),
 ...
 PosixPath('data/mask/label_0.png'),
 PosixPath('data/mask/label_1.png'),
 PosixPath('data/mask/label_2.png'),
 ...
]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.