aug

Classes

RotationModes

Functions

batch_random_crops_fraction(img_ann_pairs, ...)

rtype

List[Tuple[ndarray, Annotation]]

crop(img, ann[, top_pad, left_pad, ...])

Crops an Image and Annotation from all sides with a given values.

crop_fraction(img, ann[, top, left, bottom, ...])

Crops an Image and Annotation from all sides with the given fraction values.

flip_add_random_crops(img, ann, ...)

rtype

List[Tuple[ndarray, Annotation]]

fliplr(img, ann)

Flips an Image and Annotation around vertical axis.

flipud(img, ann)

Flips an Image and Annotation around horizontal axis.

instance_crop(img, ann, class_title[, ...])

Crops objects of specified classes from Image and Annotation with configurable padding.

load_imgaug(json_data)

rtype

imgaug.augmenters.Sequential

random_crop(img, ann, height, width)

Crops an Image and Annotation at a random location.

random_crop_fraction(img, ann, ...)

Crops an Image and Annotation at a random location with random size in a given interval.

resize(img, ann, size)

Resizes an input Image and Annotation to a given size.

rotate(img, ann, degrees[, mode])

Rotates an Image and Annotation by random angle.

scale(img, ann[, frow, fcol, f])

Scales an input Image and Annotation to a given size.

Description

Augmentations for images and annotations

crop(img, ann, top_pad=0, left_pad=0, bottom_pad=0, right_pad=0)[source]

Crops an Image and Annotation from all sides with a given values.

Parameters
img : np.ndarray

Image in numpy format, RGB.

ann : Annotation

Annotation object.

top_pad : int, optional

Top padding in pixels.

left_pad : int, optional

Left padding in pixels.

bottom_pad : int, optional

Bottom padding in pixels.

right_pad : int, optional

Right padding in pixels.

Raises

RuntimeError if Image shape does not match img_size in Annotation

Returns

Tuple containing cropped Image and Annotation

Return type

Tuple[np.ndarray, Annotation]

Usage Example
import supervisely as sly
from supervisely.aug.aug import crop

address = 'https://app.supervise.ly/'
token = 'Your Supervisely API Token'
api = sly.Api(address, token)

# Download image and annotation from API
project_id = 116501
image_id = 193940171

meta_json = api.project.get_meta(project_id)
meta = sly.ProjectMeta.from_json(meta_json)

image_np = api.image.download_np(image_id) # <class 'numpy.ndarray'>
print(image_np.shape)
# Output: (800, 1067, 3)

ann_info = api.annotation.download(image_id)
ann = sly.Annotation.from_json(ann_info.annotation, meta)

crop_image_np, crop_ann = crop(image_np, ann, top_pad=50, left_pad=100, bottom_pad=50, right_pad=100)
print(crop_image_np.shape)
# Output: (700, 867, 3)
crop_fraction(img, ann, top=0, left=0, bottom=0, right=0)[source]

Crops an Image and Annotation from all sides with the given fraction values.

Parameters
img : np.ndarray

Image in numpy format, RGB.

ann : Annotation

Annotation object.

top : int, optional

Top padding in pixels.

left : int, optional

Left padding in pixels.

bottom : int, optional

Bottom padding in pixels.

right : int, optional

Right padding in pixels.

Raises

ValueError if fraction values not between 0 and 1

Returns

Tuple containing cropped Image and Annotation

Return type

Tuple[np.ndarray, Annotation]

Usage Example
import supervisely as sly
from supervisely.aug.aug import crop_fraction

address = 'https://app.supervise.ly/'
token = 'Your Supervisely API Token'
api = sly.Api(address, token)

# Download image and annotation from API
project_id = 116501
image_id = 193940171

meta_json = api.project.get_meta(project_id)
meta = sly.ProjectMeta.from_json(meta_json)

image_np = api.image.download_np(image_id) # <class 'numpy.ndarray'>
print(image_np.shape)
# Output: (800, 1067, 3)

ann_info = api.annotation.download(image_id)
ann = sly.Annotation.from_json(ann_info.annotation, meta)

crop_image_np, crop_ann = crop_fraction(image_np, ann, top=0.1, left=0.2, bottom=0.1, right=0.2)
print(crop_image_np.shape)
# Output: (640, 641, 3)
fliplr(img, ann)[source]

Flips an Image and Annotation around vertical axis.

Parameters
img : np.ndarray

Image in numpy format, RGB.

ann : Annotation

Annotation object.

Raises

RuntimeError if Image shape does not match img_size in Annotation

Returns

Tuple containing flipped Image and Annotation

Return type

Tuple[np.ndarray, Annotation]

Usage Example
import supervisely as sly
from supervisely.aug.aug import fliplr

address = 'https://app.supervise.ly/'
token = 'Your Supervisely API Token'
api = sly.Api(address, token)

