VideoTagCollection

class VideoTagCollection[source]

Bases: supervisely.annotation.tag_collection.TagCollection

Collection with VideoTag instances. VideoTagCollection object is immutable.

Usage example
import supervisely as sly
from supervisely.video_annotation.video_tag import VideoTag
from supervisely.video_annotation.video_tag_collection import VideoTagCollection

# Create two VideoTags for collection
meta_car = sly.TagMeta('car_tag', sly.TagValueType.ANY_STRING)
car_tag = VideoTag(meta_car, value='acura')
meta_bus = sly.TagMeta('bus_tag', sly.TagValueType.ANY_STRING)
bus_tag = VideoTag(meta_bus, value='volvo')

# Create VideoTagCollection
tags = VideoTagCollection([car_tag, bus_tag])
tags_json = tags.to_json()
print(tags_json)
# Output: [
#     {
#         "name": "car_tag",
#         "value": "acura",
#         "key": "378408fcb6854305a38fed7c996f4901"
#     },
#     {
#         "name": "bus_tag",
#         "value": "volvo",
#         "key": "0c63174878204faea67c4025adec1e2a"
#     }
# ]

# Add item to VideoTagCollection
meta_truck = sly.TagMeta('truck_tag', sly.TagValueType.NONE)
truck_tag = VideoTag(meta_truck)
# Remember that VideoTagCollection is immutable, and we need to assign new instance of VideoTagCollection to a new variable
new_tags = tags.add(truck_tag)
new_tags_json = new_tags.to_json()
print(new_tags_json)
# Output: [
#     {
#         "name": "car_tag",
#         "value": "acura",
#         "key": "3d33f1685dab44da9b55d67bab3937e9"
#     },
#     {
#         "name": "bus_tag",
#         "value": "volvo",
#         "key": "7c52c168a22b47a5b39fbcdef94b0140"
#     },
#     {
#         "name": "truck_tag",
#         "key": "7188242d230b4d2783c588cc2eca5ff8"
#     }
# ]

# You can also add multiple items to collection
meta_truck = sly.TagMeta('truck_tag', sly.TagValueType.NONE)
truck_tag = VideoTag(meta_truck)
meta_train = sly.TagMeta('train_tag', sly.TagValueType.ANY_NUMBER)
train_tag = VideoTag(meta_train, value=777)
new_tags = tags.add_items([truck_tag, train_tag])
new_tags_json = new_tags.to_json()
print(new_tags_json)
# Output: [
#     {
#         "name": "car_tag",
#         "value": "acura",
#         "key": "03b052c464d84d5db6451b86f3bdef79"
#     },
#     {
#         "name": "bus_tag",
#         "value": "volvo",
#         "key": "0f7dd8fb6f8c41da9e68e64d3186df15"
#     },
#     {
#         "name": "truck_tag",
#         "key": "fc01af7c70154771b7253b6a94484179"
#     },
#     {
#         "name": "train_tag",
#         "value": 777,
#         "key": "6ce5118181074a52b74dee9335fa292d"
#     }
# ]

# Intersection, finds intersection of given list of VideoTag instances with collection items
intersect_tags = tags.intersection([bus_tag])
intersect_tags_json = intersect_tags.to_json()
print(intersect_tags_json)
# Output: [
#     {
#         "name": "bus_tag",
#         "value": "volvo",
#         "key": "13d77d8c848e4a3ebeb710bf5f3f38a6"
#     }
# ]

# Difference, finds difference between collection and given list of VideoTag
diff_tags = tags.difference([bus_tag])
diff_tags_json = diff_tags.to_json()
print(diff_tags_json)
# Output: [
#     {
#         "name": "car_tag",
#         "value": "acura",
#         "key": "341b9fc077e142c0956d5cf985d705c1"
#     }
# ]

