quacknet.dataAugmentation
1import numpy as np 2from PIL import Image 3import os 4 5class Augementation: 6 def hotEncodeLabels(self, allLabels, numClasses): 7 """ 8 Converts a list of integer labels into one hot encoded format 9 10 Args: 11 allLabels (list of lists): All of the labels of the input data. 12 numClasses (int): The total number of classes. 13 14 Returns: 15 list of ndarray: a 2D array of hot encoded labels. 16 """ 17 labels = np.zeros((len(allLabels), numClasses)) 18 for i in range(len(allLabels)): 19 labels[i][allLabels[i]] = 1 20 return labels 21 22 def getImagePaths(self, folderPath, extensions = ['.jpg', '.png', '.jpeg']): 23 """ 24 Retrieves paths of all image files in a directory and its subdirectories. 25 26 Args: 27 folderPath (str): The path to the directory containing images. 28 extensions (list of str, optional): A list of file extensions to get as images. Default is ['.jpg', '.png', '.jpeg']. 29 30 Returns: 31 list of str: A list of full paths to image files. 32 """ 33 imagePaths = [] 34 for root, dirs, files in os.walk(folderPath): 35 for file in files: 36 if(any(file.lower().endswith(ext) for ext in extensions)): 37 fullPath = os.path.join(root, file) 38 imagePaths.append(fullPath) 39 return imagePaths 40 41 def preprocessImages(self, imagePaths, targetSize = (128, 128)): 42 """ 43 Loads and preprocesses images by resising and normalising them. 44 45 Args: 46 imagePaths (list of str): A list of full paths to image files. 47 targetSize (tuple of int, optional): The desired size of the output images (width, height). Default is (128, 128). 48 49 Returns: 50 ndarray: A list of preprocessed images with values normalised between 0 to 1. 51 """ 52 images = [] 53 for path in imagePaths: 54 img = Image.open(path).convert('RGB') 55 resized = img.resize(targetSize) 56 normalised = np.array(resized) / 255.0 57 images.append(normalised) 58 return np.array(images) 59 60 def dataAugmentation(self, images): 61 """ 62 Performs basic data augmentation by flipping horizontally and vertically. 63 64 Args: 65 images (ndarray): A list of images to augment 66 67 Returns: 68 ndarray: A list containing the augmented and original images. 69 """ 70 allImages = [] 71 for img in images: 72 allImages.append(img) 73 allImages.append(np.fliplr(img)) 74 allImages.append(np.flipud(img)) 75 allImages.append(np.flipud(np.fliplr(img))) 76 return np.array(allImages)
6class Augementation: 7 def hotEncodeLabels(self, allLabels, numClasses): 8 """ 9 Converts a list of integer labels into one hot encoded format 10 11 Args: 12 allLabels (list of lists): All of the labels of the input data. 13 numClasses (int): The total number of classes. 14 15 Returns: 16 list of ndarray: a 2D array of hot encoded labels. 17 """ 18 labels = np.zeros((len(allLabels), numClasses)) 19 for i in range(len(allLabels)): 20 labels[i][allLabels[i]] = 1 21 return labels 22 23 def getImagePaths(self, folderPath, extensions = ['.jpg', '.png', '.jpeg']): 24 """ 25 Retrieves paths of all image files in a directory and its subdirectories. 26 27 Args: 28 folderPath (str): The path to the directory containing images. 29 extensions (list of str, optional): A list of file extensions to get as images. Default is ['.jpg', '.png', '.jpeg']. 30 31 Returns: 32 list of str: A list of full paths to image files. 33 """ 34 imagePaths = [] 35 for root, dirs, files in os.walk(folderPath): 36 for file in files: 37 if(any(file.lower().endswith(ext) for ext in extensions)): 38 fullPath = os.path.join(root, file) 39 imagePaths.append(fullPath) 40 return imagePaths 41 42 def preprocessImages(self, imagePaths, targetSize = (128, 128)): 43 """ 44 Loads and preprocesses images by resising and normalising them. 45 46 Args: 47 imagePaths (list of str): A list of full paths to image files. 48 targetSize (tuple of int, optional): The desired size of the output images (width, height). Default is (128, 128). 49 50 Returns: 51 ndarray: A list of preprocessed images with values normalised between 0 to 1. 52 """ 53 images = [] 54 for path in imagePaths: 55 img = Image.open(path).convert('RGB') 56 resized = img.resize(targetSize) 57 normalised = np.array(resized) / 255.0 58 images.append(normalised) 59 return np.array(images) 60 61 def dataAugmentation(self, images): 62 """ 63 Performs basic data augmentation by flipping horizontally and vertically. 64 65 Args: 66 images (ndarray): A list of images to augment 67 68 Returns: 69 ndarray: A list containing the augmented and original images. 70 """ 71 allImages = [] 72 for img in images: 73 allImages.append(img) 74 allImages.append(np.fliplr(img)) 75 allImages.append(np.flipud(img)) 76 allImages.append(np.flipud(np.fliplr(img))) 77 return np.array(allImages)
7 def hotEncodeLabels(self, allLabels, numClasses): 8 """ 9 Converts a list of integer labels into one hot encoded format 10 11 Args: 12 allLabels (list of lists): All of the labels of the input data. 13 numClasses (int): The total number of classes. 14 15 Returns: 16 list of ndarray: a 2D array of hot encoded labels. 17 """ 18 labels = np.zeros((len(allLabels), numClasses)) 19 for i in range(len(allLabels)): 20 labels[i][allLabels[i]] = 1 21 return labels
Converts a list of integer labels into one hot encoded format
Args: allLabels (list of lists): All of the labels of the input data. numClasses (int): The total number of classes.
Returns: list of ndarray: a 2D array of hot encoded labels.
23 def getImagePaths(self, folderPath, extensions = ['.jpg', '.png', '.jpeg']): 24 """ 25 Retrieves paths of all image files in a directory and its subdirectories. 26 27 Args: 28 folderPath (str): The path to the directory containing images. 29 extensions (list of str, optional): A list of file extensions to get as images. Default is ['.jpg', '.png', '.jpeg']. 30 31 Returns: 32 list of str: A list of full paths to image files. 33 """ 34 imagePaths = [] 35 for root, dirs, files in os.walk(folderPath): 36 for file in files: 37 if(any(file.lower().endswith(ext) for ext in extensions)): 38 fullPath = os.path.join(root, file) 39 imagePaths.append(fullPath) 40 return imagePaths
Retrieves paths of all image files in a directory and its subdirectories.
Args: folderPath (str): The path to the directory containing images. extensions (list of str, optional): A list of file extensions to get as images. Default is ['.jpg', '.png', '.jpeg'].
Returns: list of str: A list of full paths to image files.
42 def preprocessImages(self, imagePaths, targetSize = (128, 128)): 43 """ 44 Loads and preprocesses images by resising and normalising them. 45 46 Args: 47 imagePaths (list of str): A list of full paths to image files. 48 targetSize (tuple of int, optional): The desired size of the output images (width, height). Default is (128, 128). 49 50 Returns: 51 ndarray: A list of preprocessed images with values normalised between 0 to 1. 52 """ 53 images = [] 54 for path in imagePaths: 55 img = Image.open(path).convert('RGB') 56 resized = img.resize(targetSize) 57 normalised = np.array(resized) / 255.0 58 images.append(normalised) 59 return np.array(images)
Loads and preprocesses images by resising and normalising them.
Args: imagePaths (list of str): A list of full paths to image files. targetSize (tuple of int, optional): The desired size of the output images (width, height). Default is (128, 128).
Returns: ndarray: A list of preprocessed images with values normalised between 0 to 1.
61 def dataAugmentation(self, images): 62 """ 63 Performs basic data augmentation by flipping horizontally and vertically. 64 65 Args: 66 images (ndarray): A list of images to augment 67 68 Returns: 69 ndarray: A list containing the augmented and original images. 70 """ 71 allImages = [] 72 for img in images: 73 allImages.append(img) 74 allImages.append(np.fliplr(img)) 75 allImages.append(np.flipud(img)) 76 allImages.append(np.flipud(np.fliplr(img))) 77 return np.array(allImages)
Performs basic data augmentation by flipping horizontally and vertically.
Args: images (ndarray): A list of images to augment
Returns: ndarray: A list containing the augmented and original images.