Skip to content Skip to sidebar Skip to footer

Augmented Images Does Not Store In Their Own Classes Directory With Raw Data That Is Presented Into The Train Folder

I am working on image data augmentation for the train set data and I have been writing code of augmentation. I have 12 classes in the dataset i.e. Grass, Flower, Fruits, Dust, and

Solution 1:

solution code is shown below

import tensorflow as tf
import cv2
import os
import numpy as np
from tensorflow.keras.preprocessing.image import ImageDataGenerator
sdir= r'c:\temp\people\dtest'# set this to the directory holding the images
ext='jpg'# specify the extension foor the aufmented images
prefix='aug'#set the prefix for the augmented images
batch_size=32# set the batch size
passes=5# set the number of time to cycle the generator
datagen = ImageDataGenerator( rotation_range=45, width_shift_range=0.2, height_shift_range=0.2, shear_range = 0.2,
                                zoom_range = 0.2,  horizontal_flip=True, fill_mode = 'nearest')
data=datagen.flow_from_directory(directory = sdir, batch_size = batch_size,  target_size = (256, 256),
                                 color_mode = 'rgb', shuffle=True)
for i inrange (passes):
    images, labels=next(data)
    class_dict=data.class_indices
    new_dict={}
    # make a new dictionary with keys and values reversedfor key, value in class_dict.items(): # dictionary is now {numeric class label: string of class_name}
        new_dict[value]=key    
    for j inrange (len(labels)):                
        class_name = new_dict[np.argmax(labels[j])]         
        dir_path=os.path.join(sdir,class_name )         
        new_file=prefix + '-' +str(i*batch_size +j) + '.'  + ext       
        img_path=os.path.join(dir_path, new_file)        
        img=cv2.cvtColor(images[j], cv2.COLOR_BGR2RGB)
        cv2.imwrite(img_path, img)
print ('*** process complete')  

this will create the augmented images and store them in the associated class directories.

Post a Comment for "Augmented Images Does Not Store In Their Own Classes Directory With Raw Data That Is Presented Into The Train Folder"