ObjClassCollection

class ObjClassCollection[source]

Bases: supervisely.collection.key_indexed_collection.KeyIndexedCollection, supervisely.io.json.JsonSerializable

Collection with ObjClass instances. ObjClassCollection object is immutable.

Raises

DuplicateKeyError if instance with given name already exist

Usage example
import supervisely as sly

# Create ObjClass (see class ObjClass for more information)
class_lemon = sly.ObjClass('lemon', sly.Rectangle)
class_kiwi = sly.ObjClass('kiwi', sly.Bitmap)

class_arr = [class_lemon, class_kiwi]

# Create ObjClassCollection from ObjClasses
classes = sly.ObjClassCollection(class_arr)

# Add items to ObjClassCollection
class_potato = sly.ObjClass('potato', sly.Bitmap)

# Remember that ObjClassCollection is immutable, and we need to assign new instance of ObjClassCollection to a new variable
classes = classes.add(class_potato)

# You can also add multiple items to collection
class_cabbage = sly.ObjClass('cabbage', sly.Rectangle)
class_carrot = sly.ObjClass('carrot', sly.Bitmap)
class_turnip = sly.ObjClass('turnip', sly.Polygon)

classes = classes.add_items([class_cabbage, class_carrot, class_turnip])

# Has key, checks if given key exist in collection
classes.has_key("cabbage")
# Output: True

# Intersection, finds intersection of given list of instances with collection items
class_dog = sly.ObjClass('dog', sly.Rectangle)
class_cat = sly.ObjClass('cat', sly.Rectangle)
class_turtle = sly.ObjClass('turtle', sly.Rectangle)

classes_animals = sly.ObjClassCollection([class_dog, class_cat, class_turtle])

classes_intersections = classes.intersection(classes_animals)
print(classes_intersections.to_json())
# Output: []

# Let's add the potato ObjClass from another collection and compare them again
classes_animals = classes_animals.add(class_potato)

classes_intersections = classes.intersection(classes_animals)
print(classes_intersections.to_json())
# Output: [
#     {
#         "title":"potato",
#         "shape":"bitmap",
#         "color":"#8A570F",
#         "geometry_config":{},
#         "hotkey":""
#     }
# ]

# Difference, finds difference between collection and given list of ObjClass or ObjClassCollection
class_car = sly.ObjClass('car', sly.Rectangle)
class_bicycle = sly.ObjClass('bicycle', sly.Rectangle)

classes_vehicles = sly.ObjClassCollection([class_car, class_bicycle])

class_pedestrian = sly.ObjClass('pedestrian', sly.Rectangle)
class_road = sly.ObjClass('road', sly.Rectangle)

difference = classes_vehicles.difference([class_pedestrian, class_road])
print(difference.to_json())
# Output: [
#     {
#         "title":"car",
#         "shape":"rectangle",
#         "color":"#8A0F3B",
#         "geometry_config":{},
#         "hotkey":""
#     },
#     {
#         "title":"bicycle",
#         "shape":"rectangle",
#         "color":"#0F8A1F",
#         "geometry_config":{},
#         "hotkey":""
#     }
# ]

# Merge, merges collection and given list of ObjClasses
c_1 = sly.ObjClassCollection([class_car, class_bicycle])
c_2 = sly.ObjClassCollection([class_pedestrian, class_road])

c_3 = c_1.merge(c_2)
print(c_3.to_json())
# Output: [
#     {
#         "title":"pedestrian",
#         "shape":"rectangle",
#         "color":"#8A0F27",
#         "geometry_config":{},
#         "hotkey":""
#     },
#     {
#         "title":"road",
#         "shape":"rectangle",
#         "color":"#8A620F",
#         "geometry_config":{},
#         "hotkey":""
#     },
#     {
#         "title":"car",
#         "shape":"rectangle",
#         "color":"#8A0F3B",
#         "geometry_config":{},
#         "hotkey":""
#     },
#     {
#         "title":"bicycle",
#         "shape":"rectangle",
#         "color":"#0F8A1F",
#         "geometry_config":{},
#         "hotkey":""
#     }
# ]