# Merge, merges collection and given list of VideoTagCollection
meta_truck = sly.TagMeta('truck_tag', sly.TagValueType.NONE)
truck_tag = VideoTag(meta_truck)
meta_train = sly.TagMeta('train_tag', sly.TagValueType.ANY_NUMBER)
train_tag = VideoTag(meta_train, value=777)
over_tags = VideoTagCollection([truck_tag, train_tag])
# Merge
merge_tags = tags.merge(over_tags)
merge_tags_json = merge_tags.to_json()
print(merge_tags_json)
# Output: [
#     {
#         "name": "car_tag",
#         "value": "acura",
#         "key": "3bfde60091a24dd493485f2b80364736"
#     },
#     {
#         "name": "bus_tag",
#         "value": "volvo",
#         "key": "dac5b07954064c78acadfa71161f6998"
#     },
#     {
#         "name": "truck_tag",
#         "key": "a46aac4499574afc81d41d2bb061a3c6"
#     },
#     {
#         "name": "train_tag",
#         "value": 777,
#         "key": "bf5b1667383449478694fb6349d7b16c"
#     }
# ]

Methods

add

Add given item to collection.

add_items

Add items from given list to collection.

clone

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

difference

Find difference between collection and given list of instances.

from_api_response

Create a VideoTagCollection object from API response data.

from_json

Convert a list of json dicts to VideoTagCollection.

get

Get item from collection with given key(name).

get_all

Get item from collection with given key(name).

get_by_name

Get a list of VideoTag objects by name from the VideoTagCollection.

get_single_by_name

Get a single Tag object by name from the VideoTagCollection.

has_key

Check if given key(item name exist in collection).

intersection

Find intersection of given list of instances with collection items.

items

Get list of all items in collection.

keys

Get list of all keys(item names) in collection.

merge

Merge collection with other MultiKeyIndexedCollection object.

merge_without_duplicates

Merge collection with other MultiKeyIndexedCollection object.

remove_items

Remove items from collection by given list of keys.

to_json

Convert the VideoTagCollection to a list of json dicts.

item_type

alias of supervisely.video_annotation.video_tag.VideoTag

add(item)

Add given item to collection.

Parameters
item : KeyObject

ObjClassCollection, TagMetaCollection or TagCollection object.

Returns

New instance of KeyIndexedCollection

Return type

KeyIndexedCollection

Usage Example
import supervisely as sly

item_cat = sly.TagMeta('cat', sly.TagValueType.NONE)
item_turtle = sly.TagMeta('turtle', sly.TagValueType.ANY_STRING)
collection = sly.collection.key_indexed_collection.KeyIndexedCollection([item_cat, item_turtle])
# Remember that KeyIndexedCollection object is immutable, and we need to assign new instance of KeyIndexedCollection to a new variable
item_dog = sly.ObjClass('dog', sly.Rectangle)
new_collection = collection.add(item_dog)
add_items(items)

Add items from given list to collection.

Parameters
items : List[KeyObject]

List of ObjClassCollection, TagMetaCollection or TagCollection objects.

Returns

New instance of KeyIndexedCollection

Return type

KeyIndexedCollection

Usage Example
import supervisely as sly

item_cat = sly.TagMeta('cat', sly.TagValueType.NONE)
item_turtle = sly.TagMeta('turtle', sly.TagValueType.ANY_STRING)
collection = sly.collection.key_indexed_collection.KeyIndexedCollection([item_cat, item_turtle])
# Remember that KeyIndexedCollection object is immutable, and we need to assign new instance of KeyIndexedCollection to a new variable
item_dog = sly.ObjClass('dog', sly.Rectangle)
item_mouse = sly.ObjClass('mouse', sly.Bitmap)
new_collection = collection.add_items([item_dog, item_mouse])
clone(items=None)

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

Parameters
items : List[KeyObject], optional

List of ObjClassCollection, TagMetaCollection or TagCollection objects.

Returns

New instance of KeyIndexedCollection

Return type

KeyIndexedCollection

Usage Example
import supervisely as sly

