def __init__(self, image_data, data_name, dataset_name, img_dims): """ :param image_data: image data to be written to CSV file :param data_name: unique name used to identify extracted image data :param dataset_name: image dataset name from which to read images :param img_dims: size for dataset images (all sizes should be the same) """ # Store parameters self.image_data = image_data self.data_name = data_name self.dataset_name = dataset_name self.img_dims = img_dims self.metadata_writer = JSONWriter(self.data_name, dataset_metafile_path) # Read image labels text file and store it self.labels_path = base_training_data_store_path+dataset_name+'/labels.txt' self.path_reader = TXTReader(self.labels_path) self.path_reader.read_raw() self.path_reader.parse() self.labels = self.path_reader.parsed_data # Store path where to save output image data with labels and define CSVWriter object self.data_dir = workspace_dir+'user/training_datasets/'+self.dataset_name+'/' self.csv_writer = CSVWriter(self.data_dir+'data.csv') # Create storing folder for output image data mkdir(self.data_dir) # Store number of classes self.n_classes = len(unique(self.labels))
def write_unclassified_dataset_paths(dataset_path, dataset_name): dataset_dir = workspace_dir + 'user/unclassified_datasets/' + dataset_name mkdir(dataset_dir) images = os.listdir(dataset_path) n_images = len(images) writing_progress = bar.Bar("Writing image paths: ", max=n_images) with open(dataset_dir + '/paths.txt', 'a') as paths: for image in images: paths.write(dataset_path + '/' + image + '\n') writing_progress.next()
def write_training_dataset_paths(dataset_path, dataset_name): dataset_dir = workspace_dir + 'user/training_datasets/' + dataset_name mkdir(dataset_dir) folders = os.listdir(dataset_path) n_images = 0 for folder in folders: n_images += len(os.listdir(dataset_path + '/' + folder)) with open(dataset_dir + '/paths.txt', 'a') as paths: writing_progress = bar.Bar("Writing image paths: ", max=n_images) for folder in folders: images = os.listdir(dataset_path + '/' + folder) for tp in images: paths.write(dataset_path + '/' + folder + '/' + tp + '\n') writing_progress.next()
def __init__(self, image_data, data_name, dataset_name, img_dims): self.image_data = image_data self.data_name = data_name self.dataset_name = dataset_name self.metadata_writer = JSONWriter(self.data_name, dataset_metafile_path) self.img_dims = img_dims self.data_dir = base_unclassified_data_store_path+self.dataset_name+'/' self.csv_writer = CSVWriter(self.data_dir+'data.csv') mkdir(self.data_dir) self.n_classes = 0
def write_labels(dataset_path, dataset_name): dataset_dir = workspace_dir + 'user/training_datasets/' + dataset_name mkdir(dataset_dir) folders = os.listdir(dataset_path) n_images = 0 for folder in folders: n_images += len(os.listdir(dataset_path + '/' + folder)) class_id = {} with open(dataset_dir + '/labels.txt', 'a') as labels: writing_progress = bar.Bar("Writing image labels: ", max=n_images) for i, folder_name in enumerate(folders): for image in os.listdir(dataset_path + '/' + folder_name): labels.write(folder_name + '\n') writing_progress.next() class_id[i] = folder_name with open(dataset_dir + '/class_id.json', 'w') as txt: json.dump(class_id, txt, indent=3, sort_keys=True)
def write_predictions(self): mkdir(base_predictions_store_path) mkdir(base_predictions_store_path + '/' + self.model_name) mkdir(base_predictions_store_path + '/' + self.model_name + '/' + self.classification_name) for image_n in range(self.n_images): self.X_[image_n] = np.append( self.X_[image_n], self.predictions[self.image_paths[image_n]]) with open(self.raw_predictions_path, 'w') as csvfile: writer = csv.writer(csvfile, delimiter=',') for image_prediction in self.X_: writer.writerow(image_prediction) with open(self.predictions_paths_path, 'w') as pathfile: writer = csv.writer(pathfile, delimiter=',') for n in range(self.n_images): writer.writerow([ self.image_paths[n], self.predictions[self.image_paths[n]] ]) self.image_paths = np.asarray(self.image_paths) np.random.shuffle(self.image_paths) if self.show_images: for n in range(self.max_images): display_image(self.predictions[self.image_paths[n]], self.image_paths[n])