Label

class Label[source]

Bases: supervisely.annotation.label.LabelBase

Methods

add_tag

Adds Tag to the current Label.

add_tags

Adds multiple Tags to the current Label.

clone

Makes a copy of Label with new fields, if fields are given, otherwise it will use fields of the original Label.

convert

Converts Label geometry to another geometry shape.

crop

Clones the current Label and crops it.

draw

Draws current Label on image.

draw_contour

Draws Label geometry contour on the given image.

fliplr

Clones the current Label and flips it horizontally.

flipud

Clones the current Label and flips it vertically.

from_json

Convert a json dict to Label.

get_mask

Returns 2D boolean mask of the label.

relative_crop

Clones the current Label and crops it, but return results with coordinates relative to the given Rectangle.

resize

Clones the current Label and resizes it.

rotate

Clones the current Label and rotates it.

scale

Clones the current Label and scales it.

to_json

Convert the Label to a json dict.

translate

Clones the current Label and shifts it by a certain number of pixels.

Attributes

area

Label area.

binding_key

description

Description of the current Label.

geometry

Geometry of the current Label.

labeler_login

obj_class

ObjClass of the current Label.

tags

TagCollection of the current Label.

add_tag(tag)

Adds Tag to the current Label.

Parameters
tag : Tag

Tag to be added.

Returns

Label object

Return type

Label

Usage example
import supervisely as sly

# Create label
class_dog = sly.ObjClass('dog', sly.Rectangle)
label_dog = sly.Label(sly.Rectangle(0, 0, 500, 600), class_dog)

# Create tag
meta_dog = sly.TagMeta('dog', sly.TagValueType.NONE)
tag_dog = sly.Tag(meta_dog)

# Add Tag
# Remember that Label object is immutable, and we need to assign new instance of Label to a new variable
new_label = label_dog.add_tag(tag_dog)
add_tags(tags)

Adds multiple Tags to the current Label.

Parameters
tags : List[Tag]

List of Tags to be added.

Returns

Label object

Return type

Label

Usage example
import supervisely as sly

# Create label
class_dog = sly.ObjClass('dog', sly.Rectangle)
label_dog = sly.Label(sly.Rectangle(0, 0, 500, 600), class_dog)

# Create tags
meta_dog = sly.TagMeta('dog', sly.TagValueType.NONE)
tag_dog = sly.Tag(meta_dog)

meta_cat = sly.TagMeta('cat', sly.TagValueType.NONE)
tag_cat = sly.Tag(meta_cat)

tags_arr = [tag_dog, tag_cat]

# Add Tags
# Remember that Label object is immutable, and we need to assign new instance of Label to a new variable
new_label = label_dog.add_tags(tags_arr)
clone(geometry=None, obj_class=None, tags=None, description=None, binding_key=None)

Makes a copy of Label with new fields, if fields are given, otherwise it will use fields of the original Label.

Parameters
geometry : Geometry

Label geometry.

obj_class : ObjClass

Label class.

tags : TagCollection or List[Tag]

Label tags.

description : str, optional

Label description.

Returns

New instance of Label

Return type

Label

Usage example
import supervisely as sly
import numpy as np

# Original Label
class_dog = sly.ObjClass('dog', sly.Rectangle)
label_dog = sly.Label(sly.Rectangle(150, 150, 500, 400), class_dog)

# Let's clone our Label, but with different Geometry coordinates
# Remember that Label object is immutable, and we need to assign new instance of Label to a new variable
clone_label_dog = label_dog.clone(sly.Rectangle(100, 100, 500, 500), class_dog)

# Let's clone our Label with new TagCollection and description
meta_breed = sly.TagMeta('breed', sly.TagValueType.ANY_STRING)
tag_breed = sly.Tag(meta_breed, 'German Shepherd')
tags = sly.TagCollection([tag_breed])

# Remember that Label object is immutable, and we need to assign new instance of Label to a new variable
clone_label_dog_2 = label_dog.clone(tags=tags, description='Dog breed german shepherd')

# Note that you can't clone Label if ObjClass geometry type differ from the new given geometry
mask = np.array([[0, 0, 0, 0, 0],
                 [0, 1, 1, 1, 0],
                 [0, 1, 0, 1, 0],
                 [0, 1, 1, 1, 0],
                 [0, 0, 0, 0, 0]], dtype=np.uint8)

mask_bool = mask==1

clone_label_dog = label_dog.clone(sly.Label(sly.Bitmap(mask_bool), class_dog))
# In this case RuntimeError will be raised
convert(new_obj_class)

Converts Label geometry to another geometry shape.

Parameters
new_obj_class : ObjClass

ObjClass with new geometry shape.

Returns

List of Labels with converted geometries

Return type

List[Label]

Usage example
import supervisely as sly