item_cat = sly.TagMeta('cat', sly.TagValueType.NONE)
item_turtle = sly.TagMeta('turtle', sly.TagValueType.ANY_STRING)
collection = sly.collection.key_indexed_collection.KeyIndexedCollection([item_cat, item_turtle])
# Remember that KeyIndexedCollection object is immutable, and we need to assign new instance of KeyIndexedCollection to a new variable
new_collection = collection.clone()
difference(other)

Find difference between collection and given list of instances.

Parameters
key : List[KeyObject]

List of ObjClassCollection, TagMetaCollection or TagCollection objects.

Returns

KeyIndexedCollection object

Return type

KeyIndexedCollection

Usage Example
import supervisely as sly

item_cat = sly.TagMeta('cat', sly.TagValueType.NONE)
item_turtle = sly.TagMeta('turtle', sly.TagValueType.ANY_STRING)
collection = sly.collection.key_indexed_collection.KeyIndexedCollection([item_cat, item_turtle])

item_dog = sly.TagMeta('dog', sly.TagValueType.NONE)
item_turtle = sly.TagMeta('turtle', sly.TagValueType.ANY_STRING)
items = [item_dog, item_turtle]

diff = collection.difference(items)
print(diff.to_json())
# Output: [
#     {
#         "name": "cat",
#         "value_type": "none",
#         "color": "#8A150F",
#         "hotkey": "",
#         "applicable_type": "all",
#         "classes": []
#     }
# ]
classmethod from_api_response(data, tag_meta_collection, id_to_tagmeta=None)[source]

Create a VideoTagCollection object from API response data.

Parameters
data : List[Dict]

API response data.

tag_meta_collection : TagMetaCollection

_description_

id_to_tagmeta : Optional[Dict[int, TagMeta]]

Mapping of tag IDs to tag metadata.

Returns

VideoTagCollection object.

Return type

VideoTagCollection

Usage example
import supervisely as sly

# You can connect to API directly
address = 'https://app.supervise.ly/'
token = 'Your Supervisely API Token'
api = sly.Api(address, token)

# Or you can use API from environment
os.environ['SERVER_ADDRESS'] = 'https://app.supervise.ly'
os.environ['API_TOKEN'] = 'Your Supervisely API Token'
api = sly.Api.from_env()

videos = api.video.get_list(dataset_id)
for info in videos:
    tag_collection = sly.VideoTagCollection.from_api_response(
        info.tags, project_meta.tag_metas
    )
classmethod from_json(data, tag_meta_collection, key_id_map=None)[source]

Convert a list of json dicts to VideoTagCollection. Read more about Supervisely format.

Parameters
data : List[dict]

List with dicts in json format.

project_meta : TagMetaCollection

Input TagMetaCollection object.

key_id_map : KeyIdMap, optional

KeyIdMap object.

Returns

VideoTagCollection object

Return type

VideoTagCollection

Usage example
import supervisely as sly
from supervisely.video_annotation.video_tag_collection import VideoTagCollection

tags_json = [
    {
        "name": "car_tag",
        "value": "acura",
    },
    {
        "name": "bus_tag",
        "value": "volvo",
    }
]
meta_car = sly.TagMeta('car_tag', sly.TagValueType.ANY_STRING)
meta_bus = sly.TagMeta('bus_tag', sly.TagValueType.ANY_STRING)
meta_collection = sly.TagMetaCollection([meta_car, meta_bus])

tags = VideoTagCollection.from_json(tags_json, meta_collection)
get(key, default=None)

Get item from collection with given key(name). If there are many values for the same key, the first value will be returned.

Parameters
items : str

Name of KeyObject in collection.

default : optional

The value that is returned if there is no key in the collection.

Returns

ObjClassCollection, TagMetaCollection or TagCollection object

Return type

KeyObject

Usage Example
import supervisely as sly

