VolumeTagCollection¶
-
class VolumeTagCollection(items=
None)[source]¶ Bases:
VideoTagCollectionCollection with
VolumeTaginstances.VolumeTagCollectionobject is immutable.- Usage Example:
import supervisely as sly from supervisely.volume_annotation.volume_tag import VolumeTag from supervisely.volume_annotation.volume_tag_collection import VolumeTagCollection # Create two VolumeTags for collection meta_brain = sly.TagMeta('brain_tag', sly.TagValueType.ANY_STRING) brain_tag = VolumeTag(meta_brain, value='brain') meta_heart = sly.TagMeta('heart_tag', sly.TagValueType.ANY_STRING) heart_tag = VolumeTag(meta_heart, value='heart') # Create VolumeTagCollection tags = VolumeTagCollection([brain_tag, heart_tag]) tags_json = tags.to_json() print(tags_json) # Output: # [ # { # "key": "9fbbc3f888594a538243445fe25242ec", # "name": "brain_tag", # "value": "brain" # }, # { # "key": "804c8124d46c4da89b54c6132acf06a0", # "name": "heart_tag", # "value": "heart" # } # ] # Add item to VolumeTagCollection meta_lang = sly.TagMeta('lang_tag', sly.TagValueType.NONE) lang_tag = VolumeTag(meta_lang) # Remember that VolumeTagCollection is immutable, and we need to assign new instance of VolumeTagCollection to a new variable new_tags = tags.add(lang_tag) new_tags_json = new_tags.to_json() print(new_tags_json) # Output: [ # { # { # "key": "9fbbc3f888594a538243445fe25242ec", # "name": "brain_tag", # "value": "brain" # }, # { # "key": "804c8124d46c4da89b54c6132acf06a0", # "name": "heart_tag", # "value": "heart" # }, # { # "key": "7188242d2ddb4d2783c588cc2eca5ff8", # "name": "lang_tag" # } # ] # You can also add multiple items to collection meta_leg = sly.TagMeta('leg_tag', sly.TagValueType.NONE) leg_tag = VolumeTag(meta_leg) meta_arm = sly.TagMeta('arm_tag', sly.TagValueType.ANY_NUMBER) arm_tag = VolumeTag(meta_arm, value=777) new_tags = tags.add_items([leg_tag, arm_tag]) new_tags_json = new_tags.to_json() print(new_tags_json) # Output: [ # { # { # "key": "9fbbc3f888594a538243445fe25242ec", # "name": "brain_tag", # "value": "brain" # }, # { # "key": "804c8124d46c4da89b54c6132acf06a0", # "name": "heart_tag", # "value": "heart" # }, # { # "key": "7188242d2ddb4d2783c588cc2eca5ff8", # "name": "lang_tag" # }, # { # "key": "111c8124d46c4da89b5lgk132acf06a0", # "name": "leg_tag", # }, # { # "key": "28dc8124d46c4da89b54c6132acf06a0", # "name": "arm_tag", # "value": "777" # }, # ] # Find intersection of given list of VolumeTag instances with collection items intersect_tags = tags.intersection([leg_tag]) intersect_tags_json = intersect_tags.to_json() print(intersect_tags_json) # Output: [ # { # "key": "111c8124d46c4da89b5lgk132acf06a0", # "name": "leg_tag", # } # ] # Find difference between collection and given list of VolumeTag diff_tags = tags.difference([leg_tag, arm_tag, lang_tag]) diff_tags_json = diff_tags.to_json() print(diff_tags_json) # Output: # [ # { # "key": "9fbbc3f888594a538243445fe25242ec", # "name": "brain_tag", # "value": "brain" # }, # { # "key": "804c8124d46c4da89b54c6132acf06a0", # "name": "heart_tag", # "value": "heart" # } # ] # Merge collection and given list of VolumeTagCollection meta_leg = sly.TagMeta('leg_tag', sly.TagValueType.NONE) leg_tag = VolumeTag(meta_leg) meta_arm = sly.TagMeta('arm_tag', sly.TagValueType.ANY_NUMBER) arm_tag = VolumeTag(meta_arm, value=777) over_tags = VolumeTagCollection([leg_tag, arm_tag]) # Merge merge_tags = tags.merge(over_tags) merge_tags_json = merge_tags.to_json() print(merge_tags_json) # Output: [ # [ # { # "key": "9fbbc3f888594a538243445fe25242ec", # "name": "brain_tag", # "value": "brain" # }, # { # "key": "804c8124d46c4da89b54c6132acf06a0", # "name": "heart_tag", # "value": "heart" # }, # { # "key": "111c8124d46c4da89b5lgk132acf06a0", # "name": "leg_tag", # }, # { # "key": "28dc8124d46c4da89b54c6132acf06a0", # "name": "arm_tag", # "value": "777" # } # ]- Parameters:
:raises
DuplicateKeyError: if instance with given name already exist- Usage Example:
import supervisely as sly # Create TagMetas (see class TagMeta for additional information about creating TagMetas) meta_weather = sly.TagMeta('Weather', sly.TagValueType.ANY_STRING) seasons = ["Winter", "Spring", "Summer", "Autumn"] meta_season = sly.TagMeta('Season', sly.TagValueType.ONEOF_STRING, possible_values=seasons) # Create Tags tag_weather = sly.Tag(meta_weather, value="Sunny") tag_season = sly.Tag(meta_season, value="Spring") tags_arr = [tag_weather, tag_season] # Create TagCollection from Tags tags = sly.TagCollection(tags_arr) # Add item to TagCollection meta_potato = sly.TagMeta('potato', sly.TagValueType.NONE) tag_potato =sly.Tag(meta_potato) # Remember that TagCollection is immutable, and we need to assign new instance of TagCollection to a new variable tags = tags.add(tag_potato) # You can also add multiple items to collection meta_cabbage = sly.TagMeta('cabbage', sly.TagValueType.NONE) meta_carrot = sly.TagMeta('carrot', sly.TagValueType.NONE) meta_turnip = sly.TagMeta('turnip', sly.TagValueType.NONE) tag_cabbage = sly.Tag(meta_cabbage) tag_carrot = sly.Tag(meta_carrot) tag_turnip = sly.Tag(meta_turnip) additional_veggies = [tag_cabbage, tag_carrot, tag_turnip] tags = tags.add_items(additional_veggies) # Has key, checks if given key exist in collection tags.has_key("cabbage") # Output: True # Find intersection of given list of instances with collection items meta_dog = sly.TagMeta('dog', sly.TagValueType.NONE) tag_dog = sly.Tag(meta_dog) meta_cat = sly.TagMeta('cat', sly.TagValueType.NONE) tag_cat = sly.Tag(meta_cat) meta_turtle = sly.TagMeta('turtle', sly.TagValueType.NONE) tag_turtle = sly.Tag(meta_turtle) tags_animals = sly.TagCollection([tag_dog, tag_cat, tag_turtle]) tags_intersections = tags.intersection(tags_animals) print(tags_intersections.to_json()) # Output: [] # Let's add the potato Tag from another collection and compare them again tags_animals = tags_animals.add(tag_potato) tags_intersections = tags.intersection(tags_animals) print(tags_intersections.to_json()) # Output: [ # { # "name":"potato" # } # ] # Find difference between collection and given list of Tags or TagCollection meta_car = sly.TagMeta('car', sly.TagValueType.NONE) tag_car = sly.Tag(meta_car) meta_bicycle = sly.TagMeta('bicycle', sly.TagValueType.NONE) tag_bicycle = sly.Tag(meta_bicycle) tags_vehicles = sly.TagCollection([tag_car, tag_bicycle]) meta_pedestrian = sly.TagMeta('pedestrian', sly.TagValueType.NONE) tag_pedestrian = sly.Tag(meta_pedestrian) meta_road = sly.TagMeta('road', sly.TagValueType.NONE) tag_road = sly.Tag(meta_road) difference = tags_vehicles.difference([tag_pedestrian, tag_road]) print(difference.to_json()) # Output: [ # { # "name":"car" # }, # { # "name":"bicycle" # } # ] # Merge collection and given list of TagMetas tags_vehicles = sly.TagMetaCollection([tag_car, tag_bicycle]) tags_merge = sly.TagMetaCollection([tag_pedestrian, tag_road]) merged_collections = tags_vehicles.merge(tags_merge) print(merged_collections.to_json()) # Output: [ # { # "name":"pedestrian" # }, # { # "name":"road" # }, # { # "name":"car" # }, # { # "name":"bicycle" # } # ]
Methods
Add given item to collection.
Add items from given list to collection.
Makes a copy of KeyIndexedCollection with new fields, if fields are given, otherwise it will use fields of the original KeyIndexedCollection.
Find difference between collection and given list of instances.
Create a VideoTagCollection object from API response data.
Convert a list of json dicts to VideoTagCollection.
Get item from collection with given key(name).
Get item from collection with given key(name).
Get a list of VideoTag objects by name from the VideoTagCollection.
Get a single Tag object by name from the VideoTagCollection.
Check if given key(item name exist in collection).
Find intersection of given list of instances with collection items.
Get list of all items in collection.
Get list of all keys(item names) in collection.
Merge collection with other MultiKeyIndexedCollection object.
Merge collection with other MultiKeyIndexedCollection object.
Remove items from collection by given list of keys.
Convert the VideoTagCollection to a list of json dicts.
-
classmethod from_api_response(data, tag_meta_collection, id_to_tagmeta=
None)¶ Create a VideoTagCollection object from API response data.
- Parameters:
- Returns:
VideoTagCollection object.
- Return type:
- Usage Example:
import os from dotenv import load_dotenv import supervisely as sly # Load secrets and create API object from .env file (recommended) # Learn more here: https://developer.supervisely.com/getting-started/basics-of-authentication if sly.is_development(): load_dotenv(os.path.expanduser("~/supervisely.env")) api = sly.Api.from_env() videos = api.video.get_list(dataset_id) for info in videos: tag_collection = sly.VideoTagCollection.from_api_response( info.tags, project_meta.tag_metas )
-
classmethod from_json(data, tag_meta_collection, key_id_map=
None)¶ Convert a list of json dicts to VideoTagCollection. Read more about Supervisely format.
- Parameters:
- data : List[dict]¶
List with dicts in json format.
- project_meta :
TagMetaCollection TagMetaCollection object.
- key_id_map=
None¶ KeyIdMap object.
- Returns:
VideoTagCollection object
- Return type:
- Usage Example:
import supervisely as sly from supervisely.video_annotation.video_tag_collection import VideoTagCollection tags_json = [ {"name": "car_tag", "value": "acura"}, {"name": "bus_tag", "value": "volvo"}, ] meta_car = sly.TagMeta('car_tag', sly.TagValueType.ANY_STRING) meta_bus = sly.TagMeta('bus_tag', sly.TagValueType.ANY_STRING) meta_collection = sly.TagMetaCollection([meta_car, meta_bus]) tags = VideoTagCollection.from_json(tags_json, meta_collection)
- add(item)¶
Add given item to collection.
- Parameters:
- item¶
ObjClassCollection, TagMetaCollection orTagCollectionobject.
- Returns:
New instance of
KeyIndexedCollection- Return type:
- 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 orTagCollectionobjects.
- Returns:
New instance of
KeyIndexedCollection- Return type:
- 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 orTagCollectionobjects.
- items=
- Returns:
New instance of
KeyIndexedCollection- Return type:
- 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:
KeyIndexedCollectionobject- Return type:
- 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). If there are many values for the same key, the first value will be returned.
- Parameters:
- Returns:
ObjClassCollection, TagMetaCollection orTagCollectionobject- Return type:
- Usage Example:
import supervisely as sly item_cat = sly.TagMeta('cat', sly.TagValueType.NONE) item_turtle = sly.TagMeta('turtle', sly.TagValueType.ANY_STRING) other_cat = sly.ObjClass('cat', sly.Rectangle) collection = sly.collection.key_indexed_collection.MultiKeyIndexedCollection([item_cat, item_turtle, other_cat]) item = collection.get('cat') print(item) # Output: # Name: cat Value type:none Possible values:None Hotkey Applicable toall Applicable classes[]
-
get_all(key, default=
[])¶ Get item from collection with given key(name). If there are many values for the same key,all values will be returned by list.
- Parameters:
- Returns:
List of
ObjClassCollection, TagMetaCollection orTagCollectionobjects or empty list- Return type:
List[
KeyObject] or list- Usage Example:
import supervisely as sly item_cat = sly.TagMeta('cat', sly.TagValueType.NONE) item_turtle = sly.TagMeta('turtle', sly.TagValueType.ANY_STRING) other_cat = sly.ObjClass('cat', sly.Rectangle) collection = sly.collection.key_indexed_collection.MultiKeyIndexedCollection([item_cat, item_turtle, other_cat]) items = collection.get('cat') print(items) # Output: # [<supervisely.annotation.tag_meta.TagMeta object at 0x7f0278662340>, <supervisely.annotation.obj_class.ObjClass object at 0x7f02786623a0>]
-
get_by_name(tag_name, default=
None)¶ Get a list of VideoTag objects by name from the VideoTagCollection.
- Parameters:
- Returns:
List of VideoTag objects with the specified name.
- Return type:
List[
VideoTag]- Usage Example:
import os from dotenv import load_dotenv import supervisely as sly # Load secrets and create API object from .env file (recommended) # Learn more here: https://developer.supervisely.com/getting-started/basics-of-authentication if sly.is_development(): load_dotenv(os.path.expanduser("~/supervisely.env")) api = sly.Api.from_env() videos = api.video.get_list(dataset_id) for info in videos: tag_collection = sly.VideoTagCollection.from_api_response( info.tags, project_meta.tag_metas ) single_tag = tag_collection.get_by_name("tag_name")
-
get_single_by_name(tag_name, default=
None)¶ Get a single Tag object by name from the VideoTagCollection.
- Parameters:
- Returns:
VideoTag object with the specified name.
- Return type:
- Usage Example:
import os from dotenv import load_dotenv import supervisely as sly # Load secrets and create API object from .env file (recommended) # Learn more here: https://developer.supervisely.com/getting-started/basics-of-authentication if sly.is_development(): load_dotenv(os.path.expanduser("~/supervisely.env")) api = sly.Api.from_env() videos = api.video.get_list(dataset_id) for info in videos: tag_collection = sly.VideoTagCollection.from_api_response( info.tags, project_meta.tag_metas ) single_tag = tag_collection.get_single_by_name("tag_name")
- has_key(key)¶
Check if given key(item name exist in collection).
- Parameters:
- Returns:
Is the key in the collection or not
- Return type:
- 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:
MultiKeyIndexedCollectionobject- Return type:
- Usage Example:
import supervisely as sly item_cat = sly.TagMeta('cat', sly.TagValueType.NONE) item_turtle = sly.TagMeta('turtle', sly.TagValueType.ANY_STRING) other_cat = sly.ObjClass('cat', sly.Rectangle) collection = sly.collection.key_indexed_collection.MultiKeyIndexedCollection([item_cat, item_turtle, other_cat]) # Note, item_cat_2 have same key as item_cat, but another value item_cat_2 = sly.TagMeta('cat', sly.TagValueType.ANY_STRING) item_turtle = sly.TagMeta('turtle', sly.TagValueType.ANY_STRING) items = [item_cat_2, item_turtle] intersect = collection.intersection(items) print(intersect.to_json()) # Output: [ # { # "name": "turtle", # "value_type": "any_string", # "color": "#5B8A0F", # "hotkey": "", # "applicable_type": "all", # "classes": [] # } # ]
- items()¶
Get list of all items in collection.
- Returns:
List of
ObjClassCollection, TagMetaCollection orTagCollectionobjects- 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) other_cat = sly.ObjClass('cat', sly.Rectangle) collection = sly.collection.key_indexed_collection.MultiKeyIndexedCollection([item_cat, item_turtle, other_cat]) print(collection.items()) # Output: # [<supervisely.annotation.tag_meta.TagMeta object at 0x7fdbd28ce340>, # <supervisely.annotation.obj_class.ObjClass object at 0x7fdbd28ce3a0>, # <supervisely.annotation.tag_meta.TagMeta object at 0x7fdbd28ce370>]
- 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 with other MultiKeyIndexedCollection object.
- Parameters:
- other¶
Other multi key indexed collection object.
- Returns:
Merged multi key indexed collection object
- Return type:
- Usage Example:
import supervisely as sly item_cat = sly.TagMeta('cat', sly.TagValueType.NONE) item_turtle = sly.TagMeta('turtle', sly.TagValueType.ANY_STRING) other_cat = sly.ObjClass('cat', sly.Rectangle) collection = sly.collection.key_indexed_collection.MultiKeyIndexedCollection([item_cat, item_turtle, other_cat]) item_dog = sly.TagMeta('dog', sly.TagValueType.NONE) item_turtle = sly.TagMeta('turtle', sly.TagValueType.ANY_STRING) other_collection = sly.collection.key_indexed_collection.MultiKeyIndexedCollection([item_dog, item_turtle]) merge = collection.merge(other_collection) print(merge.to_json()) # Output: [ # { # "name": "cat", # "value_type": "none", # "color": "#198A0F", # "hotkey": "", # "applicable_type": "all", # "classes": [] # }, # { # "title": "cat", # "shape": "rectangle", # "color": "#898A0F", # "geometry_config": {}, # "hotkey": "" # }, # { # "name": "turtle", # "value_type": "any_string", # "color": "#650F8A", # "hotkey": "", # "applicable_type": "all", # "classes": [] # }, # { # "name": "turtle", # "value_type": "any_string", # "color": "#0F8A83", # "hotkey": "", # "applicable_type": "all", # "classes": [] # }, # { # "name": "dog", # "value_type": "none", # "color": "#1A8A0F", # "hotkey": "", # "applicable_type": "all", # "classes": [] # } # ]
- merge_without_duplicates(other)¶
Merge collection with other MultiKeyIndexedCollection object. Duplicates will be ignored.
- Parameters:
- other¶
Other multi key indexed collection object.
- Raises:
ValueError – if item name from given
MultiKeyIndexedCollectionis in collection but items in both are different- Returns:
MultiKeyIndexedCollection object
- Return type:
- Usage Example:
import supervisely as sly item_cat = sly.TagMeta('cat', sly.TagValueType.NONE) item_turtle = sly.TagMeta('turtle', sly.TagValueType.ANY_STRING) other_cat = sly.ObjClass('cat', sly.Rectangle) collection = sly.collection.key_indexed_collection.MultiKeyIndexedCollection([item_cat, item_turtle, other_cat]) item_dog = sly.TagMeta('dog', sly.TagValueType.NONE) item_turtle = sly.TagMeta('turtle', sly.TagValueType.ANY_STRING) other_collection = sly.collection.key_indexed_collection.MultiKeyIndexedCollection([item_dog, item_turtle]) merge = collection.merge_without_duplicates(other_collection) print(merge.to_json()) # Output: [ # { # "name": "dog", # "value_type": "none", # "color": "#8A0F37", # "hotkey": "", # "applicable_type": "all", # "classes": [] # }, # { # "name": "cat", # "value_type": "none", # "color": "#778A0F", # "hotkey": "", # "applicable_type": "all", # "classes": [] # }, # { # "title": "cat", # "shape": "rectangle", # "color": "#8A0F76", # "geometry_config": {}, # "hotkey": "" # }, # { # "name": "turtle", # "value_type": "any_string", # "color": "#850F8A", # "hotkey": "", # "applicable_type": "all", # "classes": [] # } # ]
- remove_items(keys)¶
Remove items from collection by given list of keys. Creates a new instance of KeyIndexedCollection.
- Parameters:
- Returns:
New instance of
KeyIndexedCollection- Return type:
-
to_json(key_id_map=
None)¶ Convert the VideoTagCollection to a list of json dicts. Read more about Supervisely format.
- Parameters:
- key_id_map=
None¶ KeyIdMap object.
- key_id_map=
- Returns:
List of dicts in json format
- Return type:
List[dict]
- Usage Example:
import supervisely as sly from supervisely.video_annotation.video_tag import VideoTag from supervisely.video_annotation.video_tag_collection import VideoTagCollection meta_car = sly.TagMeta('car_tag', sly.TagValueType.ANY_STRING) car_tag = VideoTag(meta_car, value='acura') meta_bus = sly.TagMeta('bus_tag', sly.TagValueType.ANY_STRING) bus_tag = VideoTag(meta_bus, value='volvo') tags = VideoTagCollection([car_tag, bus_tag]) tags_json = tags.to_json() print(tags_json) # Output: [ # { # "name": "car_tag", # "value": "acura", # "key": "378408fcb6854305a38fed7c996f4901" # }, # { # "name": "bus_tag", # "value": "volvo", # "key": "0c63174878204faea67c4025adec1e2a" # } # ]