Augmentation

supervisely_lib.aug.aug.fliplr(img: numpy.ndarray, ann: supervisely_lib.annotation.annotation.Annotation) → Tuple[numpy.ndarray, supervisely_lib.annotation.annotation.Annotation][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
from supervisely_lib.aug.aug import fliplr

# 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)
supervisely_lib.aug.aug.flipud(img: numpy.ndarray, ann: supervisely_lib.annotation.annotation.Annotation) → Tuple[numpy.ndarray, supervisely_lib.annotation.annotation.Annotation][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
from supervisely_lib.aug.aug import flipud

# 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)
supervisely_lib.aug.aug.crop(img: numpy.ndarray, ann: supervisely_lib.annotation.annotation.Annotation, top_pad: int = 0, left_pad: int = 0, bottom_pad: int = 0, right_pad: int = 0) → Tuple[numpy.ndarray, supervisely_lib.annotation.annotation.Annotation][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
from supervisely_lib.aug.aug import crop

# 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)
supervisely_lib.aug.aug.crop_fraction(img: numpy.ndarray, ann: supervisely_lib.annotation.annotation.Annotation, top: float = 0, left: float = 0, bottom: float = 0, right: float = 0) → Tuple[numpy.ndarray, supervisely_lib.annotation.annotation.Annotation][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
from supervisely_lib.aug.aug import crop_fraction

# 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)
supervisely_lib.aug.aug.random_crop(img: numpy.ndarray, ann: supervisely_lib.annotation.annotation.Annotation, height: int, width: int) → Tuple[numpy.ndarray, supervisely_lib.annotation.annotation.Annotation][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
from supervisely_lib.aug.aug import random_crop

# 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)
supervisely_lib.aug.aug.random_crop_fraction(img: numpy.ndarray, ann: supervisely_lib.annotation.annotation.Annotation, height_fraction_range: Tuple[float, float], width_fraction_range: Tuple[float, float]) → Tuple[numpy.ndarray, supervisely_lib.annotation.annotation.Annotation][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
from supervisely_lib.aug.aug import random_crop_fraction

# 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)
supervisely_lib.aug.aug.batch_random_crops_fraction(img_ann_pairs: list, crops_per_image: int, height_fraction_range: tuple, width_fraction_range: tuple) → list[source]
supervisely_lib.aug.aug.flip_add_random_crops(img: numpy.ndarray, ann: supervisely_lib.annotation.annotation.Annotation, crops_per_image: int, height_fraction_range: tuple, width_fraction_range: tuple) → list[source]
supervisely_lib.aug.aug.instance_crop(img: numpy.ndarray, ann: supervisely_lib.annotation.annotation.Annotation, class_title: str, save_other_classes_in_crop: bool = True, padding_config: Optional[dict] = None) → List[Tuple[numpy.ndarray, supervisely_lib.annotation.annotation.Annotation]][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
from supervisely_lib.aug.aug import instance_crop

# 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)
supervisely_lib.aug.aug.resize(img: numpy.ndarray, ann: supervisely_lib.annotation.annotation.Annotation, size: Tuple[int, int]) → Tuple[numpy.ndarray, supervisely_lib.annotation.annotation.Annotation][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
from supervisely_lib.aug.aug import resize

# 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)
supervisely_lib.aug.aug.scale(img: numpy.ndarray, ann: supervisely_lib.annotation.annotation.Annotation, frow: Optional[float] = None, fcol: Optional[float] = None, f: Optional[float] = None) → Tuple[numpy.ndarray, supervisely_lib.annotation.annotation.Annotation][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
from supervisely_lib.aug.aug import scale

# 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)
class supervisely_lib.aug.aug.RotationModes[source]

Bases: object

KEEP = 'keep'
CROP = 'crop'
supervisely_lib.aug.aug.rotate(img: numpy.ndarray, ann: supervisely_lib.annotation.annotation.Annotation, degrees: int, mode: str = 'keep') → Tuple[numpy.ndarray, supervisely_lib.annotation.annotation.Annotation][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
from supervisely_lib.aug.aug import rotate

# 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)