1. Working with Metadata

import dtlpy as dl
# Get project and dataset
project = dl.projects.get(project_name='project_name')
dataset = project.datasets.get(dataset_name='dataset_name')

1.1. User Metadata

As a powerful tool to manage data based on your categories and information, you can add any keys and values to both the item’s and annotations’ user-metadata sections using the Dataloop SDK. Then, you can use your user-metadata for data filtering, sorting, etc.

Note
When adding metadata to the same item, the new metadata overwrites existing metadata. To avoid overwriting existing metadata, use the "list" data type and add to the list the new metadata.

1.1.1. Metadata Data Types

Metadata is a dictionary attribute used with items, annotations, and other entities of the Dataloop system (task, recipe, and more). As such, it can be used with string, number, boolean, list or null types.

1.1.2. String

item.metadata['user']['MyKey'] = 'MyValue'
annotation.metadata['user']['MyKey'] = 'MyValue'

1.1.3. Number

item.metadata['user']['MyKey'] = 3
annotation.metadata['user']['MyKey'] = 3

1.1.4. Boolean

item.metadata['user']['MyKey'] = True
annotation.metadata['user']['MyKey'] = True

1.1.5. Null – add metadata with no information

item.metadata['user']['MyKey'] = None
annotation.metadata['user']['MyKey'] = None

1.1.6. List

# add metadata of a list (can contain elements of different types).
item.metadata['user']['MyKey'] = ["A", 2, False]
annotation.metadata['user']['MyKey'] = ["A", 2, False]

1.1.7. Add new metadata to a list without losing existing data

item.metadata['user']['MyKey'].append(3)
item = item.update()
annotation.metadata['user']['MyKey'].append(3)
annotation = annotation.update()

1.1.8. Add metadata to an item’s user metadata

# upload and claim item
item = dataset.items.upload(local_path=r'C:/home/project/images/item.mimetype')
# or get item
item = dataset.items.get(item_id='write-your-id-number')
# modify metadata
item.metadata['user'] = dict()
item.metadata['user']['MyKey'] = 'MyValue'
# update and reclaim item
item = item.update()

1.1.9. Modify an existing user metadata field

# upload and claim item
item = dataset.items.upload(local_path=r'C:/home/project/images/item.mimetype')
# or get item
item = dataset.items.get(item_id='write-your-id-number')
# modify metadata
if 'user' not in item.metadata:
    item.metadata['user'] = dict()
item.metadata['user']['MyKey'] = 'MyValue'
# update and reclaim item
item = item.update()

1.1.10. Add metadata to annotations’ user metadata

# Get annotation
annotation = dl.annotations.get(annotation_id='my-annotation-id')
# modify metadata
annotation.metadata['user'] = dict()
item.metadata['user']['red'] = True
# update and reclaim annotation
annotation = annotation.update()

1.1.11. Filter items by user metadata

1.1.11.1. 1. Get your dataset

project = dl.projects.get(project_name='project_name')
dataset = project.datasets.get(dataset_name='dataset_name')

1.1.11.2. 2. Add metadata to an item

You can also add metadata to filtered items

# upload and claim item
item = dataset.items.upload(local_path=r'C:/home/project/images/item.mimetype')
# or get item
item = dataset.items.get(item_id='write-your-id-number')
# modify metadata
item.metadata['user'] = dict()
item.metadata['user']['MyKey'] = 'MyValue'
# update and reclaim item
item = item.update()

1.1.11.3. 3. Create a filter

filters = dl.Filters()
# set resource - optional - default is item
filters.resource = dl.FiltersResource.ITEM

1.1.11.4. 4. Filter by your written key

filters.add(field='metadata.user.Key', values='Value')

1.1.11.5. 5. Get filtered items

pages = dataset.items.list(filters=filters)
# Go over all item and print the properties
for page in pages:
    for item in page:
        item.print()