I have 2 images (cat and dog) and I want to use ImageDataGenerator
to increase the number of images as follows:
import keras
import cv2
import numpy as np
import os
from keras.preprocessing.image import ImageDataGenerator
from keras.utils import img_to_array
from os import listdir
from os.path import join, isfile
datagen = ImageDataGenerator(
width_shift_range=0.15,
height_shift_range=0.25
)
# Directory folder
Dir_img = '/content/drive/MyDrive/Test'
files = os.listdir(Dir_img)
print(files) # ['cat.jpg', 'dog.jpg']
After that, I use OpenCV
to read images from Dir_img and keep array of each image into list as follows:
dataset = []
# type of dataset is list and consist of 2 arrays of images (cat and dog)
for img in files:
img_ = cv2.imread(os.path.join(Dir_img,img), cv2.IMREAD_UNCHANGED) # default BGR
Org_ = cv2.cvtColor(img_, cv2.COLOR_BGR2RGB) # Convert BGR -> RGB
dataset.append(Org_)
Next, I want to reshape
from (W,H,3)
to (1,W,H,3)
Array = [] # len(Array) = 2 consist of Array[0] = cat, Array[1] = dog
for IMG in dataset:
#print(IMG.shape) # cat: (666,1000,3), dog: (825,1100,3)
ARR_ = img_to_array(IMG)
ARR_ = ARR_.reshape((1,) + ARR_.shape)
Array.append(ARR_)
#print(ARR_.shape) # cat: (1, 666, 1000, 3), dog: (1, 825, 1100, 3)
Finally, I use ImageDataGenerator.flow
to create images.
i = 0
for k in datagen.flow(Array,
batch_size = 2,
seed = 2,
save_to_dir = '/content/drive/MyDrive/Test/Augmentation',
save_prefix = 'Aug',
save_format = 'png'
):
i += 1
if i >= 4:
break
I received 4 more images of cats but I didn’t receive images of dogs.
Why is the dog image not added? Please guide me.