# Create label
 class_dog = sly.ObjClass('dog', sly.Rectangle)
 label_dog = sly.Label(sly.Rectangle(0, 0, 500, 600), class_dog)

 print(label_dog.geometry.to_json())
 # {'geometryType': 'rectangle'}

 label_cat = sly.ObjClass('cat', sly.Bitmap)

 convert_label = label_dog.convert(label_cat)
 for label_bitmap in convert_label:
     print(label_bitmap.geometry.to_json())
     # Output: {'geometryType': 'bitmap'}
crop(rect)

Clones the current Label and crops it. Mostly used for internal implementation. See usage example in Annotation.

Parameters
rect : Rectangle

Rectangle object.

Returns

List of Labels with new geometries

Return type

List[Label]

draw(bitmap, color=None, thickness=1, draw_tags=False, tags_font=None, draw_class_name=False, class_name_font=None)

Draws current Label on image. Modifies Mask. Mostly used for internal implementation. See usage example in Annotation.

Parameters
bitmap : np.ndarray

image.

color : List[int, int, int], optional

Drawing color in [R, G, B].

thickness : int, optional

Thickness of the drawing figure.

draw_tags : bool, optional

Determines whether to draw tags on bitmap or not.

tags_font : FreeTypeFont, optional

Font of tags to be drawn, uses FreeTypeFont from PIL.

draw_class_name : bool, optional

Draw the class name on the bitmap. If the draw_tags parameter is set to True, the class name will use the same font as the tags_font.

class_name_font : FreeTypeFont, optional

Font of class name to be drawn, uses FreeTypeFont from PIL.

Returns

None

Return type

NoneType

draw_contour(bitmap, color=None, thickness=1, draw_tags=False, tags_font=None, draw_class_name=False, class_name_font=None)

Draws Label geometry contour on the given image. Modifies mask. Mostly used for internal implementation. See usage example in Annotation.

Parameters
bitmap : np.ndarray

image.

color : List[int, int, int], optional

Drawing color in [R, G, B].

thickness : int, optional

Thickness of the drawn contour.

draw_tags : bool, optional

Determines whether to draw tags on bitmap or not.

tags_font : FreeTypeFont, optional

Font of tags to be drawn, uses FreeTypeFont from PIL.

draw_class_name : bool, optional

Draw the class name on the bitmap. If the draw_tags parameter is set to True, the class name will use the same font as the tags_font.

class_name_font : FreeTypeFont, optional

Font of class name to be drawn, uses FreeTypeFont from PIL.

Returns

None

Return type

NoneType

fliplr(img_size)

Clones the current Label and flips it horizontally. Mostly used for internal implementation. See usage example in Annotation.

Parameters
img_size : Tuple[int, int]

Input image size (height, width) of the Annotation to which Label belongs.

Returns

New instance of Label with flipped geometry

Return type

Label

flipud(img_size)

Clones the current Label and flips it vertically. Mostly used for internal implementation. See usage example in Annotation.

Parameters
img_size : Tuple[int, int]

Input image size (height, width) of the Annotation to which Label belongs.

Returns

New instance of Label with flipped geometry

Return type

Label

classmethod from_json(data, project_meta)

Convert a json dict to Label. Read more about Supervisely format.

Parameters
data : dict

Label in json format as a dict.

project_meta : ProjectMeta

Input ProjectMeta.

Returns

Label object

Return type

Label

Usage example
import supervisely as sly

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

meta = api.project.get_meta(PROJECT_ID)

data = {
    "classTitle": "dog",
    "tags": [
        {
            "name": "dog",
            "value": "Woof"
        }
    ],
    "points": {
        "exterior": [[100, 100], [900, 700]],
        "interior": []
    }
}

label_dog = sly.Label.from_json(data, meta)
get_mask(img_size)

Returns 2D boolean mask of the label. With shape as img_size (height, width) and filled with True values inside the label and False values outside. dtype = np.bool shape = img_size

Parameters
img_size : Tuple[int, int]

size of the image (height, width)

Returns

2D boolean mask of the Label

Return type

np.ndarray

relative_crop(rect)

Clones the current Label and crops it, but return results with coordinates relative to the given Rectangle. Mostly used for internal implementation. See usage example in Annotation.

Parameters
rect : Rectangle

Rectangle object.

Returns

List of Labels with new geometries

Return type

List[Label]

resize(in_size, out_size)

Clones the current Label and resizes it. Mostly used for internal implementation. See usage example in Annotation.

Parameters
in_size : Tuple[int, int]

Input image size (height, width) of the Annotation to which Label belongs.

out_size : Tuple[int, int]

Desired output image size (height, width) of the Annotation to which Label belongs.

Returns

New instance of Label with resized geometry

Return type

Label

rotate(rotator)

