FrameCollection

class FrameCollection(items=None)[source]

Bases: KeyIndexedCollection

Collection of Frame instances indexed by frame index. Immutable.

Usage Example:
import supervisely as sly

# Create two frames for collection
fr_index_1 = 7
frame_1 = sly.Frame(fr_index_1)
fr_index_2 = 10
frame_2 = sly.Frame(fr_index_2)

# Create FrameCollection
fr_collection = sly.FrameCollection([frame_1, frame_2])
print(fr_collection.to_json())
# Output: [
#     {
#         "index": 7,
#         "figures": []
#     },
#     {
#         "index": 10,
#         "figures": []
#     }
# ]

# Add item to FrameCollection
frame_3 = sly.Frame(12)

# Remember that TagCollection is immutable, and we need to assign new instance of TagCollection to a new variable
new_fr_collection = fr_collection.add(frame_3)
print(new_fr_collection.to_json())
# Output: [
#     {
#         "index": 7,
#         "figures": []
#     },
#     {
#         "index": 10,
#         "figures": []
#     },
#     {
#         "index": 12,
#         "figures": []
#     }
# ]

# You can also add multiple items to collection
frame_3 = sly.Frame(12)
frame_4 = sly.Frame(15)

# Remember that TagCollection is immutable, and we need to assign new instance of TagCollection to a new variable
new_fr_collection = fr_collection.add_items([frame_3, frame_4])
print(new_fr_collection.to_json())
# Output: [
#     {
#         "index": 7,
#         "figures": []
#     },
#     {
#         "index": 10,
#         "figures": []
#     },
#     {
#         "index": 12,
#         "figures": []
#     },
#     {
#         "index": 15,
#         "figures": []
#     }
# ]

# Has key, checks if given key exist in collection
fr_collection.has_key(7)
# Output: True

# Find intersection of given list of instances with collection items
frame_1 = sly.Frame(7)
frame_2 = sly.Frame(10)
fr_collection = sly.FrameCollection([frame_1, frame_2])
frame_3 = sly.Frame(12)
frames_intersections = fr_collection.intersection([frame_3])
print(frames_intersections.to_json())
# Output: []

frames_intersections = fr_collection.intersection([frame_2])
print(frames_intersections.to_json())
# Output: [
#     {
#         "index": 10,
#         "figures": []
#     }
# ]

# Note, two frames with the same index values are not equal
frame_4 = sly.Frame(10)
frames_intersections = fr_collection.intersection([frame_4])
# Output:
# ValueError: Different values for the same key 10

# Find difference between collection and given list of Frames
frames_difference = fr_collection.difference([frame_2])
print(frames_difference.to_json())
# Output: [
#     {
#         "index": 7,
#         "figures": []
#     }
# ]

# Merge collection and given list of FrameCollection
frame_3 = sly.Frame(12)
frame_4 = sly.Frame(15)
over_collection = sly.FrameCollection([frame_3, frame_4])
merged_collection = fr_collection.merge(over_collection)
print(merged_collection.to_json())
# Output: [
#     {
#         "index": 12,
#         "figures": []
#     },
#     {
#         "index": 15,
#         "figures": []
#     },
#     {
#         "index": 7,
#         "figures": []
#     },
#     {
#         "index": 10,
#         "figures": []
#     }
# ]

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 FrameCollection.

get

Get item from collection with given key(name) and set a default if item does not exist.

get_figures_and_keys

Get figures from all frames in collection in json format, keys from all figures in frames in collection.

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 FrameCollection to a list of json dicts.

Attributes

figures

Get figures from all frames in collection.

item_type

alias of Frame

classmethod from_json(data, objects, frames_count=None, key_id_map=None, skip_corrupted=False)[source]

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

Parameters:
data : List[dict]

List with dicts in json format.

objects

VideoObjectCollection object.

frames_count : int, optional

Number of frames in video.

key_id_map=None

KeyIdMap object.

Returns:

FrameCollection object.

Return type:

FrameCollection

Usage Example:
import supervisely as sly

fr_collection_json = [
    {"index": 7, "figures": []},
    {"index": 10, "figures": []}
]

objects = []
fr_collection = sly.FrameCollection.from_json(fr_collection_json, objects)
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)[source]

Get item from collection with given key(name) and set a default if item does not exist.

Parameters:
key : str

Name of Frame in collection.

default : Optional[Any]

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

Returns:

Frame, Slice or PointcloudEpisodeFrame object

Return type:

KeyObject

Usage Example:
import supervisely as sly

frame_index = 7
geometry = sly.Rectangle(0, 0, 100, 100)
class_car = sly.ObjClass('car', sly.Rectangle)
object_car = sly.VideoObject(class_car)
figure_car = sly.VideoFigure(object_car, geometry, frame_index)

