VolumeObjectCollection

class VolumeObjectCollection(items=None)[source]

Bases: VideoObjectCollection

Collection with VolumeObject instances. VolumeObjectCollection object is immutable.

Usage Example:
import supervisely as sly

# Create two VolumeObjects for collection
class_heart = sly.ObjClass('heart', sly.Rectangle)
obj_heart = sly.VolumeObject(class_heart)
class_lang = sly.ObjClass('lang', sly.Rectangle)
obj_lang = sly.VolumeObject(class_lang)

# Create VolumeObjectCollection
obj_collection = sly.VolumeObjectCollection([obj_heart, obj_lang])
obj_collection_json = obj_collection.to_json()
print(obj_collection_json)
# Output: [
#     {
#         "key": "773300c59fde4707887068d555269ba5",
#         "classTitle": "heart",
#         "tags": []
#     },
#     {
#         "key": "8257371497d2402cadabc690f796b1d1",
#         "classTitle": "lang",
#         "tags": []
#     }
# ]

# Add item to VolumeObjectCollection
class_arm = sly.ObjClass('arm', sly.Rectangle)
obj_arm = sly.VolumeObject(class_arm)
# Remember that VolumeObjectCollection is immutable, and we need to assign new instance of VolumeObjectCollection to a new variable
new_obj_collection = obj_collection.add(obj_arm)
new_obj_collection_json = new_obj_collection.to_json()
print(new_obj_collection_json)
# Output: [
#     {
#         "key": "1b62882f180f49ae96744e13fba26d02",
#         "classTitle": "heart",
#         "tags": []
#     },
#     {
#         "key": "0c95107fce6c4ac1b42132e83c1a6b3b",
#         "classTitle": "lang",
#         "tags": []
#     },
#     {
#         "key": "660d8572e20c4101b4cc7edf7f04b090",
#         "classTitle": "arm",
#         "tags": []
#     }
# ]

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

# Find intersection of given list of instances with collection items
obj_intersections = obj_collection.intersection([obj_heart])
obj_intersections_json = obj_intersections.to_json()
print(obj_intersections_json)
# Output: [
#     {
#         "key": "5fae14a4a42c42a29904a887442162c9",
#         "classTitle": "heart",
#         "tags": []
#     }
# ]

# Find difference between collection and given list of VolumeObject
obj_diff = obj_collection.difference([obj_heart])
obj_diff_json = obj_diff.to_json()
print(obj_diff_json)
# Output: [
#     {
#         "key": "b4365bbec0314de58e20267735a39164",
#         "classTitle": "lang",
#         "tags": []
#     }
# ]

# Merge collection and given list of VolumeObjectCollection

class_arm = sly.ObjClass('arm', sly.Rectangle)
obj_arm = sly.VolumeObject(class_arm)
class_train = sly.ObjClass('train', sly.Rectangle)
obj_train = sly.VolumeObject(class_train)
over_collection = sly.VolumeObjectCollection([obj_arm, 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": "arm",
#         "tags": []
#     },
#     {
#         "key": "a010f91a5e7646bdb8e06dfd6fa50f30",
#         "classTitle": "train",
#         "tags": []
#     },
#     {
#         "key": "8440e915a94640b0be279c4fa293553b",
#         "classTitle": "heart",
#         "tags": []
#     },
#     {
#         "key": "e03e7dd2a2d14854945b6ee7bf7c72e3",
#         "classTitle": "lang",
#         "tags": []
#     }
# ]

Base class for ObjClassCollection, TagMetaCollection and TagCollection instances. It is an analogue of python’s standard Dict. It allows to store objects inherited from KeyObject.

Parameters:
items : list, optional

List of ObjClassCollection, TagMetaCollection and TagCollection objects.

:raises DuplicateKeyError, when trying to add object with already existing key

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])
print(collection.to_json())
# Output: [
#     {
#         "name": "cat",
#         "value_type": "none",
#         "color": "#8A0F12",
#         "hotkey": "",
#         "applicable_type": "all",
#         "classes": []
#     },
#     {
#         "name": "turtle",
#         "value_type": "any_string",
#         "color": "#8A860F",
#         "hotkey": "",
#         "applicable_type": "all",
#         "classes": []
#     }
# ]

# Try to add item with a key that already exists in the collection
dublicate_item = sly.ObjClass('cat', sly.Rectangle)
new_collection = collection.add(dublicate_item)
# Output:
# DuplicateKeyError: "Key 'cat' already exists"

# Add item with a key that not exist in the collection
item_dog = sly.ObjClass('dog', sly.Rectangle)
new_collection = collection.add(item_dog)
print(new_collection.to_json())
# Output: [
#     {
#         "name": "cat",
#         "value_type": "none",
#         "color": "#668A0F",
#         "hotkey": "",
#         "applicable_type": "all",
#         "classes": []
#     },
#     {
#         "name": "turtle",
#         "value_type": "any_string",
#         "color": "#4D0F8A",
#         "hotkey": "",
#         "applicable_type": "all",
#         "classes": []
#     },
#     {
#         "title": "dog",
#         "shape": "rectangle",
#         "color": "#0F7F8A",
#         "geometry_config": {},
#         "hotkey": ""
#     }
# ]

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 VolumeObject

classmethod from_json(data, project_meta, key_id_map=None)

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

Input ProjectMeta object.

key_id_map=None

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)
add(item)

Add given item to collection.

Parameters:
item

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 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=None

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:
other

List of items to subtract from the collection.

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": []
#     }
# ]
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:
other

List of items to intersect with the collection.

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:
other

Other collection to merge with.

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)

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

Parameters:
key_id_map=None

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": []
#     }
# ]