How do I import images and labels stored in 2 different folders?

23 Views Asked by At

I have 2 different folders named "images" and "labels", both with same file names "image_XXXX" and respectively .jpeg and .txt extensions (obviously).

I want to write a function that loads my data in order to perform image classification using different models.

Can anyone help?

The functions I've been writing so far do not return any value stored under X and y (my images and labels then). Also I'm working on google colab for this task especially.

not including the dependencies:

def import_selected_data(path, label_list=None):
    """Load images and labels from selected directories"""
    images = []
    labels = []

    if label_list is None:
        folder_names = [folder for folder in sorted(os.listdir(path)) if not folder.startswith('.')]
    else:
        folder_names = [folder for folder in sorted(os.listdir(path)) if folder in label_list]

    for folder in folder_names:
        file_names = [file for file in sorted(os.listdir(os.path.join(path, folder))) if file.endswith('.jpg')]
        for file in file_names:
            images.append(io.imread(os.path.join(path, folder, file)))
            labels.append(folder)

    return images, labels
0

There are 0 best solutions below