# Download image and annotation from API
project_id = 116501
image_id = 193940171

meta_json = api.project.get_meta(project_id)
meta = sly.ProjectMeta.from_json(meta_json)

image_np = api.image.download_np(image_id) # <class 'numpy.ndarray'>
ann_info = api.annotation.download(image_id)
ann = sly.Annotation.from_json(ann_info.annotation, meta)

# Flip image and annotation
flip_image_np, flip_ann = fliplr(image_np, ann)
flipud(img, ann)[source]

Flips an Image and Annotation around horizontal axis.

Parameters
img : np.ndarray

Image in numpy format, RGB.

ann : Annotation

Annotation object.

Raises

RuntimeError if Image shape does not match img_size in Annotation

Returns

Tuple containing flipped Image and Annotation

Return type

Tuple[np.ndarray, Annotation]

Usage Example
import supervisely as sly
from supervisely.aug.aug import flipud

address = 'https://app.supervise.ly/'
token = 'Your Supervisely API Token'
api = sly.Api(address, token)

# Download image and annotation from API
project_id = 116501
image_id = 193940171

meta_json = api.project.get_meta(project_id)
meta = sly.ProjectMeta.from_json(meta_json)

image_np = api.image.download_np(image_id) # <class 'numpy.ndarray'>
ann_info = api.annotation.download(image_id)
ann = sly.Annotation.from_json(ann_info.annotation, meta)

# Flip image and annotation
flip_image_np, flip_ann = flipud(image_np, ann)
instance_crop(img, ann, class_title, save_other_classes_in_crop=True, padding_config=None)[source]

Crops objects of specified classes from Image and Annotation with configurable padding.

Parameters
img : np.ndarray

Image in numpy format, RGB.

ann : Annotation

Annotation object.

class_title : str

Name of class to crop.

save_other_classes_in_crop : bool, optional

If True saves non-target classes in each cropped Annotation, otherwise don’t.

padding_config : dict, optional

Dict with padding.

Raises

ValueError if padding size format is incorrect

Returns

List of cropped (image numpy array, Annotation) pairs

Return type

List[Tuple[np.ndarray, Annotation]]

Usage Example
import supervisely as sly
from supervisely.aug.aug import instance_crop

address = 'https://app.supervise.ly/'
token = 'Your Supervisely API Token'
api = sly.Api(address, token)

# Download image and annotation from API
project_id = 116501
image_id = 193940171

meta_json = api.project.get_meta(project_id)
meta = sly.ProjectMeta.from_json(meta_json)

image_np = api.image.download_np(image_id) # <class 'numpy.ndarray'>
print(image_np.shape)
# Output: (800, 1067, 3)

ann_info = api.annotation.download(image_id)
ann = sly.Annotation.from_json(ann_info.annotation, meta)

result = instance_crop(image_np, ann, 'kiwi', True, {'top': '20px', 'left': '50px', 'bottom': '700px', 'right': '1000px'})
for crop_image_np, crop_ann in result:
    print(crop_image_np.shape)
    # Output: (270, 635, 3)
    #         (426, 345, 3)
random_crop(img, ann, height, width)[source]

Crops an Image and Annotation at a random location.

Parameters
img : np.ndarray

Image in numpy format, RGB.

ann : Annotation

Annotation object.

height : int, optional

Desired height of output crop.

width : int, optional

Desired width of output crop.

Raises

RuntimeError if Image shape does not match img_size in Annotation

Returns

Tuple containing cropped Image and Annotation

Return type

Tuple[np.ndarray, Annotation]

Usage Example
import supervisely as sly
from supervisely.aug.aug import random_crop

address = 'https://app.supervise.ly/'
token = 'Your Supervisely API Token'
api = sly.Api(address, token)

# Download image and annotation from API
project_id = 116501
image_id = 193940171

meta_json = api.project.get_meta(project_id)
meta = sly.ProjectMeta.from_json(meta_json)

image_np = api.image.download_np(image_id) # <class 'numpy.ndarray'>
print(image_np.shape)
# Output: (800, 1067, 3)

ann_info = api.annotation.download(image_id)
ann = sly.Annotation.from_json(ann_info.annotation, meta)

crop_image_np, crop_ann = random_crop(image_np, ann, height=500, width=700)
print(crop_image_np.shape)
# Output: (500, 700, 3)
random_crop_fraction(img, ann, height_fraction_range, width_fraction_range)[source]

Crops an Image and Annotation at a random location with random size in a given interval.

Parameters
img : np.ndarray

Image in numpy format, RGB.

ann : Annotation

Annotation object.

height_fraction_range : Tuple[float, float]

Range of relative values [0, 1] to select output height from.

width_fraction_range : Tuple[float, float]

Range of relative values [0, 1] to select output width from.

Raises

RuntimeError if Image shape does not match img_size in Annotation

