I have about 1000 images in a cvs file. I have already managed to put those images in my Python programm by doing the following:
df = pd.read_csv("./Testes_small.csv") # Creates the dataframe training_set = pd.DataFrame({'Images': training_imgs,'Labels': training_labels}) train_dataGen = ImageDataGenerator(rescale=1./255) train_generator = train_dataGen.flow_from_dataframe(dataframe = training_set, directory="", x_col="Images", y_col="Labels", class_mode="categorical", target_size=(224, 224),batch_size=32) ##Steps to plot the images imgs,labels = next(train_generator) for i in range(batch_size): # range de 0 a 31 image = imgs[i] plt.imshow(image) plt.show() So now I have the train_generator variable of type python.keras.preprocessing.image.DataframeIterator Its size is (32,224,224,3).
In the function ImageDataGenerator I want to put my own preprocessing function to resize the images. I want to do this because I have some rectangular images that when resized lose its ratio.
Per examples these images before(upper image) and after(the lower one) resizing:
Clearly the secong image loses shape
I found this function(it's the answer to a previous thread):
def resize_image(self, image: Image, length: int) -> Image: """ Resize an image to a square. Can make an image bigger to make it fit or smaller if it doesn't fit. It also crops part of the image. :param self: :param image: Image to resize. :param length: Width and height of the output image. :return: Return the resized image. """ """ Resizing strategy : 1) We resize the smallest side to the desired dimension (e.g. 1080) 2) We crop the other side so as to make it fit with the same length as the smallest side (e.g. 1080) """ if image.size[0] < image.size[1]: # The image is in portrait mode. Height is bigger than width. # This makes the width fit the LENGTH in pixels while conserving the ration. resized_image = image.resize((length, int(image.size[1] * (length / image.size[0])))) # Amount of pixel to lose in total on the height of the image. required_loss = (resized_image.size[1] - length) # Crop the height of the image so as to keep the center part. resized_image = resized_image.crop( box=(0, required_loss / 2, length, resized_image.size[1] - required_loss / 2)) # We now have a length*length pixels image. return resized_image else: # This image is in landscape mode or already squared. The width is bigger than the heihgt. # This makes the height fit the LENGTH in pixels while conserving the ration. resized_image = image.resize((int(image.size[0] * (length / image.size[1])), length)) # Amount of pixel to lose in total on the width of the image. required_loss = resized_image.size[0] - length # Crop the width of the image so as to keep 1080 pixels of the center part. resized_image = resized_image.crop( box=(required_loss / 2, 0, resized_image.size[0] - required_loss / 2, length)) # We now have a length*length pixels image. return resized_image I'm trying to insert it like this img_datagen = ImageDataGenerator(rescale=1./255, preprocessing_function = resize_image but it doesn't work because I'm not giving an im. Do you have any ideas on how can I do this?
没有评论:
发表评论