Images

Introduction

In this tutorial we will focus on working with images using Supervisely SDK.

You will learn how to:

πŸ“— 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.

Step 3. Open repository directory in Visual Studio Code.

Step 4. Change workspace ID in local.env file by copying the ID from the context menu of the workspace.

Step 5. Start debugging src/main.py.

Import libraries

Init API client

First, we load environment variables with credentials and init API for communicating with Supervisely Instance.

Get variables from environment

In this tutorial, you will need an workspace ID that you can get from environment variables. Learn more here

Create new project and dataset

Create new project.

Source code:

Output:

Create new dataset.

Source code:

Output:

Upload images from local directory to Supervisely

Upload single image.

Source code:

Output:

Upload list of images.

βœ… Supervisely API allows uploading multiple images in a single request. The code sample below sends fewer requests and it leads to a significant speed-up of our original code.

Source code:

Output:

Upload images as NumPy matrix

Single image

Source code:

Output:

Upload list of images

Source code:

Output:

Get information about images

Single image

Get information about image from Supervisely by id.

Source code:

Output:

You can also get information about image from Supervisely by name.

Source code:

Output:

Get all images from dataset.

Get information about image from Supervisely by id.

Source code:

Output:

Download images to local directory

Single image

Download image from Supervisely to local directory by id.

Source code:

Output:

Download list of images to local directory

Download list of images from Supervisely to local directory by ids.

Source code:

Output:

Download images as RGB NumPy matrix

Single image

Download image from Supervisely to local directory by id.

Source code:

Output:

Download list of images as RGB NumPy matrix

Download list of images from Supervisely to local directory by ids.

Source code:

Output:

Get and update image metadata

Get image metadata from server

Source code:

Output:

Update image metadata

Source code:

Output:

Get metadata in Image labeling toolbox

You can also get image metadata in Image labeling toolbox interface

Remove images from Supervisely

Remove one image.

Remove image from Supervisely by id

Source code:

Output:

Remove list of images.

Remove list of images from Supervisely by ids.

Source code:

Output:

Custom image sorting for Image Labeling Toolbox

To enhance the usability of working with images in the Image Labeling Toolbox, a custom sorting parameter can be added for project images. This parameter will define the order of images in the interface list.

  1. Sort button

  2. Sorting parameter

Upload list of images with added custom sorting parameter

The best and fastest way to accomplish this is to use context manager ImageApi.add_custom_sort This context manager allows you to set the sort_by attribute of ImageApi object for the duration of the context, then delete it. If nested functions support this functionality, each image they process will automatically receive a custom sorting parameter based on the available meta object. Currently, almost all image uploading methods support this functionality. Methods that support it have a corresponding description in the docstring.

Source code:

Output:

Upload whole images project in Supervisely format with added custom sorting parameter

It is also recommended to use a context manager for uploading the entire project. The only difference from the previous point is that there is no need to pass meta in dictionaries. It can be stored either in image info files or in meta files within the project structure. To learn more about the project structure and its files, see the Project Structure section.

Source code:

Output:

Add custom sorting parameter to meta object

Here are several ways to modify meta for images

1. Add parameter to meta dict and update meta on server

Source code:

Output:

2. Set directly on server

Source code:

Output:

3. Set directly on server in bulk

Same as the previous case, but for more than one image

Source code:

Output:

Last updated

Was this helpful?