R/k_imgDirSetup.R

Defines functions img_dir_setup

Documented in img_dir_setup

#' Image Directory Setup
#'
#' Creates sub directories of images according to their classification labels
#' Function is intended to be used to set up folder structure required for 
#' Keras' flow_images_from_directory function
#'
#' @param directory The directory where subfolders will be placed
#' @param labels Labels of your dataset. One subfolder will be created per unique label
#' @return Returns NULL
#' @export


img_dir_setup <- function(directory, labels){
  
  # Create the train and test directories
  if (!dir.exists(paste0(directory, "/train"))) dir.create(paste0(directory, "/train"))
  if (!dir.exists(paste0(directory, "/test"))) dir.create(paste0(directory, "/test"))
  
  # Create a subfolder for each class
  for (i in unique(labels)) {
    
    if (!dir.exists(paste0(directory, "/train/", i))) dir.create(paste0(directory, "/train/", i, "/"))
    if (!dir.exists(paste0(directory, "/test/", i))) dir.create(paste0(directory, "/test/", i, "/"))
    
  }
  
  return(NULL)
}
blazickjoe/DataScienceLibrary documentation built on Nov. 5, 2019, 2:26 p.m.