aug¶
Classes
Enum-like class for rotation behavior: KEEP (pad to fit) or CROP. |
Functions
|
|
|
Crops an Image and Annotation from all sides with a given values. |
|
Crops an Image and Annotation from all sides with the given fraction values. |
|
|
|
Flips an Image and Annotation around vertical axis. |
|
Flips an Image and Annotation around horizontal axis. |
|
Crops objects of specified classes from Image and Annotation with configurable padding. |
|
|
|
Crops an Image and Annotation at a random location. |
|
Crops an Image and Annotation at a random location with random size in a given interval. |
|
Resizes an input Image and Annotation to a given size. |
|
Rotates an Image and Annotation by random angle. |
|
Scales an input Image and Annotation to a given size. |
Description
Augmentations for images and annotations
- class RotationModes[source]¶
Bases:
objectEnum-like class for rotation behavior: KEEP (pad to fit) or CROP.
-
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:
- 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 os from dotenv import load_dotenv import supervisely as sly from supervisely.aug.aug import crop # Load secrets and create API object from .env file (recommended) # Learn more here: https://developer.supervisely.com/getting-started/basics-of-authentication if sly.is_development(): load_dotenv(os.path.expanduser("~/supervisely.env")) api = sly.Api.from_env() # 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:
- 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 os from dotenv import load_dotenv import supervisely as sly from supervisely.aug.aug import crop_fraction # Load secrets and create API object from .env file (recommended) # Learn more here: https://developer.supervisely.com/getting-started/basics-of-authentication if sly.is_development(): load_dotenv(os.path.expanduser("~/supervisely.env")) api = sly.Api.from_env() # 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:
- 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 os from dotenv import load_dotenv import supervisely as sly from supervisely.aug.aug import fliplr # Load secrets and create API object from .env file (recommended) # Learn more here: https://developer.supervisely.com/getting-started/basics-of-authentication if sly.is_development(): load_dotenv(os.path.expanduser("~/supervisely.env")) api = sly.Api.from_env() # 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:
- 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 os from dotenv import load_dotenv import supervisely as sly from supervisely.aug.aug import flipud # Load secrets and create API object from .env file (recommended) # Learn more here: https://developer.supervisely.com/getting-started/basics-of-authentication if sly.is_development(): load_dotenv(os.path.expanduser("~/supervisely.env")) api = sly.Api.from_env() # 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:
- 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 os from dotenv import load_dotenv import supervisely as sly from supervisely.aug.aug import instance_crop # Load secrets and create API object from .env file (recommended) # Learn more here: https://developer.supervisely.com/getting-started/basics-of-authentication if sly.is_development(): load_dotenv(os.path.expanduser("~/supervisely.env")) api = sly.Api.from_env() # 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:
- 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 os from dotenv import load_dotenv import supervisely as sly from supervisely.aug.aug import random_crop # Load secrets and create API object from .env file (recommended) # Learn more here: https://developer.supervisely.com/getting-started/basics-of-authentication if sly.is_development(): load_dotenv(os.path.expanduser("~/supervisely.env")) api = sly.Api.from_env() # 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:
- 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 os from dotenv import load_dotenv import supervisely as sly from supervisely.aug.aug import random_crop_fraction # Load secrets and create API object from .env file (recommended) # Learn more here: https://developer.supervisely.com/getting-started/basics-of-authentication if sly.is_development(): load_dotenv(os.path.expanduser("~/supervisely.env")) api = sly.Api.from_env() # 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, skip_empty_masks=
False)[source]¶ Resizes an input Image and Annotation to a given size.
- Parameters:
- 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 os from dotenv import load_dotenv import supervisely as sly from supervisely.aug.aug import resize # Load secrets and create API object from .env file (recommended) # Learn more here: https://developer.supervisely.com/getting-started/basics-of-authentication if sly.is_development(): load_dotenv(os.path.expanduser("~/supervisely.env")) api = sly.Api.from_env() # 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:
- 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 os from dotenv import load_dotenv import supervisely as sly from supervisely.aug.aug import rotate # Load secrets and create API object from .env file (recommended) # Learn more here: https://developer.supervisely.com/getting-started/basics-of-authentication if sly.is_development(): load_dotenv(os.path.expanduser("~/supervisely.env")) api = sly.Api.from_env() # 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:
- 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 os from dotenv import load_dotenv import supervisely as sly from supervisely.aug.aug import scale # Load secrets and create API object from .env file (recommended) # Learn more here: https://developer.supervisely.com/getting-started/basics-of-authentication if sly.is_development(): load_dotenv(os.path.expanduser("~/supervisely.env")) api = sly.Api.from_env() # 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)