Package: augmento
Augmento: An Image Augmentation LibraryAugmento is a Python library that provides a collection of image augmentation techniques for machine learning applications. It offers a wide range of augmentation functions that can help improve the accuracy and robustness of computer vision models by introducing variations to the input data. With Augmento, you can easily incorporate different types of image augmentations in your data pipeline and train more effective models.The library is designed to be flexible and easy-to-use, with a user-friendly API that allows you to chain together different augmentation functions and customize their parameters to fit your specific use case. Augmento supports a variety of image formats and integrates well with popular machine learning frameworks like PyTorch and TensorFlow.Whether you're working on a computer vision project, training an object detection algorithm, or building a neural network for image classification, Augmento can help you create more diverse and representative training datasets and improve the performance of your models.InstallationTo install Augmento, you can use pip, a popular Python package manager. First, ensure that you have pip installed on your system. Then, run the following command:pipinstallaugmentoThis will download and install the latest version of Augmento and its dependencies. Once installation is complete, you can begin using the library in your Python projects.UsageAfter installation, you can import theAugmentoclass and the desired augmentation classes from theaugmento.augmentationsmodule.Here's an example of how to use theAugmentoclass to apply multiple augmentations to an image:fromaugmento.augmentationsimportColors,Resizing,RotationsfromaugmentoimportAugmentoimportcv2# Load the imageimage=cv2.imread("image.jpg")# Create an instance of the Augmento classaugmentor=Augmento()# Add augmentations to the pipelineaugmentor.add(augmentation=Colors.jitter())augmentor.add(augmentation=Resizing.cropper())augmentor.add(augmentation=Rotations.rotate())# Apply the augmentations to the imageaugmented=augmentor(image)# Display the augmented imagecv2.imshow("Augmented Image",augmented["image"])cv2.waitKey(0)cv2.destroyAllWindows()In the above example, we first loaded an image using OpenCV'simread()function. Then we created an instance of theAugmentoclass and added three augmentations to the pipeline using theadd()method. Finally, we applied the augmentations to the image by callingaugmentorand displayed the augmented image using OpenCV'simshow()function.You can also apply augmentations to annotations, if provided, by passing them as an additional argumentannotationsin both formats i.e. bounding boxes or points. For bounding boxes, the shape of annotations should be(n, 4)with each bounding box as[x1, y1, x2, y2]and(n, 2)for key-points annotations with each point as[x1, y1]wherenrepresents number of annotations in an image.fromaugmento.augmentationsimportColors,Resizing,RotationsfromaugmentoimportAugmentoimportcv2importnumpyasnpimportmatplotlib.pyplotasplt# Load the image and annotationsimage=cv2.imread("image.jpg")bboxes=[[500,1750,3600,2650]]# Create an instance of the Augmento classaugmentor=Augmento()# Add augmentations to the pipelineaugmentor.add(augmentation=Colors.jitter())augmentor.add(augmentation=Resizing.cropper())augmentor.add(augmentation=Rotations.rotate())# Apply the augmentations to the image and annotationsresults=augmentor(image,bboxes)# Display the augmented image and annotationsfig,plots=plt.subplots(1,2,figsize=(12,6))plots[1].set_title('Augmented')plots[1].imshow(results['image'])plots[1].axis('off')plots[0].set_title('Original')plots[0].imshow(image)plots[0].axis('off')plt.show()DocsAugmentoImport asfrom augmento import AugmentoTheAugmentoobject is the main object of theaugmentolibrary, which will be used as an image augmentation generator. It accepts a list of augmentation objects and then apply all those augmentations to the input images and annotations by calling it. It could generate a unique image every time based on settings of added augmentations.Methodsadd(augmentation)Theaddmethod is used to add an augmentation to theAugmentogenerator. It accepts a single argument, which is an instance of an augmentation class such asColors.jitter(),Resizing.cropper({}),Rotations.rotate({}), etc.Call Augmento ObjectTo apply the augmentations added in theAugmentogenerator, simply call the generator and it will apply the whole list of augmentations to the input image. It accepts two arguments:image: The input image to be augmented. The image should be a NumPy array with dimensions(height, width, channels).annotations(Optional): A 2D array of annotations in either bounding boxes or key-points format. For bounding boxes, the shape of annotations should be(n, 4)with each bounding box as[x1, y1, x2, y2]and(n, 2)for key-points annotations with each point as[x1, y1]wherenrepresents the number of annotations in an image.The method returns a dictionary with the following keys:'image': The augmented image as a NumPy array with dimensions(height, width, channels).'annotations': The augmented annotations (if any) as a numpy array.AugmentationsColorsimport asfrom augmento.augmentations import ColorsTheColorsprovides image color augmentation techniques.jitter(params={})Thejittermethod create color jittering augmentation.Arguments:params(dict): A dictionary of parameters for the colors jittering augmentation. The following key-value pairs are accepted:distribution(str, default='normal'): The type of distribution to use for generating the augmentation values.add_brightness(bool, default=True): Whether or not to add brightness augmentation.brightness_range(int, default=50): The range of brightness values to use for augmentation.brightness_value(int, default=None): A fixed brightness value to use for augmentation. IfNone, then a random value within the range will be chosen.add_contrast(bool, default=True): Whether or not to add contrast augmentation.contrast_range(int, default=20): The range of contrast values to use for augmentation.contrast_value(float, default=None): A fixed contrast value to use for augmentation. IfNone, then a random value within the range will be chosen.Returns:An instance of the Colors jittering augmentation.Resizingimport asfrom augmento.augmentations import ResizingIt provides augmentations for image resizing including following:cropper(params={})Create a cropping augmentation for an input image.Arguments:params(dict): A dictionary of parameters for the image cropping. The following key-value pairs are accepted:background(str, default=None): The path to the background image to use for cropped area. It will work withkeep_within_size=Trueand set the specified background for the cropped area.keep_within_size(bool, default=True): Whether to keep the output image within the same size as the input image.cropping_value(int, default=None): The amount of cropping to apply to the input image. IfNone, then a random cropping value will be set selected from a random normal distribution in the range of 0-360.Returns:An instance of thecropperaugmentation class.Rotationsimport asfrom augmento.augmentations import Rotationsrotate(params={}):Therotatemethod creates a rotation augmentation for an input image.Arguments:params(dict): A dictionary of parameters for the image rotation. The following key-value pairs are accepted:angle(int, default=None): The angle of rotation in degrees. If None, a random angle between -25 and 25 degrees will be selected.random_bg(bool, default=False): Whether to fill the background of the rotated image with a random color.Returns:Therotatemethod returns an instance of the rotation augmentation class, which can be added to an instance of theAugmentoclass for augmenting an image.Flipperimport asfrom augmento.augmentations import Flipperhorizontal_flip(prob):Thehorizontal_flipmethod creates a horizontal flipping augmentation for an input image.Arguments:prob(float): A value between 0 and 1 for the probability of applying this augmentation. Default value is1.0that means it will apply flipping augmentation to each image.Returns:Thehorizontal_flipmethod returns an instance of the horizontal flipping augmentation class.vertical_flip(prob):Thevertical_flipmethod creates a vertical flipping augmentation for an input image.Arguments:prob(float): A value between 0 and 1 for the probability of applying this augmentation. Default value is1.0that means it will apply flipping augmentation to each image.Returns:Thevertical_flipmethod returns an instance of the vertical flipping augmentation class.AffineTransformerimport asfrom augmento.augmentations import AffineTransformerwith_padding(scale_range, shift_range):Thewith_paddingmethod creates an affine transformation augmentation with keeping the padding area in the canvas for an input image.Arguments:scale_range(tuple): Range for image scaling. Default is(0.8, 1.2)i.e. the augmentation will select any random value between this range for scaling image.shift_range: Range for image shifting inside the canvas. Default value is(-0.1, 0.1)i.e. the augmentation will select any random value between this range and move the image accordingly.Returns:Thewith_paddingmethod returns an instance of the affine transformation augmentation class.without_padding(scale_range, shift_range):Thewithout_paddingmethod creates an affine transformation augmentation and remove the padding area. In other words, it will enable the scaling+cropping behaviour and every image generated by this augmentation could be in different image sizes.Arguments:scale_range(tuple): Range for image scaling. Default is(0.8, 1.2)i.e. the augmentation will select any random value between this range for scaling image.shift_range: Range for image shifting inside the canvas. Default value is(-0.1, 0.1)i.e. the augmentation will select any random value between this range and move the image accordingly.Returns:Thewithout_paddingmethod returns an instance of the affine transformation augmentation class.ExamplesFirst, let's import the necessary classes and libraries:fromaugmento.augmentationsimportAugmento,Colors,Rotationsimportcv2importnumpyasnpThen, let's load the image and its corresponding annotations:image_path='path/to/image.jpg'annotation_path='path/to/annotations.npy'# Load the imageimage=cv2.imread(image_path)# Load the annotationsannotations=np.load(annotation_path)Next, let's create an instance of theAugmentoclass and add the desired augmentations:# Create an instance of the Augmento classaugmentor=Augmento()# Add color jittering augmentationaugmentor.add(augmentation=Colors.jitter())# Add rotation augmentationaugmentor.add(augmentation=Rotations.rotate(params={'angle':45}))Now, we can apply the augmentations to the image and annotations# Apply the augmentationsaugmented=augmentor(image,annotations)That's it! You have successfully applied color jittering and rotation augmentation to an image with annotations using theaugmentolibrary.