TagMeta

class TagMeta[source]

Bases: supervisely.collection.key_indexed_collection.KeyObject, supervisely.io.json.JsonSerializable

General information about Tag. TagMeta object is immutable.

Parameters
name : str

Tag name.

value_type : str

Tag value type.

possible_values : List[str], optional

List of possible values.

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

[R, G, B] color, generates random color by default.

sly_id : int, optional

Tag ID in Supervisely server.

hotkey : str, optional

Hotkey for Tag in annotation tool UI.

applicable_to : str, optional

Defines applicability of Tag only to images, objects or both.

applicable_classes : List[str], optional

Defines applicability of Tag only to certain classes.

Raises

ValueError, if color is not list, or doesn’t have exactly 3 values

Usage example
import supervisely as sly

# TagMeta
meta_dog = sly.TagMeta('dog', sly.TagValueType.NONE)

# TagMeta applicable only to Images example
meta_cat = sly.TagMeta('cat', sly.TagValueType.NONE, applicable_to=sly.TagApplicableTo.IMAGES_ONLY)

# TagMeta with string value applicable only to Objects example
meta_breed = sly.TagMeta('breed', sly.TagValueType.ANY_STRING, applicable_to=sly.TagApplicableTo.OBJECTS_ONLY)

# More complex TagMeta example
# Create a list with possible values in order to use "ONEOF_STRING" value type
coat_colors = ["brown", "white", "black", "red", "chocolate", "gold", "grey"]
# Note that "ONEOF_STRING" value type requires possible values, otherwise ValueError will be raised
meta_coat_color = sly.TagMeta('coat color', sly.TagValueType.ONEOF_STRING, coat_colors, [255,120,0], hotkey="M", applicable_to=sly.TagApplicableTo.OBJECTS_ONLY, applicable_classes=["dog", "cat"])

Methods

add_possible_value

Adds a new value to the list of possible values.

clone

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

from_json

Convert a json dict to TagMeta.

get_header_ptable

get_row_ptable

is_compatible

rtype

bool

is_valid_value

Checks value against object value type to make sure that value is valid.

key

rtype

str

to_json

Convert the TagMeta to a json dict.

Attributes

applicable_classes

Applicable classes.

applicable_to

Tag applicability to objects, images, or both.

color

[R,G,B] color.

hotkey

Hotkey for Tag in annotation tool UI.

name

Name.

possible_values

Possible values of object.

sly_id

Tag ID in Supervisely server.

value_type

Value type.

add_possible_value(value)[source]

Adds a new value to the list of possible values.

Parameters
value : str

New value that will be added to a list.

Raises

ValueError, if object’s value type is not “oneof_string” or already exists in a list

Returns

New instance of TagMeta

Return type

TagMeta

Usage Example
import supervisely as sly

#In order to add possible values, you must first initialize a variable where all possible values will be stored if it doesnt exist already
colors = ["brown", "white", "black", "red", "chocolate", "gold", "grey"]
meta_coat_color = sly.TagMeta('coat color', sly.TagValueType.ONEOF_STRING, possible_values=colors, applicable_classes=["dog", "cat"])

print(meta_coat_color.possible_values)
# Output: ['brown', 'white', 'black', 'red', 'chocolate', 'gold', 'grey']

#Now we can add new possible value to our TagMeta
# Remember that TagMeta object is immutable, and we need to assign new instance of TagMeta to a new variable
meta_coat_color = meta_coat_color.add_possible_value("bald (no coat)")

print(meta_coat_color.possible_values)
# Output: ['brown', 'white', 'black', 'red', 'chocolate', 'gold', 'grey', 'bald (no coat)']
clone(name=None, value_type=None, possible_values=None, color=None, sly_id=None, hotkey=None, applicable_to=None, applicable_classes=None)[source]

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

Parameters
name : str

Tag name.

value_type : str

Tag value type.

possible_values : List[str], optional

List of possible values.

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

[R, G, B] color, generates random color by default.

sly_id : int, optional

Tag ID in Supervisely server.

hotkey : str, optional

Hotkey for Tag in annotation tool UI.

applicable_to : str, optional

Defines applicability of Tag only to images, objects or both.

applicable_classes : List[str], optional

Defines applicability of Tag only to certain classes.

Returns

New instance of TagMeta

Return type

TagMeta

Usage Example
import supervisely as sly

#Original TagMeta
meta_dog_breed = sly.TagMeta('breed', sly.TagValueType.NONE)

# TagMetas made of original TagMeta
# Remember that TagMeta class object is immutable, and we need to assign new instance of TagMeta to a new variable
A_breeds = ["Affenpinscher", "Afghan Hound", "Aidi", "Airedale Terrier", "Akbash Dog", "Akita"]
meta_A_breed = meta_dog_breed.clone(value_type=sly.TagValueType.ONEOF_STRING, possible_values=A_breeds, hotkey='A')

B_breeds = ["Basset Fauve de Bretagne", "Basset Hound", "Bavarian Mountain Hound", "Beagle", "Beagle-Harrier", "Bearded Collie"]
meta_B_breed = meta_A_breed.clone(possible_values=B_breeds, hotkey='B')

C_breeds = ["Cairn Terrier", "Canaan Dog", "Canadian Eskimo Dog", "Cane Corso", "Cardigan Welsh Corgi", "Carolina Dog"]
meta_C_breed = meta_B_breed.clone(possible_values=C_breeds, hotkey='C')
classmethod from_json(data)[source]

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

Parameters
data : dict

TagMeta in json format as a dict.

Returns

TagMeta object

Return type

