Answers for "how to create and save augmented images"

0

how to create and save augmented images

### This is just for my personal reference, 
# This functions assume you have a dataframe with your image id and their labels
# It randomly pick some images and create specified number of augmented images 
# out of it, and will save it in specified directory
def img_aug(df, label, amount, copies=3):
    """This function will create augmented images"""
    # data agumentation
    datagen = ImageDataGenerator(rotation_range=40, vertical_flip=True, horizontal_flip=True,    
                                width_shift_range=0.2, height_shift_range=0.2, shear_range=0.2,
                                zoom_range=0.2, fill_mode='nearest')
    
    # selecting random image
    to_select = int(amount/copies)
    df = df[df['Target']==label].sample(to_select, random_state=0)
    img_id = list('Images/' + df['Image_ID'] + '.tif')

    # creating augmented image
    for i in range(len(img_id)):
        
        # processing image
        img = load_img(img_id[i])  
        x = img_to_array(img)  
        x = x.reshape((1,) + x.shape) 

        # prepare iterator
        path = 'train/'+str(label)+'/'
        it = datagen.flow(x, batch_size=1, save_to_dir=path,
                           save_prefix='AUG', save_format='jpeg')
        
        # making specified number of copies
        i = 0
        for batch in it:
            i += 1
            if i >= copies:
                break
            
            # progress 
            clear_output(wait=True)
Posted by: Guest on October-12-2021

Code answers related to "how to create and save augmented images"

Python Answers by Framework

Browse Popular Code Answers by Language