item_cat = sly.TagMeta('cat', sly.TagValueType.NONE)
item_turtle = sly.TagMeta('turtle', sly.TagValueType.ANY_STRING)
other_cat = sly.ObjClass('cat', sly.Rectangle)
collection = sly.collection.key_indexed_collection.MultiKeyIndexedCollection([item_cat, item_turtle, other_cat])
item = collection.get('cat')
print(item)
# Output:
# Name:  cat                      Value type:none          Possible values:None       Hotkey                  Applicable toall        Applicable classes[]
get_all(key, default=[])

Get item from collection with given key(name). If there are many values for the same key,all values will be returned by list.

Parameters
items : str

Name of KeyObject in collection.

default : List, optional

The value that is returned if there is no key in the collection.

Returns

List of ObjClassCollection, TagMetaCollection or TagCollection objects or empty list

Return type

List[KeyObject] or list

Usage Example
import supervisely as sly

item_cat = sly.TagMeta('cat', sly.TagValueType.NONE)
item_turtle = sly.TagMeta('turtle', sly.TagValueType.ANY_STRING)
other_cat = sly.ObjClass('cat', sly.Rectangle)
collection = sly.collection.key_indexed_collection.MultiKeyIndexedCollection([item_cat, item_turtle, other_cat])
items = collection.get('cat')
print(items)
# Output:
# [<supervisely.annotation.tag_meta.TagMeta object at 0x7f0278662340>, <supervisely.annotation.obj_class.ObjClass object at 0x7f02786623a0>]
get_by_name(tag_name, default=None)[source]

Get a list of VideoTag objects by name from the VideoTagCollection.

Parameters
tag_name : str

Name of the tags to get.

Returns

List of VideoTag objects with the specified name.

Return type

List[VideoTag]

Usage example
import supervisely as sly

# You can connect to API directly
address = 'https://app.supervise.ly/'
token = 'Your Supervisely API Token'
api = sly.Api(address, token)

# Or you can use API from environment
os.environ['SERVER_ADDRESS'] = 'https://app.supervise.ly'
os.environ['API_TOKEN'] = 'Your Supervisely API Token'
api = sly.Api.from_env()

videos = api.video.get_list(dataset_id)
for info in videos:
    tag_collection = sly.VideoTagCollection.from_api_response(
        info.tags, project_meta.tag_metas
    )
    single_tag = tag_collection.get_by_name("tag_name")
get_single_by_name(tag_name, default=None)[source]

Get a single Tag object by name from the VideoTagCollection.

Parameters
tag_name : str

Name of the tag to get.

Returns

VideoTag object with the specified name.

Return type

VideoTag

Usage example
import supervisely as sly

# You can connect to API directly
address = 'https://app.supervise.ly/'
token = 'Your Supervisely API Token'
api = sly.Api(address, token)

# Or you can use API from environment
os.environ['SERVER_ADDRESS'] = 'https://app.supervise.ly'
os.environ['API_TOKEN'] = 'Your Supervisely API Token'
api = sly.Api.from_env()

videos = api.video.get_list(dataset_id)
for info in videos:
    tag_collection = sly.VideoTagCollection.from_api_response(
        info.tags, project_meta.tag_metas
    )
    single_tag = tag_collection.get_single_by_name("tag_name")
has_key(key)

Check if given key(item name exist in collection).

Parameters
key : str

The key to look for in the collection.

Returns

Is the key in the collection or not

Return type

bool

Usage Example
import supervisely as sly

item_cat = sly.TagMeta('cat', sly.TagValueType.NONE)
item_turtle = sly.TagMeta('turtle', sly.TagValueType.ANY_STRING)
collection = sly.collection.key_indexed_collection.KeyIndexedCollection([item_cat, item_turtle])

collection.has_key('cat') # True
collection.has_key('hamster') # False
intersection(other)

Find intersection of given list of instances with collection items.

Parameters
key : List[KeyObject]

List of ObjClassCollection, TagMetaCollection or TagCollection objects.