frame = sly.Frame(frame_index, figures=[figure_car])
frame_collection = sly.FrameCollection([frame])

item = frame_collection.get(frame_index)
print(item.to_json())
# Output: {
#     "figures": [
#         {
#         "geometry": {
#             "points": {
#             "exterior": [
#                 [0, 0],
#                 [100, 100]
#             ],
#             "interior": []
#             }
#         },
#         "geometryType": "rectangle",
#         "key": "713968a7d5384709bc5d4e63cd4535f2",
#         "objectKey": "3342e68eff3b44dcb75712499265be55"
#         }
#     ],
#     "index": 7
# }
get_figures_and_keys(key_id_map)[source]

Get figures from all frames in collection in json format, keys from all figures in frames in collection.

Parameters:
key_id_map

KeyIdMap object.

Returns:

Figures from all frames in collection in json format, keys from all figures in frames in collection

Return type:

Tuple[List[dict], List[str]]

Usage Example:
import supervisely as sly
from supervisely.video_annotation.key_id_map import KeyIdMap

key_id_map = KeyIdMap()

fr_index_1 = 7
geometry = sly.Rectangle(0, 0, 100, 100)
obj_class_car = sly.ObjClass('car', sly.Rectangle)
video_object_car = sly.VideoObject(obj_class_car)
video_figure_car = sly.VideoFigure(video_object_car, geometry, fr_index_1)
frame_1 = sly.Frame(fr_index_1, figures=[video_figure_car])

fr_index_2 = 10
geometry = sly.Rectangle(0, 0, 500, 600)
obj_class_bus = sly.ObjClass('bus', sly.Rectangle)
video_object_bus = sly.VideoObject(obj_class_bus)
video_figure_bus = sly.VideoFigure(video_object_bus, geometry, fr_index_2)
frame_2 = sly.Frame(fr_index_2, figures=[video_figure_bus])

fr_collection = sly.FrameCollection([frame_1, frame_2])
figures, keys = fr_collection.get_figures_and_keys(key_id_map)
print(keys) # [UUID('0ac041b2-314e-4f6b-9d38-704b341fb383'), UUID('88aa1cb3-b1e3-480f-8ace-6346c9a9daba')]

print(figures)
# Output: [
#     {
#         "key": "a8cae05d6b8c4a67b18004130941fdec",
#         "objectKey": "cc9a9475d360481c9753f8ac3c63f8b7",
#         "geometryType": "rectangle",
#         "geometry": {
#             "points": {
#                 "exterior": [
#                     [
#                         0,
#                         0
#                     ],
#                     [
#                         100,
#                         100
#                     ]
#                 ],
#                 "interior": []
#             }
#         },
#         "meta": {
#             "frame": 7
#         }
#     },
#     {
#         "key": "6e00287acc4644dfb21d67406534080b",
#         "objectKey": "cad78d53ffc84e69a28f5f8941be9021",
#         "geometryType": "rectangle",
#         "geometry": {
#             "points": {
#                 "exterior": [
#                     [
#                         0,
#                         0
#                     ],
#                     [
#                         600,
#                         500
#                     ]
#                 ],
#                 "interior": []
#             }
#         },
#         "meta": {
#             "frame": 10
#         }
#     }
# ]
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)[source]

Convert the FrameCollection 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

frame_1 = sly.Frame(7)
frame_2 = sly.Frame(10)

# Create FrameCollection
fr_collection = sly.FrameCollection([frame_1, frame_2])
print(fr_collection.to_json())
# Output: [
#     {
#         "index": 7,
#         "figures": []
#     },
#     {
#         "index": 10,
#         "figures": []
#     }
# ]
property figures : list[supervisely.video_annotation.video_figure.VideoFigure]

Get figures from all frames in collection.

Returns:

List of figures from all frames in collection

Return type:

List[VideoFigure]

Usage Example:
import supervisely as sly

fr_index_1 = 7
geometry = sly.Rectangle(0, 0, 100, 100)
obj_class_car = sly.ObjClass('car', sly.Rectangle)
video_object_car = sly.VideoObject(obj_class_car)
video_figure_car = sly.VideoFigure(video_object_car, geometry, fr_index_1)
frame_1 = sly.Frame(fr_index_1, figures=[video_figure_car])

fr_index_2 = 10
geometry = sly.Rectangle(0, 0, 500, 600)
obj_class_bus = sly.ObjClass('bus', sly.Rectangle)
video_object_bus = sly.VideoObject(obj_class_bus)
video_figure_bus = sly.VideoFigure(video_object_bus, geometry, fr_index_2)
frame_2 = sly.Frame(fr_index_2, figures=[video_figure_bus])

fr_collection = sly.FrameCollection([frame_1, frame_2])
figures = fr_collection.figures