diff --git a/iss/tools/tools.py b/iss/tools/tools.py index 27a76ea..35d4ef9 100644 --- a/iss/tools/tools.py +++ b/iss/tools/tools.py @@ -2,6 +2,9 @@ import PIL import os +import numpy as np +from keras_preprocessing.image.utils import load_img + class Tools: @@ -29,4 +32,48 @@ class Tools: os.makedirs(path) return path + @staticmethod + def encoded_pictures_from_generator(generator, model): + + predictions_tuple = tuple([model.get_encoded_prediction(imgs) for imgs in generator]) + predictions = np.concatenate(predictions_tuple, axis = 0) + + return predictions + + @staticmethod + def read_np_picture(path, target_size = None, scale = 1): + # img = PIL.Image.open(filename) + img = load_img(path, target_size = target_size) + img_np = np.asarray(img, dtype = 'uint8') + img_np = img_np * scale + return img_np + + @staticmethod + def list_directory_filenames(path): + filenames = os.listdir(path) + filenames = [path + f for f in filenames] + + return filenames + + @staticmethod + def generator_np_picture_from_filenames(filenames, target_size = None, scale = 1, batch = 124, nb_batch = None): + + max_n = len(filenames) + div = np.divmod(max_n, batch) + + if nb_batch is None: + nb_batch = div[0] + 1 * (div[1] != 0) + + for i in range(nb_batch): + # for i in [75, 76]: + i_debut = i*batch + i_fin = min(i_debut + batch, max_n) + print("i_debut:" + str(i_debut)) + print("i_fin:" + str(i_fin)) + yield np.array([Tools.read_np_picture(f, target_size, scale) for f in filenames[i_debut:i_fin]]) + + + + +