Raises

ValueError if find items with same keys(item names)

Returns

MultiKeyIndexedCollection object

Return type

MultiKeyIndexedCollection

Usage Example
import supervisely as sly

item_cat = sly.TagMeta('cat', sly.TagValueType.NONE)
item_turtle = sly.TagMeta('turtle', sly.TagValueType.ANY_STRING)
other_cat = sly.ObjClass('cat', sly.Rectangle)
collection = sly.collection.key_indexed_collection.MultiKeyIndexedCollection([item_cat, item_turtle, other_cat])

# Note, item_cat_2 have same key as item_cat, but another value
item_cat_2 = sly.TagMeta('cat', sly.TagValueType.ANY_STRING)
item_turtle = sly.TagMeta('turtle', sly.TagValueType.ANY_STRING)
items = [item_cat_2, item_turtle]

intersect = collection.intersection(items)
print(intersect.to_json())
# Output: [
#     {
#         "name": "turtle",
#         "value_type": "any_string",
#         "color": "#5B8A0F",
#         "hotkey": "",
#         "applicable_type": "all",
#         "classes": []
#     }
# ]
items()

Get list of all items in collection.

Returns

List of ObjClassCollection, TagMetaCollection or TagCollection objects

Return type

List[KeyObject]

Usage Example
import supervisely as sly

item_cat = sly.TagMeta('cat', sly.TagValueType.NONE)
item_turtle = sly.TagMeta('turtle', sly.TagValueType.ANY_STRING)
other_cat = sly.ObjClass('cat', sly.Rectangle)
collection = sly.collection.key_indexed_collection.MultiKeyIndexedCollection([item_cat, item_turtle, other_cat])
print(collection.items())
# Output:
# [<supervisely.annotation.tag_meta.TagMeta object at 0x7fdbd28ce340>,
#  <supervisely.annotation.obj_class.ObjClass object at 0x7fdbd28ce3a0>,
#  <supervisely.annotation.tag_meta.TagMeta object at 0x7fdbd28ce370>]
keys()

Get list of all keys(item names) in collection.

Returns

List of collection keys

Return type

List[str]

Usage Example
import supervisely as sly

item_cat = sly.TagMeta('cat', sly.TagValueType.NONE)
item_turtle = sly.TagMeta('turtle', sly.TagValueType.ANY_STRING)
collection = sly.collection.key_indexed_collection.KeyIndexedCollection([item_cat, item_turtle])
keys = collection.keys() # ['cat', 'turtle']
merge(other)

Merge collection with other MultiKeyIndexedCollection object.

Parameters
key : MultiKeyIndexedCollection

MultiKeyIndexedCollection object.

Returns

MultiKeyIndexedCollection object

Return type

MultiKeyIndexedCollection

Usage Example
import supervisely as sly

item_cat = sly.TagMeta('cat', sly.TagValueType.NONE)
item_turtle = sly.TagMeta('turtle', sly.TagValueType.ANY_STRING)
other_cat = sly.ObjClass('cat', sly.Rectangle)
collection = sly.collection.key_indexed_collection.MultiKeyIndexedCollection([item_cat, item_turtle, other_cat])

item_dog = sly.TagMeta('dog', sly.TagValueType.NONE)
item_turtle = sly.TagMeta('turtle', sly.TagValueType.ANY_STRING)
other_collection = sly.collection.key_indexed_collection.MultiKeyIndexedCollection([item_dog, item_turtle])

