dan
dan

Reputation: 493

How to read the image data from different folders?

I have 4 folders: Cat, Dog, Tiger and Kangaroo, each with 100 images save in the respective folders.

When I use chainer library to import mnist dataset, I get tuple of images and their corresponding labels. I wish to read and bring my dataset in the same format.

Chainer code looks like:

train, test = chainer.datasets.get_mnist()
train_data, train_targets = np.array(train).transpose()
test_data, test_targets = np.array(test).transpose()
train_data = np.array(list(train_data)).reshape(train_data.shape[0],1,28,28)
test_data = np.array(list(test_data)).reshape(test_data.shape[0],1,28,28)

I tried following code but it does not bring the code in same format.

img_dict=dict()

for root, dirs, files in os.walk(path):
    print(os.path.basename(root))
    my_key = os.path.basename(root)

    dir_images = []
    for file_ in files:
        full_file_path = os.path.join(root, file_)
        img = cv2.imread(full_file_path)
        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        dir_images.append(img)

    img_dict[my_key] = dir_images

What is the correct way to read these images and bring it in the same format as mentioned above for MNIST from chainer library?

Upvotes: 1

Views: 699

Answers (1)

TulakHord
TulakHord

Reputation: 432

X=[]
Z=[]
IMG_SIZE=150
CAT_DIR='../CAT'
DOG_DIR='../DOG'
TIGER_DIR='../TIGER'
KANGAROO_DIR='../KANGAROO'

def assign_label(img,label):
    return label
def make_train_data(label,DIR):
    for img in tqdm(os.listdir(DIR)):
        label=assign_label(img,label)
        path = os.path.join(DIR,img)
        img = cv2.imread(path,cv2.IMREAD_COLOR)
        img = cv2.resize(img, (IMG_SIZE,IMG_SIZE))
    
        X.append(np.array(img))
        Z.append(str(label))

make_train_data('CAT',CAT_DIR)
make_train_data('DOG',DOG_DIR)
make_train_data('TIGER',TIGER_DIR)
make_train_data('KANGAROO',KANGAROO_DIR)

# USE A LABELENCODER IF YOU WANT HERE

holder = zip(X, Z)
print(tuple(holder))

Upvotes: 0

Related Questions