Tag

class Tag[source]

Bases: supervisely.collection.key_indexed_collection.KeyObject

Tag can be attached to whole image and/or to individual Label. Tag object is immutable.

Parameters
meta : TagMeta

General information about Tag.

value : Optional[Union[str, int, float]]

Tag value. Depends on TagValueType of TagMeta.

sly_id : int, optional

Tag ID in Supervisely server.

labeler_login : str, optional

Login of user who created Tag.

updated_at : str, optional

Date and Time when Tag was modified last. Date Format: Year:Month:Day:Hour:Minute:Seconds. Example: ‘2021-01-22T19:37:50.158Z’.

created_at : str, optional

Date and Time when Tag was created. Date Format is the same as in “updated_at” parameter.

Raises

ValueError, if meta is None or if Tag has incompatible value against it’s meta value type

Usage example
import supervisely as sly

# Let's create 3 Tags with different values
# First we need to initialize a TagMeta
meta_dog = sly.TagMeta('dog', sly.TagValueType.NONE)

# Now we can create a Tag using our TagMeta
tag_dog = sly.Tag(meta_dog)

# When you are creating a new Tag
# Tag.value is automatically cross-checked against your TagMeta value type to make sure the value is valid.
# If we now try to add a value to our newly created Tag, we receive "ValueError", because our TagMeta value type is "NONE"
tag_dog = sly.Tag(meta_dog, value="Husky")
# Output: ValueError: Tag dog can not have value Husky

# Let's create another Tag with a string value type
meta_cat = sly.TagMeta('cat', sly.TagValueType.ANY_STRING)
tag_cat = sly.Tag(meta_cat, value="Fluffy")

# Now let's create a Tag using TagMeta with "ONEOF_STRING" value type
# In order to use "oneof_string value type", you must initialize a variable with possible values(see class TagMeta for more information)
colors = ["brown", "white", "black", "red", "chocolate", "gold", "grey"]
meta_coat_color = sly.TagMeta('coat color', sly.TagValueType.ONEOF_STRING, possible_values=colors)
tag_coat_color = sly.Tag(meta_coat_color, value="white")

# If given value is not in a list of possible Tags, ValueError will be raised
tag_coat_color = sly.Tag(meta_coat_color, value="yellow")
# Output: ValueError: Tag coat color can not have value yellow

Methods

clone

Clone makes a copy of Tag with new fields, if fields are given, otherwise it will use original Tag fields.

from_json

Convert a json dict to Tag.

get_compact_str

Displays information about Tag's name and value in string format.

get_header_ptable

Get header of the table with tags.

get_row_ptable

Get row with tag properties.

key

Get TagMeta key value.

to_json

Convert the Tag to a json dict.

Attributes

meta

General information about Tag.

name

Name property.

value

Tag value.

clone(meta=None, value=None, sly_id=None, labeler_login=None, updated_at=None, created_at=None)[source]

Clone makes a copy of Tag with new fields, if fields are given, otherwise it will use original Tag fields.

Parameters
meta : TagMeta

General information about Tag.

sly_id : int, optional

Tag ID in Supervisely server.

labeler_login : str, optional

Login of user who created Tag.

updated_at : str, optional

Date and Time when Tag was modified last. Date Format: Year:Month:Day:Hour:Minute:Seconds. Example: ‘2021-01-22T19:37:50.158Z’.

created_at : str, optional

Date and Time when Tag was created. Date Format is the same as in “updated_at” parameter.

Returns

New instance of Tag

Return type

Tag

Usage Example
import supervisely as sly

#Original Tag
weather_conditions = ["Sunny", "Cloudy", "Snowy", "Foggy", "Rainy"]
meta_weather = sly.TagMeta("weather", sly.TagValueType.ONEOF_STRING, possible_values=weather_conditions)

tag_weather = sly.Tag(meta_weather, value="Sunny")

# Let's create some more tags by cloning our original Tag
# Remember that Tag class object is immutable, and we need to assign new instance of Tag to a new variable
clone_weather_1 = tag_weather.clone(value="Snowy")

clone_weather_2 = tag_weather.clone(value="Cloudy")

clone_weather_3 = tag_weather.clone(value="Rainy")
classmethod from_json(data, tag_meta_collection)[source]

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

Parameters
data : dict

Tag in json format as a dict.

tag_meta_collection : TagMetaCollection

TagMetaCollection object.

Returns

Tag object

Return type

Tag

Usage example
import supervisely as sly

