Frame Collection

class supervisely_lib.video_annotation.frame_collection.FrameCollection(items: Optional[dict] = None)[source]

Bases: supervisely_lib.collection.key_indexed_collection.KeyIndexedCollection

Collection with Frame instances. FrameCollection object is immutable.

Usage example
# 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

# Intersection, finds 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

# Difference, finds 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, merges 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": []
#     }
# ]
item_type

alias of supervisely_lib.video_annotation.frame.Frame

to_json(key_id_map: Optional[supervisely_lib.video_annotation.key_id_map.KeyIdMap] = None) → List[dict][source]

Convert the FrameCollection 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
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": []
#     }
# ]
classmethod from_json(data: List[dict], objects: supervisely_lib.video_annotation.video_object_collection.VideoObjectCollection, frames_count: Optional[int] = None, key_id_map: Optional[supervisely_lib.video_annotation.key_id_map.KeyIdMap] = None)supervisely_lib.video_annotation.frame_collection.FrameCollection[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) – VideoObjectCollection object.

  • frames_count (int, optional) – Number of frames in video.

  • key_id_map (KeyIdMap, optional) – KeyIdMap object.

Returns

FrameCollection object

Return type

FrameCollection

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

objects = []
fr_collection = sly.FrameCollection.from_json(fr_collection_json, objects)
property figures

Get figures from all frames in collection.

Returns

List of figures from all frames in collection

Return type

List[VideoFigure]

Usage example
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
get_figures_and_keys(key_id_map: supervisely_lib.video_annotation.key_id_map.KeyIdMap) → Tuple[list, list][source]

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

Parameters

key_id_map (KeyIdMap) – KeyIdMap object.

Returns

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

Return type

Tuple[list, list]

Usage example
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
#         }
#     }
# ]