Frame

class supervisely_lib.video_annotation.frame.Frame(index: int, figures: Optional[list] = None)[source]

Bases: supervisely_lib.collection.key_indexed_collection.KeyObject

Frame object for VideoAnnotation. Frame object is immutable.

Parameters
  • index (int) – Index of the Frame.

  • figures (list, optional) – List of VideoFigures.

Usage example
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])
print(frame.to_json())
# Output: {
#     "index": 7,
#     "figures": [
#         {
#             "key": "39f3eb15791f4c72b7cdb98c17b3f0f1",
#             "objectKey": "319814af474941a98ca208c3fad5ed81",
#             "geometryType": "rectangle",
#             "geometry": {
#                 "points": {
#                     "exterior": [
#                         [
#                             0,
#                             0
#                         ],
#                         [
#                             100,
#                             100
#                         ]
#                     ],
#                     "interior": []
#                 }
#             }
#         }
#     ]
# }
property index

Frame index.

Returns

Frame index.

Return type

int

Usage example
frame_index = frame.index # 7
key()[source]
property figures

Frame figures.

Returns

List of figures on Frame.

Return type

List[VideoFigure]

Usage example
frame_figures = frame.figures
validate_figures_bounds(img_size: Optional[Tuple[int, int]] = None) → None[source]

Checks if image with given size contains a figure.

Parameters

img_size (Tuple[int, int], optional) – Size of the image (height, width).

Raises

OutOfImageBoundsExtension, if figure is out of image bounds

Returns

None

Return type

NoneType

Usage Example
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])

image_size = (20, 200)
frame.validate_figures_bounds(image_size)
# raise OutOfImageBoundsExtension("Figure is out of image bounds")
to_json(key_id_map: Optional[supervisely_lib.video_annotation.key_id_map.KeyIdMap] = None) → dict[source]

Convert the Frame to a json dict. Read more about Supervisely format.

Parameters

key_id_map (KeyIdMap, optional) – KeyIdMap object.

Returns

Json format as a dict

Return type

dict

Usage example
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_json = frame.to_json()
print(frame_json)
# Output: {
#     "index": 7,
#     "figures": [
#         {
#             "key": "39f3eb15791f4c72b7cdb98c17b3f0f1",
#             "objectKey": "319814af474941a98ca208c3fad5ed81",
#             "geometryType": "rectangle",
#             "geometry": {
#                 "points": {
#                     "exterior": [
#                         [
#                             0,
#                             0
#                         ],
#                         [
#                             100,
#                             100
#                         ]
#                     ],
#                     "interior": []
#                 }
#             }
#         }
#     ]
# }
classmethod from_json(data: 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.Frame[source]

Convert a json dict to Frame. Read more about Supervisely format.

Parameters
  • data (dict) – Dict in json format.

  • objects (VideoObjectCollection) – VideoObjectCollection object.

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

  • key_id_map (KeyIdMap, optional) – KeyIdMap object.

Raises

ValueError if frame index < 0 and if frame index > number of frames in video

Returns

Frame object

Return type

Frame

Usage example
frame_json = {
    "index": 7,
    "figures": [
        {
            "key": "22eef4321cc04e5190f39bd7fe5a5648",
            "objectKey": "05088eb5d6fc481caa98972c6e3189ff",
            "geometryType": "rectangle",
            "geometry": {
                "points": {
                    "exterior": [
                        [
                            0,
                            0
                        ],
                        [
                            100,
                            100
                        ]
                    ],
                    "interior": []
                }
            }
        }
    ]
}

class_car = sly.ObjClass('car', sly.Rectangle)
object_car = sly.VideoObject(class_car)
video_obj_coll = sly.VideoObjectCollection([object_car])

frame_car = sly.Frame.from_json(frame_json, video_obj_coll)
clone(index: Optional[int] = None, figures: Optional[list] = None)supervisely_lib.video_annotation.frame.Frame[source]

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

Parameters
  • index (int, optional) – Index of the Frame.

  • figures (list, optional) – List of VideoFigures.

Returns

Frame object

Return type

Frame

Usage example
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])

# Remember that Frame object is immutable, and we need to assign new instance of Frame to a new variable
new_frame = frame.clone(index=100, figures=[])
print(new_frame.to_json())
# Output: {
#     "index": 100,
#     "figures": []
# }