VideoObjectCollection

class VideoObjectCollection[source]

Bases: supervisely.collection.key_indexed_collection.KeyIndexedCollection

Collection with VideoObject instances. VideoObjectCollection object is immutable.

Usage example
import supervisely as sly

# Create two VideoObjects for collection
class_car = sly.ObjClass('car', sly.Rectangle)
obj_car = sly.VideoObject(class_car)
class_bus = sly.ObjClass('bus', sly.Rectangle)
obj_bus = sly.VideoObject(class_bus)

# Create VideoObjectCollection
obj_collection = sly.VideoObjectCollection([obj_car, obj_bus])
obj_collection_json = obj_collection.to_json()
print(obj_collection_json)
# Output: [
#     {
#         "key": "773300c59fde4707887068d555269ba5",
#         "classTitle": "car",
#         "tags": []
#     },
#     {
#         "key": "8257371497d2402cadabc690f796b1d1",
#         "classTitle": "bus",
#         "tags": []
#     }
# ]

# Add item to VideoObjectCollection
class_truck = sly.ObjClass('truck', sly.Rectangle)
obj_truck = sly.VideoObject(class_truck)
# Remember that VideoObjectCollection is immutable, and we need to assign new instance of VideoObjectCollection to a new variable
new_obj_collection = obj_collection.add(obj_truck)
new_obj_collection_json = new_obj_collection.to_json()
print(new_obj_collection_json)
# Output: [
#     {
#         "key": "1b62882f180f49ae96744e13fba26d02",
#         "classTitle": "car",
#         "tags": []
#     },
#     {
#         "key": "0c95107fce6c4ac1b42132e83c1a6b3b",
#         "classTitle": "bus",
#         "tags": []
#     },
#     {
#         "key": "660d8572e20c4101b4cc7edf7f04b090",
#         "classTitle": "truck",
#         "tags": []
#     }
# ]

# You can also add multiple items to collection
class_truck = sly.ObjClass('truck', sly.Rectangle)
obj_truck = sly.VideoObject(class_truck)
class_train = sly.ObjClass('train', sly.Rectangle)
obj_train = sly.VideoObject(class_train)
# Remember that VideoObjectCollection is immutable, and we need to assign new instance of VideoObjectCollection to a new variable
new_obj_collection = obj_collection.add_items([obj_truck, obj_train])
new_obj_collection_json = new_obj_collection.to_json()
print(new_obj_collection_json)
# Output: [
#     {
#         "key": "892c62aae48b495687fe8b33ce8ebe96",
#         "classTitle": "car",
#         "tags": []
#     },
#     {
#         "key": "db0f64a7b60447769374943077e57679",
#         "classTitle": "bus",
#         "tags": []
#     },
#     {
#         "key": "72c24a107f344ef68d4fe8e93dff8184",
#         "classTitle": "truck",
#         "tags": []
#     },
#     {
#         "key": "d20738a608234152b43b1c23b7958c47",
#         "classTitle": "train",
#         "tags": []
#     }
# ]

# Intersection, finds intersection of given list of instances with collection items

obj_intersections = obj_collection.intersection([obj_car])
obj_intersections_json = obj_intersections.to_json()
print(obj_intersections_json)
# Output: [
#     {
#         "key": "5fae14a4a42c42a29904a887442162c9",
#         "classTitle": "car",
#         "tags": []
#     }
# ]

# Difference, finds difference between collection and given list of VideoObject

obj_diff = obj_collection.difference([obj_car])
obj_diff_json = obj_diff.to_json()
print(obj_diff_json)
# Output: [
#     {
#         "key": "b4365bbec0314de58e20267735a39164",
#         "classTitle": "bus",
#         "tags": []
#     }
# ]

# Merge, merges collection and given list of VideoObjectCollection

