-2

I have generated many images like number plate as below[![enter image description here] Now, want to convert all such images like real world vehicle number plate image. For example- enter image description here How to these type of augmentation and save the all the augmented images in another folder.

1 Answer 1

2

Solution

Check out the library: albumentations. Try to answer the question: "what is the difference between the image you have and the image you want?". For instance, that image is :

  • more pixelated,
  • grainy,
  • has lower resolution,
  • also could have nails/fastening screws on it
  • may have something else written under or over the main number
  • may have shadows on it
  • the number plate may be unevenly bright at places, etc.

Albumentations, helps you come up with many types of image augmentations. Please try to break down this problem like I suggested and then try and find out which augemntations you need there from albumentations.

Example of image augmentation using albumentations

The following code block (source) shows you how to apply albumentations for image augmentation. In case you had an image and a mask, both of them will undergo identical transformations.

Another example from kaggle: Image Augmentation Demo with albumentation

from albumentations import (
    HorizontalFlip, IAAPerspective, ShiftScaleRotate, CLAHE, RandomRotate90,
    Transpose, ShiftScaleRotate, Blur, OpticalDistortion, GridDistortion, HueSaturationValue,
    IAAAdditiveGaussianNoise, GaussNoise, MotionBlur, MedianBlur, IAAPiecewiseAffine,
    IAASharpen, IAAEmboss, RandomBrightnessContrast, Flip, OneOf, Compose
)
import numpy as np

def strong_aug(p=0.5):
    return Compose([
        RandomRotate90(),
        Flip(),
        Transpose(),
        OneOf([
            IAAAdditiveGaussianNoise(),
            GaussNoise(),
        ], p=0.2),
        OneOf([
            MotionBlur(p=0.2),
            MedianBlur(blur_limit=3, p=0.1),
            Blur(blur_limit=3, p=0.1),
        ], p=0.2),
        ShiftScaleRotate(shift_limit=0.0625, scale_limit=0.2, rotate_limit=45, p=0.2),
        OneOf([
            OpticalDistortion(p=0.3),
            GridDistortion(p=0.1),
            IAAPiecewiseAffine(p=0.3),
        ], p=0.2),
        OneOf([
            CLAHE(clip_limit=2),
            IAASharpen(),
            IAAEmboss(),
            RandomBrightnessContrast(),
        ], p=0.3),
        HueSaturationValue(p=0.3),
    ], p=p)

image = np.ones((300, 300, 3), dtype=np.uint8)
mask = np.ones((300, 300), dtype=np.uint8)
whatever_data = "my name"
augmentation = strong_aug(p=0.9)
data = {"image": image, "mask": mask, "whatever_data": whatever_data, "additional": "hello"}
augmented = augmentation(**data)
image, mask, whatever_data, additional = augmented["image"], augmented["mask"], augmented["whatever_data"], augmented["additional"]

Strategy

  • First tone down the number of augmentations to a bare minimum
  • Save a single augmented-image
  • Save a few images post augmentation.
  • Now test and update your augmentation pipeline to suit your requirements of mimicking the ground-truth scenario.
  • finalize your pipeline and run it on a larger number of images.
  • Time it: how long this takes for how many images.
  • Then finally run it on all the images: this time you can have a time estimate on how long it is going to take to run it.

NOTE: every time an image passes through the augmentation pipeline, only a single instance of augmented image comes out of it. So, say you want 10 different augmented versions of each image, you will need to pass each image through the augmentation pipeline 10 times, before moving on to the next image.

# this will not be what you end up using
# but you can begin to understand what 
# you need to do with it.

def simple_aug(p-0,5):
    return return Compose([
        RandomRotate90(),
        # Flip(),
        # Transpose(),
        OneOf([
            IAAAdditiveGaussianNoise(),
            GaussNoise(),
        ], p=0.2),
    ])

# for a single image: check first
image = ... # write your code to read in your image here
augmentation = strong_aug(p=0.5)

augmented = augmentation({'image': image}) # see albumentations docs
# SAVE the image
# If you are using imageio or PIL, saving an image 
# is rather straight forward, and I will let you
# figure that out.
# save the content of the variable: augmented['image']

For multiple images

Assuming each image passing 10 times through the augmentation pipeline, your code could look like as follows:

import os

# I assume you have a way of loading your 
# images from the filesystem, and they come 
# out of `images` (an iterator)

NUM_AUG_REPEAT = 10
AUG_SAVE_DIR = 'data/augmented'

# create directory of not present already
if not os.path.isdir(AUG_SAVE_DIR):
    os.makedirs(AUG_SAVE_DIR)

# This will create augmentation ids for the same image
# example: '00', '01', '02', ..., '08', '09' for
#          - NUM_AUG_REPEAT = 10
aug_id = lambda x: str(x).zfill(len(str(NUM_AUG_REPEAT)))

for image in images:
    for i in range(NUM_AUG_REPEAT):
        data = {'image': image}
        augmented = augmentation(**data)
        # I assume you have a function: save_image(image_path, image)
        # You need to write this function with 
        # whatever logic necessary. (Hint: use imageio or PIL.Image)
        image_filename = f'image_name_{aug_id(i)}.png'
        save_image(os.path.join(AUG_SAVE_DIR, image_filename), augmented['image'])
5
  • augmentation I can try but as I have 1000 such images to augment ..I am not able to do augmentation on all images once and save all augmented images to seperate folder can you help me with that.
    – k_p
    Jul 9, 2021 at 22:08
  • @CypherX i am also stucked in same kind of augmentation and applying augmentation on all the images in folder and saving in seperate folder it will be great help if you elaborate on this
    – pk_11
    Jul 9, 2021 at 22:20
  • 1
    @k_p looks like you have a file management issue rather than an image processing issue. Sound like you simply need a for-loop to say "for in_img in input_folder: out_img = do_augmentation(in_img); save(out_img, output_folder)". If so, please show your image reading/saving code to get help.
    – Guang
    Jul 9, 2021 at 23:26
  • @k_p You can either augment them on the fly and (assuming you want to train some image model) feed them to the model you train. Alternatively, you can apply augmentation to these images, and then save them to a folder. Granted, it would run for a while, but it's just a matter of time to get all of them pass through your augmentation routine. I would suggest you try with something very basic (simple augmentation) and then develop on top of it.
    – CypherX
    Jul 10, 2021 at 1:56
  • @k_p I updated the answer. Please check it out. I hope this helps.
    – CypherX
    Jul 10, 2021 at 2:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.