Luan Souza
Luan Souza

Reputation: 175

How to use the python method "ImageDataGenerator" and save the augmented images in a variable?

I'm constructing an augmented database to improve my CNN. The scheme is:

The code above shows what I'm talking about. Take a look at the parameter "save_to_dir"... If I neglect it the processing is made but the data isn't saved anywhere. Can anyone help me?

import numpy as np
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras.preprocessing.image import ImageDataGenerator
import matplotlib.pyplot as plt
import cv2

IMAGE_PATH = "---"
OUTPUT_PATH = "---"

image = cv2.imread(IMAGE_PATH)
plt.imshow(image)

image = np.expand_dims(image, axis=0)

imgAug = ImageDataGenerator(rotation_range=360, width_shift_range=0.1, height_shift_range=0.1, zoom_range=0.20, fill_mode='wrap',     horizontal_flip=True, vertical_flip=True)

imgGen = imgAug.flow(image, save_to_dir=OUTPUT_PATH,
                     save_format='png', save_prefix='dentezudo_')

counter = 0
for (i, newImage) in enumerate(imgGen):
    counter += 1

    if counter == 10:
        break

Upvotes: 2

Views: 1321

Answers (1)

Florentin Hennecker
Florentin Hennecker

Reputation: 2164

The function .flow() returns a generator that you can iterate over (like you do in your code) to get your images. In your code, the augmented images will be assigned to newImage.

According to the docs, flow() can also save the images to disk:

save_to_dir: None or str (default: None). This allows you to optionally specify a directory to which to save the augmented pictures being generated (useful for visualizing what you are doing).

Upvotes: 1

Related Questions