Image and object tags

How to tag image and object using Supervisely SDK

Introduction

In this tutorial we will be focusing on working with tags using Supervisely SDK. We'll go through complete cycle from creating TagMeta in project to assigning Tags to images and objects directly.

You will learn:

  1. how to create tags for different tasks and scenarios with various parameters.

  2. how to create tags (sly.TagMeta) in project

  3. how to assign tags (sly.Tag) to images and objects

๐Ÿ“— Everything you need to reproduce this tutorial is on GitHub: source code and demo data.

How to debug this tutorial

Step 1. Prepare ~/supervisely.env file with credentials. Learn more here.

Step 2. Clone repository with source code and demo data and create Virtual Environment.

git clone https://github.com/supervisely-ecosystem/tutorial-working-with-tags
cd tutorial-working-with-tags
./create_venv.sh

Step 3. Open repository directory in Visual Studio Code.

code -r .

Step 4. Get Lemons (Annotated) project from ecosystem. Lemons (Annotated) is an example project with annotated lemons and kiwi fruits, with 6 images in it.

Step 5. change โœ… project ID โœ… in local.env file by copying the ID from the context menu of the project.

Step 6. Start debugging src/main.py

Part 1. Tag Meta

Import libraries

Init API client

Init API for communicating with Supervisely Instance. First, we load environment variables with credentials and project ID:

Create Tag Meta

TagMeta object contains general information about Tag. In order to create Tag itself you must create TagMeta object (information about tags that we will create and assign to images or objects) with parameters such as:

  • name (required) - name of the Tag.

  • value_type (required)- restricts Tag to have a certain value type. Available value types:

    • NONE = "none"

    • ANY_STRING = "any_string"

    • ANY_NUMBER = "any_number"

    • ONEOF_STRING = "oneof_string"

  • possible_values (required if value type is "oneof_string") - list of possible Tag values.

  • color (optional) - color of the Tag, must be an RGB value, if not specified, random color will be generated.

  • applicable_to (optional) - defines if Tag can be assigned to only images, to only objects or both. By default tag can be assigned to both images and objects.

  • applicable_classes (optional) - defines applicability of Tag only to certain classes. List of strings (class names).

Let's start with creating a simple TagMeta for showcasing, it can be applied to both images and objects, and also to any class. We won't use it for our project later. Tags with value type "none" can be used as "train" and "val" tags for example.

Let's make this TagMeta applicable only to images. We can recreate TagMeta with additional parameters. Most supervisely classes are immutable, so you have to assign or reassign them to variables.

Now let's create a few TagMetas with different value types that we will apply to our project.

We can start with creating fruit name TagMeta with "any_string" value type. This Tag can be assigned only to objects of classes "lemon" and "kiwi".

Create fruit size TagMeta with "oneof_string" value type. This Tag can be assigned only to objects of any classes and has possible values.

Now we create a TagMeta with "any_string" value type to enter the origin of the fruit into it. This Tag can be assigned only to objects of classes "lemon" and "kiwi".

And one more TagMeta with "any_number" value type for counting total fruits on image. This Tag is applicable only to images.

Bring all created TagMetas together in a list

Part 2. Add TagMetas to project

Get project meta from server

Check that created Tag Metas don't already exist in project meta, and if not, add them to project meta.

Update project meta on Supervisely instance after adding Tag Metas to project meta.

Part 3. Create Tags and update annotation on server

Retrieve images with object tags of interest

Sometimes, you may need to filter your images to retrieve only those that feature specific tags or meet certain criteria.

The code snippet below demonstrates how to get images featuring objects with multiple tags attached:

Advanced API

Advanced API allows user to manage tags directly on images or objects without downloading annotation data from server.

Get project meta again after updating it with new tags.

Create TagCollection from image Tags without downloading annotation

Get image id from image info (see part 3)

Get image tags

Create TagCollection from image tags

Add Tag directly to image

Get image tag ID from project meta

Add Tag to image using Tag supervisely ID from project meta

Add Tags to objects directly

Add Tag to set of images

With api.image.add_tag_batch() method you can add a tag to a list of images without need to update annotation of each image one by one.

Last updated

Was this helpful?