|
| 1 | +def load_imgs(path, im_size:int): |
| 2 | + |
| 3 | + ''' |
| 4 | + Function to load a directory of images and resize them for training a Convolutional Neural Network (CNN) model. |
| 5 | + IMPORTANT: Images must be divided into subdirectories according to the target |
| 6 | + (e.g. one directory for dog photos and another for cat photos). |
| 7 | + It can be used for both binary and categorical classification. |
| 8 | +
|
| 9 | + Args: |
| 10 | + - path: Path where the subdirectories with the images are located. |
| 11 | + - im_size: Size to which we want to resize the image (e.g. 32). |
| 12 | +
|
| 13 | + Returns: |
| 14 | + - df: Dataframe with the names of the images and the category to which they belong (target). |
| 15 | + - X_train: Array with the image data loaded after resizing. |
| 16 | + - y_train: Array with the target values. |
| 17 | + ''' |
| 18 | + |
| 19 | + import numpy as np |
| 20 | + import cv2 |
| 21 | + import os |
| 22 | + from skimage.io import imread |
| 23 | + import pandas as pd |
| 24 | + |
| 25 | + filenames = [] |
| 26 | + X = [] |
| 27 | + y = [] |
| 28 | + |
| 29 | + # Create a dictionary with the target values. |
| 30 | + class_names = os.listdir(path) |
| 31 | + class_names_label = {class_name:i for i , class_name in enumerate(class_names)} |
| 32 | + |
| 33 | + # Iterate over the subdirectories of the given path. |
| 34 | + for subdir, dirs, files in os.walk(path): |
| 35 | + for file in files: |
| 36 | + # Save the names of the files in a list. |
| 37 | + filenames.append(file) |
| 38 | + if file [-4:] == '.jpg' or file [-4:] == '.png': |
| 39 | + # Read the image in color. |
| 40 | + image = imread(subdir + '\\' + file) |
| 41 | + # Resize the image. |
| 42 | + smallimage = cv2.resize(image, (im_size, im_size)) |
| 43 | + # Save the images in the X variable. |
| 44 | + X.append(smallimage) |
| 45 | + |
| 46 | + # Save the target values of each image in a list. |
| 47 | + for i in range(len(class_names_label.keys())): |
| 48 | + if list(class_names_label.keys())[i] in subdir: |
| 49 | + y.append(list(class_names_label.values())[i]) |
| 50 | + |
| 51 | + # Returns: |
| 52 | + |
| 53 | + # 1. Dataframe with the names of the images and the category to which they belong. |
| 54 | + df = pd.DataFrame({ |
| 55 | + 'filename': filenames, |
| 56 | + 'category': y |
| 57 | + }) |
| 58 | + |
| 59 | + # 2. Array with the image data loaded after resizing. |
| 60 | + X_train = np.array(X) |
| 61 | + |
| 62 | + # 3. Array with the y values (target). |
| 63 | + y_train = np.array(y) |
| 64 | + |
| 65 | + return df, X_train, y_train |
0 commit comments