meta_dog = sly.TagMeta('dog', sly.TagValueType.ANY_STRING)
tag_metas = sly.TagMetaCollection([meta_dog])

data = {
    "name": "dog",
    "value": "Husky",
    "labelerLogin": "admin",
    "updatedAt": "2021-01-22T19:37:50.158Z",
    "createdAt": "2021-01-22T18:00:00.000Z"
}

tag_dog = sly.Tag.from_json(data, tag_metas)
get_compact_str()[source]

Displays information about Tag’s name and value in string format.

Returns

Name and value of the given Tag

Return type

str

Usage example
import supervisely as sly

meta_dog = sly.TagMeta('dog', sly.TagValueType.ANY_STRING)
tag_dog = sly.Tag(meta_dog, value="Husky")

print(tag_dog.get_compact_str())
# Output: 'dog:Husky'
classmethod get_header_ptable()[source]

Get header of the table with tags.

Returns

List of table header values.

Return type

List[str]

Usage example
import supervisely as sly

header = sly.Tag.get_header_ptable()

print(header)
# Output: ['Name', 'Value type', 'Value']
get_row_ptable()[source]

Get row with tag properties.

Returns

List of tag properties.

Return type

List[str]

Usage example
import supervisely as sly

weather_conditions = ["Sunny", "Cloudy", "Snowy", "Foggy", "Rainy"]
meta_weather = sly.TagMeta("weather", sly.TagValueType.ONEOF_STRING, possible_values=weather_conditions)
tag_weather = sly.Tag(meta_weather, value="Sunny")

row = tag_weather.get_row_ptable()

print(row)
# Output: ['weather', 'oneof_string', 'Sunny']
key()[source]

Get TagMeta key value.

Returns

TagMeta key value

Return type

str

Usage example
import supervisely as sly

weather_conditions = ["Sunny", "Cloudy", "Snowy", "Foggy", "Rainy"]
meta_weather = sly.TagMeta("weather", sly.TagValueType.ONEOF_STRING, possible_values=weather_conditions)
tag_weather = sly.Tag(meta_weather, value="Sunny")
key = tag_weather.key()
print(key)
# Output: weather
to_json()[source]

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

Returns

Json format as a dict

Return type

dict

Usage example
import supervisely as sly

# Tag with all fields filled in
meta_dog = sly.TagMeta('dog', sly.TagValueType.ANY_STRING)
tag_dog = sly.Tag(meta=meta_dog, value="Husky", sly_id=38456, labeler_login="admin",
                  updated_at="2021-01-22T19:37:50.158Z", created_at="2021-01-22T18:00:00.000Z")

tag_dog_json = tag_dog.to_json()
print(tag_dog_json)
# Notice that sly_id won't print
# Output: {
#  "name": "dog",
#  "value": "Husky",
#  "labelerLogin": "admin",
#  "updatedAt": "2021-01-22T19:37:50.158Z",
#  "createdAt": "2021-01-22T18:00:00.000Z"
# }
property meta

General information about Tag. When creating a new Tag, it’s value is automatically cross-checked against TagValueType to make sure that value is valid.

Returns

TagMeta object

Return type

TagMeta

Usage example
meta_dog = sly.TagMeta('dog', sly.TagValueType.NONE)
tag_dog = sly.Tag(meta_dog)

# Our TagMeta has value type 'NONE', if we try to add value to our Tag, "ValueError" error will be raised
tag_dog = sly.Tag(meta_dog, value="Husky")
# Output: ValueError: Tag dog can not have value Husky
property name

Name property.

Returns

Name

Return type

str

Usage example
meta_dog = sly.TagMeta('dog', sly.TagValueType.ANY_STRING)
tag_dog = sly.Tag(meta_dog, value="Husky")

print(tag_dog.name)
# Output: "dog"
property value

Tag value. Return type depends on TagValueType.

Returns

Tag value

Return type

str, int or float or None

Usage example
meta_dog = sly.TagMeta('dog', sly.TagValueType.ANY_STRING)
tag_dog = sly.Tag(meta_dog, value="Husky")

meta_age = sly.TagMeta('age', sly.TagValueType.ANY_NUMBER)
tag_age = sly.Tag(meta_age, value=9)

colors = ["Black", "White", "Golden", "Brown"]
meta_color = sly.TagMeta('coat color', sly.TagValueType.ONEOF_STRING, possible_values=colors)
tag_color = sly.Tag(meta_color, value="White")

type(tag_dog.value)   # 'str'
type(tag_age.value)   # 'int'
type(tag_color.value) # 'str'