TagMeta

Usage example
import supervisely as sly

data = {
    "name":"Color",
    "value_type":"oneof_string",
    "color":"#FF7800",
    "values":[
        "brown",
        "white",
        "black",
        "red",
        "blue",
        "yellow",
        "grey"
    ],
    "hotkey":"M",
    "applicable_type":"all",
    "classes":[
        "car",
        "bicycle"
    ]
}

meta_colors = sly.TagMeta.from_json(data)
classmethod get_header_ptable()[source]
get_row_ptable()[source]
is_compatible(other)[source]
Return type

bool

is_valid_value(value)[source]

Checks value against object value type to make sure that value is valid.

Parameters
value : str

Value to check.

Returns

True if value is supported, otherwise False

Return type

bool

Usage example
import supervisely as sly

# Initialize TagMeta
meta_dog = sly.TagMeta('dog', sly.TagValueType.ANY_STRING)

# Check what value type is in our Tagmeta
print(meta_dog.value_type)
# Output: 'any_string'

# Our TagMeta has 'any_string' value type, it means only 'string' values will work with it
# Let's check if value is valid for our TagMeta
meta_dog.is_valid_value('Woof!')            # True
meta_dog.is_valid_value(555)                # False

# TagMetas with 'any_number' value type are compatible with 'int' and 'float' values
meta_quantity = sly.TagMeta('quantity', sly.TagValueType.ANY_NUMBER)

meta_quantity.is_valid_value('new string value') # False
meta_quantity.is_valid_value(555)                # True
meta_quantity.is_valid_value(3.14159265359)      # True
to_json()[source]

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

Returns

Json format as a dict

Return type

dict

Usage example
import supervisely as sly

colors = ["brown", "white", "black", "red", "blue", "yellow", "grey"]
meta_color = sly.TagMeta('Color',
                        sly.TagValueType.ONEOF_STRING,
                        possible_values=colors,
                        color=[255, 120, 0],
                        hotkey="M",
                        applicable_classes=["car", "bicycle"])


meta_color_json = meta_color.to_json()
print(meta_color_json)
# Output: {
#     "name":"Color",
#     "value_type":"oneof_string",
#     "color":"#FF7800",
#     "values":[
#         "brown",
#         "white",
#         "black",
#         "red",
#         "blue",
#         "yellow",
#         "grey"
#     ],
#     "hotkey":"M",
#     "applicable_type":"all",
#     "classes":[
#         "car",
#         "bicycle"
#     ]
# }
property applicable_classes

Applicable classes.

Returns

List of applicable classes

Return type

List[str]

Usage example
# Imagine we have 2 ObjClasses in our Project
class_car = sly.ObjClass(name='car', geometry_type='rectangle')
class_bicycle = sly.ObjClass(name='bicycle', geometry_type='rectangle')

# You can put a "string" with ObjClass name or use ObjClass.name
meta_vehicle = sly.TagMeta('vehicle', sly.TagValueType.NONE, applicable_classes=["car", class_bicycle.name])

print(meta_vehicle.applicable_classes)
# Output: ['car', 'bicycle']
property applicable_to

Tag applicability to objects, images, or both.

Returns

Applicability

Return type

str

Usage example
meta_dog = sly.TagMeta('dog', sly.TagValueType.NONE, applicable_to=IMAGES_ONLY)

print(meta_dog.applicable_to)
# Output: 'imagesOnly'
property color

[R,G,B] color.

Returns

Color

Return type

List[int, int, int]

Usage example
meta_dog = sly.TagMeta('dog', sly.TagValueType.NONE, color=[255,120,0])

print(meta_dog.color)
# Output: [255,120,0]
property hotkey

Hotkey for Tag in annotation tool UI.

Returns

Hotkey

Return type

str

Usage example
meta_dog = sly.TagMeta('dog', sly.TagValueType.NONE, hotkey='M')

print(meta_dog.hotkey)
# Output: 'M'
property name

Name.

Returns

Name

Return type

str

Usage example
meta_dog = sly.TagMeta('dog', sly.TagValueType.ANY_STRING)
print(meta_dog.name)
# Output: 'dog'
property possible_values

Possible values of object. This is a required field if object has “oneof_string” value type.

Raise

ValueError if list of possible values is not defined or TagMeta value_type is not “oneof_string”.

Returns

List of possible values

Return type

List[str]

Usage example
# List of possible values
coat_colors = ["brown", "white", "black", "red", "chocolate", "gold", "grey"]

# TagMeta
meta_coat_color = sly.TagMeta('coat color', sly.TagValueType.ONEOF_STRING, possible_values=coat_colors)

print(meta_coat_color.possible_values)
# Output: ['brown', 'white', 'black', 'red', 'chocolate', 'gold', 'grey']

# Note that this is a required field if object has "oneof_string" value type.
meta_coat_color = sly.TagMeta('coat color', sly.TagValueType.ONEOF_STRING)
# Output: ValueError: TagValueType is ONEOF_STRING. List of possible values have to be defined.
property sly_id

Tag ID in Supervisely server.

Returns

ID

Return type

int

Usage example
meta_dog = sly.TagMeta('dog', sly.TagValueType.NONE, sly_id=38584)

print(meta_dog.sly_id)
# Output: 38584
property value_type

Value type. See possible value types in TagValueType.

Returns

Value type

Return type

str

Usage example
meta_dog = sly.TagMeta('dog', sly.TagValueType.ANY_STRING)
meta_dog.value_type == sly.TagValueType.ANY_STRING # True

print(meta_dog.value_type)
# Output: 'any_string'