aleju / imgaug

Image augmentation for machine learning experiments.
http://imgaug.readthedocs.io
MIT License
14.43k stars 2.44k forks source link

Issue with imgaug.imwrite() #773

Closed NickosKal closed 3 years ago

NickosKal commented 3 years ago

Hello I am running the following code for image and boundind boxes augmentation:

`import re

import imgaug as ia
from IPython.display import display
from imgaug.augmentables.bbs import BoundingBoxesOnImage
from imgaug import augmenters as iaa
import imageio
import pandas as pd
from xml_to_csv import labels_df
ia.seed(1)

aug = iaa.SomeOf(6, [
    iaa.Affine(scale=(0.5, 1.5)),
    iaa.Affine(rotate=(-60, 60)),
    iaa.Affine(translate_percent={"x": (-0.3, 0.3), "y": (-0.3, 0.3)}),
    iaa.Fliplr(1),
    iaa.Multiply((0.5, 1.5)),
    iaa.GaussianBlur(sigma=(1.0, 3.0)),
    iaa.AdditiveGaussianNoise(scale=(0.03 * 255, 0.05 * 255))
])

def bbs_obj_to_df(bbs_object):
    #   convert BoundingBoxesOnImage object into array
    bbs_array = bbs_object.to_xyxy_array()

    #   convert array into a DataFrame ['xmin', 'ymin', 'xmax', 'ymax'] columns
    df_bbs = pd.DataFrame(bbs_array, columns=['xmin', 'ymin', 'xmax', 'ymax'])
    return df_bbs

def images_aug(df, images_path, aug_images_path, image_prefix, augmentor):
    #   create data frame which we're going to populate with augmented image info
    aug_bbs_xy = pd.DataFrame(columns=
                              ['filename', 'width', 'height', 'class', 'xmin', 'ymin', 'xmax', 'ymax']
                              )
    grouped = df.groupby('filename')

    for filename in df['filename'].unique():
        # Get separate data frame grouped by file name
        group_df = grouped.get_group(filename)
        group_df = group_df.reset_index()
        group_df = group_df.drop(['index'], axis=1)
        # read the image
        image = imageio.imread(images_path + filename)
        # get bounding boxes coordinates and write into array
        bb_array = group_df.drop(['filename', 'width', 'height', 'class'], axis=1).values
        # pass the array of bounding boxes coordinates to the imgaug library
        bbs = BoundingBoxesOnImage.from_xyxy_array(bb_array, shape=image.shape)
        # apply augmentation on image and on the bounding boxes
        image_aug, bbs_aug = augmentor(image=image, bounding_boxes=bbs)
        # disregard bounding boxes which have fallen out of image pane
        bbs_aug = bbs_aug.remove_out_of_image()
        # clip bounding boxes which are partially outside of image pane
        bbs_aug = bbs_aug.clip_out_of_image()
        #
        # don't perform any actions with the image if there are no bounding boxes left in it
        if re.findall('Image....', str(bbs_aug)) == ['Image([]']:
            pass

        # otherwise continue
        else:
            # write augmented image to a file
            imageio.imwrite(aug_images_path+image_prefix+filename, image_aug)
            # create a data frame with augmented values of image width and height
            info_df = group_df.drop(['xmin', 'ymin', 'xmax', 'ymax'], axis=1)
            for index, _ in info_df.iterrows():
                info_df.at[index, 'width'] = image_aug.shape[1]
                info_df.at[index, 'height'] = image_aug.shape[0]
            # rename filenames by adding the predifined prefix
            info_df['filename'] = info_df['filename'].apply(lambda x: image_prefix + x)
            # create a data frame with augmented bounding boxes coordinates
            bbs_df = bbs_obj_to_df(bbs_aug)
            # concat all new augmented info into new data frame
            aug_df = pd.concat([info_df, bbs_df], axis=1)
            # append rows to aug_bbs_xy data frame
            aug_bbs_xy = pd.concat([aug_bbs_xy, aug_df])

    # return dataframe with updated images and bounding boxes annotations
    aug_bbs_xy = aug_bbs_xy.reset_index()
    aug_bbs_xy = aug_bbs_xy.drop(['index'], axis=1)
    return aug_bbs_xy

augmented_images_df = images_aug(labels_df, r"....\Image Augmentation\images/",
                                r"....\Image Augmentation\aug_images",
                                "aug1_", aug)
display(augmented_images_df)`

But the augmented images are not saved in the aug_images folder that I want them but instead they appear in where my .py is anyone I have checked the paths and they are all correct anyone knows what may cause the issue. Thanks in advance.