class_truck = sly.ObjClass('truck', sly.Rectangle)
obj_truck = sly.VideoObject(class_truck)
class_train = sly.ObjClass('train', sly.Rectangle)
obj_train = sly.VideoObject(class_train)
over_collection = sly.VideoObjectCollection([obj_truck, obj_train])
# Merge
merge_collection = obj_collection.merge(over_collection)
merge_collection_json = merge_collection.to_json()
print(merge_collection_json)
# Output: [
#     {
#         "key": "8e6f9f05f0954193bdb2005b9cd6e20b",
#         "classTitle": "truck",
#         "tags": []
#     },
#     {
#         "key": "a010f91a5e7646bdb8e06dfd6fa50f30",
#         "classTitle": "train",
#         "tags": []
#     },
#     {
#         "key": "8440e915a94640b0be279c4fa293553b",
#         "classTitle": "car",
#         "tags": []
#     },
#     {
#         "key": "e03e7dd2a2d14854945b6ee7bf7c72e3",
#         "classTitle": "bus",
#         "tags": []
#     }
# ]

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_json

Convert a list of json dicts to VideoObjectCollection.

get

Get item from collection with given key(name).

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 and other KeyIndexedCollection object.

remove_items

Remove items from collection by given list of keys.

to_json

Convert the VideoObjectCollection to a list of json dicts.

item_type

alias of supervisely.video_annotation.video_object.VideoObject

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_json(data, project_meta, key_id_map=None)[source]

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

Parameters
data : List[dict]

List with dicts in json format.

project_meta : ProjectMeta

Input ProjectMeta.

key_id_map : KeyIdMap, optional

KeyIdMap object.

Returns

VideoObjectCollection object

Return type

VideoObjectCollection

Usage example
import supervisely as sly

obj_collection_json = [
    {
        "classTitle": "car",
        "tags": []
    },
    {
        "classTitle": "bus",
        "tags": []
    }
]

class_car = sly.ObjClass('car', sly.Rectangle)
class_bus = sly.ObjClass('bus', sly.Rectangle)
classes = sly.ObjClassCollection([class_car, class_bus])
meta = sly.ProjectMeta(obj_classes=classes)

video_obj_collection = sly.VideoObjectCollection.from_json(obj_collection_json, meta)
get(key, default=None)

Get item from collection with given key(name).

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)
collection = sly.collection.key_indexed_collection.KeyIndexedCollection([item_cat, item_turtle])

item_cat = collection.get('cat')
print(item_cat)
# Output:
# Name:  cat                      Value type:none          Possible values:None       Hotkey                  Applicable toall        Applicable classes[]

item_not_exist = collection.get('no_item', {1: 2})
print(item_not_exist)
# Output:
# {1: 2}
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

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]

intersection = collection.intersection(items)
print(intersection.to_json())
# Output: [
#     {
#         "name": "turtle",
#         "value_type": "any_string",
#         "color": "#760F8A",
#         "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)
collection = sly.collection.key_indexed_collection.KeyIndexedCollection([item_cat, item_turtle])
items = collection.items()
print(items)
# Output:
# [<supervisely.annotation.tag_meta.TagMeta object at 0x7fd08eae4340>,
#  <supervisely.annotation.tag_meta.TagMeta object at 0x7fd08eae4370>]
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 and other KeyIndexedCollection object.

Parameters
key : KeyIndexedCollection

KeyIndexedCollection object.

Raises

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

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)
other_collection = sly.collection.key_indexed_collection.KeyIndexedCollection([item_dog, item_turtle])

merge = collection.merge(other_collection)
print(merge.to_json())
# Output: [
#     {
#         "name": "dog",
#         "value_type": "none",
#         "color": "#8A6C0F",
#         "hotkey": "",
#         "applicable_type": "all",
#         "classes": []
#     },
#     {
#         "name": "cat",
#         "value_type": "none",
#         "color": "#0F4A8A",
#         "hotkey": "",
#         "applicable_type": "all",
#         "classes": []
#     },
#     {
#         "name": "turtle",
#         "value_type": "any_string",
#         "color": "#4F0F8A",
#         "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 VideoObjectCollection 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

class_car = sly.ObjClass('car', sly.Rectangle)
obj_car = sly.VideoObject(class_car)
class_bus = sly.ObjClass('bus', sly.Rectangle)
obj_bus = sly.VideoObject(class_bus)
obj_collection = sly.VideoObjectCollection([obj_car, obj_bus])
obj_collection_json = obj_collection.to_json()
print(obj_collection_json)
# Output: [
#     {
#         "key": "773300c59fde4707887068d555269ba5",
#         "classTitle": "car",
#         "tags": []
#     },
#     {
#         "key": "8257371497d2402cadabc690f796b1d1",
#         "classTitle": "bus",
#         "tags": []
#     }
# ]