merge = collection.merge(other_collection)
print(merge.to_json())
# Output: [
#     {
#         "name": "cat",
#         "value_type": "none",
#         "color": "#198A0F",
#         "hotkey": "",
#         "applicable_type": "all",
#         "classes": []
#     },
#     {
#         "title": "cat",
#         "shape": "rectangle",
#         "color": "#898A0F",
#         "geometry_config": {},
#         "hotkey": ""
#     },
#     {
#         "name": "turtle",
#         "value_type": "any_string",
#         "color": "#650F8A",
#         "hotkey": "",
#         "applicable_type": "all",
#         "classes": []
#     },
#     {
#         "name": "turtle",
#         "value_type": "any_string",
#         "color": "#0F8A83",
#         "hotkey": "",
#         "applicable_type": "all",
#         "classes": []
#     },
#     {
#         "name": "dog",
#         "value_type": "none",
#         "color": "#1A8A0F",
#         "hotkey": "",
#         "applicable_type": "all",
#         "classes": []
#     }
# ]
merge_without_duplicates(other)

Merge collection with other MultiKeyIndexedCollection object. Duplicates will be ignored.

Parameters
key : MultiKeyIndexedCollection

MultiKeyIndexedCollection object.

Raises

ValueError if item name from given MultiKeyIndexedCollection is in collection but items in both are different

Returns

MultiKeyIndexedCollection object

Return type

MultiKeyIndexedCollection

Usage Example
import supervisely as sly

item_cat = sly.TagMeta('cat', sly.TagValueType.NONE)
item_turtle = sly.TagMeta('turtle', sly.TagValueType.ANY_STRING)
other_cat = sly.ObjClass('cat', sly.Rectangle)
collection = sly.collection.key_indexed_collection.MultiKeyIndexedCollection([item_cat, item_turtle, other_cat])

item_dog = sly.TagMeta('dog', sly.TagValueType.NONE)
item_turtle = sly.TagMeta('turtle', sly.TagValueType.ANY_STRING)
other_collection = sly.collection.key_indexed_collection.MultiKeyIndexedCollection([item_dog, item_turtle])

merge = collection.merge_without_duplicates(other_collection)
print(merge.to_json())
# Output: [
#     {
#         "name": "dog",
#         "value_type": "none",
#         "color": "#8A0F37",
#         "hotkey": "",
#         "applicable_type": "all",
#         "classes": []
#     },
#     {
#         "name": "cat",
#         "value_type": "none",
#         "color": "#778A0F",
#         "hotkey": "",
#         "applicable_type": "all",
#         "classes": []
#     },
#     {
#         "title": "cat",
#         "shape": "rectangle",
#         "color": "#8A0F76",
#         "geometry_config": {},
#         "hotkey": ""
#     },
#     {
#         "name": "turtle",
#         "value_type": "any_string",
#         "color": "#850F8A",
#         "hotkey": "",
#         "applicable_type": "all",
#         "classes": []
#     }
# ]
remove_items(keys)

Remove items from collection by given list of keys. Creates a new instance of KeyIndexedCollection.

Parameters
keys : List[str]

List of keys(item names) in collection.

Returns

New instance of KeyIndexedCollection

Return type

KeyIndexedCollection

to_json(key_id_map=None)[source]

Convert the VideoTagCollection to a list of json dicts. Read more about Supervisely format.

Parameters
key_id_map : KeyIdMap, optional

KeyIdMap object.

Returns

List of dicts in json format

Return type

List[dict]

Usage example
import supervisely as sly
from supervisely.video_annotation.video_tag import VideoTag
from supervisely.video_annotation.video_tag_collection import VideoTagCollection

meta_car = sly.TagMeta('car_tag', sly.TagValueType.ANY_STRING)
car_tag = VideoTag(meta_car, value='acura')
meta_bus = sly.TagMeta('bus_tag', sly.TagValueType.ANY_STRING)
bus_tag = VideoTag(meta_bus, value='volvo')
tags = VideoTagCollection([car_tag, bus_tag])
tags_json = tags.to_json()
print(tags_json)
# Output: [
#     {
#         "name": "car_tag",
#         "value": "acura",
#         "key": "378408fcb6854305a38fed7c996f4901"
#     },
#     {
#         "name": "bus_tag",
#         "value": "volvo",
#         "key": "0c63174878204faea67c4025adec1e2a"
#     }
# ]