# Merge will raise ValueError if item name from given list is in collection but items in both are different
class_bicycle_1 = sly.ObjClass('bicycle', sly.Rectangle)
class_bicycle_2 = sly.ObjClass('bicycle', sly.Bitmap)

classes_1 = sly.ObjClassCollection([class_bicycle_1])
classes_2 = sly.ObjClassCollection([class_bicycle_2])

test_merge = classes_1.merge(classes_2)
# Output: ValueError: Error during merge for key 'bicycle': values are different

# Let's try to create now a collection where ObjClasses have identical names
class_cow = sly.ObjClass('cow', sly.Rectangle)
class_chicken = sly.ObjClass('cow', sly.Rectangle)

test_classes = sly.ObjClassCollection([class_cow, class_chicken])
# Output: DuplicateKeyError: "Key 'cow' already exists"

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 with dicts in json format to ObjClassCollection.

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.

refresh_ids_from

Updates ids of classes in the collection from given classes.

remove_items

Remove items from collection by given list of keys.

to_json

Convert the ObjClassCollection to a list of json dicts.

validate_classes_colors

Checks for unique colors in the ObjClassCollection.

item_type

alias of supervisely.annotation.obj_class.ObjClass

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)[source]

Convert a list with dicts in json format to ObjClassCollection. Read more about Supervisely format.

Parameters
data : List[dict]

List with dicts in json format.

Returns

ObjClassCollection object

Return type

ObjClassCollection

Usage example
import supervisely as sly

data = [
     {
          "title": "lemon",
          "shape": "rectangle",
          "color": "#300F8A",
          "hotkey": ""
     },
     {
          "title": "kiwi",
          "shape": "bitmap",
          "color": "#7C0F8A",
          "hotkey": ""
     }
]

classes = sly.ObjClassCollection.from_json(data)
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": []
#     }
# ]
refresh_ids_from(classes)[source]

Updates ids of classes in the collection from given classes.

Parameters
classes : ObjClassCollection

Collection with classes to update ids from.

Return type

None

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()[source]

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

Returns

List of dicts in json format

Return type

List[dict]

Usage example
import supervisely as sly

class_lemon = sly.ObjClass('lemon', sly.Rectangle)
class_kiwi = sly.ObjClass('kiwi', sly.Bitmap)

# Add ObjClasses to ObjClassCollection
classes = sly.ObjClassCollection([class_lemon, class_kiwi])

classes_json = classes.to_json()
print(classes_json)
# Output: [
#      {
#           "title": "lemon",
#           "shape": "rectangle",
#           "color": "#300F8A",
#           "geometry_config": {},
#           "hotkey": ""
#      },
#      {
#           "title": "kiwi",
#           "shape": "bitmap",
#           "color": "#7C0F8A",
#           "geometry_config": {},
#           "hotkey": ""
#      }
# ]
validate_classes_colors(logger=None)[source]

Checks for unique colors in the ObjClassCollection.

Parameters
logger : logger, optional

Input logger.

Returns

Notification if there are objects with the same colors, otherwise None

Return type

str or NoneType

Usage example
import supervisely as sly

# Let's create 2 ObjClasses with the same color
class_lemon = sly.ObjClass('lemon', sly.Rectangle, [0, 0, 0])
class_kiwi = sly.ObjClass('kiwi', sly.Bitmap, [0, 0, 0])

# Add them to ObjClassCollection
classes = sly.ObjClassCollection([class_lemon, class_kiwi])

print(classes.validate_classes_colors())
# Output: Classes ['lemon', 'kiwi'] have the same RGB color = [0, 0, 0]

# Now let's change colors of our ObjClasses
class_lemon = sly.ObjClass('lemon', sly.Rectangle, [255, 0, 0])
class_kiwi = sly.ObjClass('kiwi', sly.Bitmap, [0, 0, 255])

classes = sly.ObjClassCollection([class_lemon, class_kiwi])

print(classes.validate_classes_colors())
# Output: None