Clones the current Label and rotates it. Mostly used for internal implementation. See usage example in Annotation.

Parameters
rotator : ImageRotator

ImageRotator object.

Returns

New instance of Label with rotated geometry

Return type

Label

scale(factor)

Clones the current Label and scales it. Mostly used for internal implementation. See usage example in Annotation.

Parameters
factor : float

Scale factor.

Returns

New instance of Label with scaled geometry

Return type

Label

to_json()

Convert the Label to a json dict. Read more about Supervisely format.

Returns

Json format as a dict

Return type

dict

Usage example
import supervisely as sly

meta_dog = sly.TagMeta('dog', sly.TagValueType.ANY_STRING)
tag_dog = sly.Tag(meta_dog, 'Woof')
class_dog = sly.ObjClass('dog', sly.Rectangle)
label_dog = sly.Label(sly.Rectangle(100, 100, 700, 900), class_dog, sly.TagCollection([tag_dog]), description='Insert Label description here')

label_dog_json = label_dog.to_json()
print(label_dog_json)
# Output: {
#    "classTitle": "dog",
#    "description": "",
#    "tags": [
#        {
#            "name": "dog",
#            "value": "Woof"
#        }
#    ],
#    "points": {
#        "exterior": [[100, 100],[900, 700]],
#        "interior": []
#    },
#    "geometryType": "rectangle",
#    "shape": "rectangle"
# }
translate(drow, dcol)

Clones the current Label and shifts it by a certain number of pixels. Mostly used for internal implementation.

Parameters
drow : int

Horizontal shift.

dcol : int

Vertical shift.

Returns

New instance of Label with translated geometry

Return type

Label

Usage example
import supervisely as sly

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

# Get image and annotation from API
project_id = 117139
image_id = 194190568
meta_json = api.project.get_meta(project_id)
meta = sly.ProjectMeta.from_json(meta_json)
ann_info = api.annotation.download(image_id)
ann = sly.Annotation.from_json(ann_info.annotation, meta)
img = api.image.download_np(image_id)
new_img = copy.deepcopy(img)
ann.draw_pretty(img, thickness=3) # before

# Translate label with name 'lemon'
new_labels = []
for label in ann.labels:
    if label.obj_class.name == 'lemon':
        translate_label = label.translate(250, -350)
        new_labels.append(translate_label)
    else:
        new_labels.append(label)
ann = ann.clone(labels=new_labels)
ann.draw_pretty(new_img, thickness=3)  # after
property area

Label area.

Returns

Area of current geometry in Label.

Return type

float

Usage example
import supervisely as sly

# Create label
class_dog = sly.ObjClass('dog', sly.Rectangle)
label_dog = sly.Label(sly.Rectangle(0, 0, 500, 600), class_dog)

figure_area = label_dog.area
print(figure_area)
# Output: 301101.0
property description

Description of the current Label.

Returns

Description

Return type

str

Usage example
class_dog = sly.ObjClass('dog', sly.Rectangle)
label_dog = sly.Label(sly.Rectangle(150, 150, 400, 500), class_dog, description="Insert Label description here")

print(label_dog.description)
# Output: 'Insert Label description here'
property geometry

Geometry of the current Label.

Returns

Geometry object

Return type

Geometry

Usage example
class_dog = sly.ObjClass('dog', sly.Rectangle)
label_dog = sly.Label(sly.Rectangle(150, 150, 400, 500), class_dog)

label_json = label_dog.geometry.to_json()
print(label_json)
# Output: {
#    "points": {
#        "exterior": [
#            [
#                150,
#                150
#            ],
#            [
#                500,
#                400
#            ]
#        ],
#        "interior": []
#    }
# }
property obj_class

ObjClass of the current Label.

Returns

ObjClass object

Return type

ObjClass

Usage example
class_dog = sly.ObjClass('dog', sly.Rectangle)
label_dog = sly.Label(sly.Rectangle(150, 150, 400, 500), class_dog)

label_dog_json = label_dog.obj_class.to_json()
print(label_dog_json)
# Output: {
#    "title": "dog",
#    "shape": "rectangle",
#    "color": "#0F8A12",
#    "geometry_config": {},
#    "hotkey": ""
# }
property tags

TagCollection of the current Label.

Returns

TagCollection object

Return type

TagCollection

Usage example
meta_dog = sly.TagMeta('dog', sly.TagValueType.ANY_STRING)
tag_dog = sly.Tag(meta_dog, 'Woof')
class_dog = sly.ObjClass('dog', sly.Rectangle)

label_dog = sly.Label(sly.Rectangle(100, 100, 700, 900), class_dog, sly.TagCollection([tag_dog]))

label_dog_json = label_dog.tags.to_json()
print(label_dog_json)
# Output: [
#    {
#        "name": "dog",
#        "value": "Woof"
#    }
# ]