Returns

Tuple containing cropped Image and Annotation

Return type

Tuple[np.ndarray, Annotation]

Usage Example
import supervisely as sly
from supervisely.aug.aug import random_crop_fraction

address = 'https://app.supervise.ly/'
token = 'Your Supervisely API Token'
api = sly.Api(address, token)

# Download image and annotation from API
project_id = 116501
image_id = 193940171

meta_json = api.project.get_meta(project_id)
meta = sly.ProjectMeta.from_json(meta_json)

image_np = api.image.download_np(image_id) # <class 'numpy.ndarray'>
print(image_np.shape)
# Output: (800, 1067, 3)

ann_info = api.annotation.download(image_id)
ann = sly.Annotation.from_json(ann_info.annotation, meta)

crop_image_np, crop_ann = random_crop_fraction(image_np, ann, height_fraction_range=(0.1, 0.8), width_fraction_range=(0.1, 0.8))
print(crop_image_np.shape)
# Output: (486, 585, 3)
resize(img, ann, size)[source]

Resizes an input Image and Annotation to a given size.

Parameters
img : np.ndarray

Image in numpy format, RGB.

ann : Annotation

Annotation object.

size : Tuple[int, int]

Desired size (height, width) in pixels or -1.

Raises

RuntimeError if Image shape does not match img_size in Annotation

Returns

Tuple containing resized Image and Annotation

Return type

Tuple[np.ndarray, Annotation]

Usage Example
import supervisely as sly
from supervisely.aug.aug import resize

address = 'https://app.supervise.ly/'
token = 'Your Supervisely API Token'
api = sly.Api(address, token)

# Download image and annotation from API
project_id = 116501
image_id = 193940171

meta_json = api.project.get_meta(project_id)
meta = sly.ProjectMeta.from_json(meta_json)

image_np = api.image.download_np(image_id) # <class 'numpy.ndarray'>
print(image_np.shape)
# Output: (800, 1067, 3)

ann_info = api.annotation.download(image_id)
ann = sly.Annotation.from_json(ann_info.annotation, meta)

resize_image_np, resize_ann = resize(image_np, ann, (600, -1))
print(resize_image_np.shape)
# Output: (600, 800, 3)
rotate(img, ann, degrees, mode='keep')[source]

Rotates an Image and Annotation by random angle.

Parameters
img : np.ndarray

Image in numpy format, RGB.

ann : Annotation

Annotation object.

degrees : int

Rotation angle.

mode : RotationModes, optional

One of RotateMode enum values.

Raises

RuntimeError if Image shape does not match img_size in Annotation

Returns

Tuple containing rotated Image and Annotation

Return type

Tuple[np.ndarray, Annotation]

Usage Example
import supervisely as sly
from supervisely.aug.aug import rotate

address = 'https://app.supervise.ly/'
token = 'Your Supervisely API Token'
api = sly.Api(address, token)

# Download image and annotation from API
project_id = 116501
image_id = 193940171

meta_json = api.project.get_meta(project_id)
meta = sly.ProjectMeta.from_json(meta_json)

image_np = api.image.download_np(image_id) # <class 'numpy.ndarray'>
print(image_np.shape)
# Output: (800, 1067, 3)

ann_info = api.annotation.download(image_id)
ann = sly.Annotation.from_json(ann_info.annotation, meta)

rotate_image_np, rotate_ann = rotate(image_np, ann, 30)
print(rotate_image_np.shape)
# Output: (1231, 1326, 3)
scale(img, ann, frow=None, fcol=None, f=None)[source]

Scales an input Image and Annotation to a given size.

Parameters
img : np.ndarray

Image in numpy format, RGB.

ann : Annotation

Annotation object.

frow : float, optional

Desired height scale height value.

fcol : float, optional

Desired width scale height value.

f : float, optional

Desired height and width scale values in one(positive).

Raises

RuntimeError if Image shape does not match img_size in Annotation

Returns

Tuple containing scaled Image and Annotation

Return type

Tuple[np.ndarray, Annotation]

Usage Example
import supervisely as sly
from supervisely.aug.aug import scale

address = 'https://app.supervise.ly/'
token = 'Your Supervisely API Token'
api = sly.Api(address, token)

# Download image and annotation from API
project_id = 116501
image_id = 193940171

meta_json = api.project.get_meta(project_id)
meta = sly.ProjectMeta.from_json(meta_json)

image_np = api.image.download_np(image_id) # <class 'numpy.ndarray'>
print(image_np.shape)
# Output: (800, 1067, 3)

ann_info = api.annotation.download(193940171)
ann = sly.Annotation.from_json(ann_info.annotation, meta)

scale_image_np, scale_ann = scale(image_np, ann, frow=0.7, fcol=0.8)
print(scale_image_np.shape)
# Output: (560, 854, 3)