Python SDK quickstart

On the web platform you can create datasets, upload samples, create releases and download labels. All of these - and more - can also be done programmatically with the Python SDK.

This tutorial walks you through the most common Python SDK functions. The complete list of functions is documented in detail in the Python SDK reference.

First install the Python SDK using pip:

pip install --upgrade segments-ai

Import the necessary packages, and initialize the Segments client using your API key:

from segments import SegmentsClient
import json

# You can find your api key at
api_key = "YOUR_API_KEY_HERE"

client = SegmentsClient(api_key)

Create a new dataset

Let's create a new image segmentation dataset programmatically using client.add_dataset(). Note that this dataset will be created under the user account corresponding to the API key.

The format of the task_attributes field is documented here.

name = "pets"
description = "A dataset with images of cats and dogs."
task_type = "segmentation-bitmap"

task_attributes = {
 "format_version": "0.1",
 "categories": [
   "name": "cat",
   "id": 1
   "name": "dog",
   "id": 2
   "name": "other",
   "id": 3

dataset = client.add_dataset(name, description, task_type, task_attributes)

Add samples to a dataset

Now let's upload some images to this dataset using client.add_sample().

dataset_identifier = 'jane/pets' # a dataset is always referred to as user/dataset.

images = [
        'name': 'Bombay_220.jpg', 
        'url': ''
        'name': 'shiba_inu_178.jpg', 
        'url': ''
        'name': 'havanese_196.jpg', 
        'url': ''

for image in images:    
    name = image['name']
    attributes = {
        'image': {
          'url': image['url'] 
    sample = client.add_sample(dataset_identifier, name, attributes)

If the image file is on your local computer, you should first upload it to our asset storage service (using upload_asset()) or to another cloud storage service.

We can verify that the dataset now contains 3 images using client.get_samples().

dataset_identifier = 'jane/pets'

samples = client.get_samples(dataset_identifier)

Now switch to the web platform and label the three images you just uploaded by pressing the "Start labeling" button.

Get the label of a sample

Once you've labeled some samples, you can programmatically retrieve their labels using client.get_label().

dataset_identifier = 'jane/pets'
sample = client.get_samples(dataset_identifier)[0]

label = client.get_label(sample.uuid, labelset='ground-truth')

Optional: visualize the instance and semantic labels

When working with image segmentation datasets, you'll probably want to visualize the image and label at this point. The segments.utils module offers some helper functions for that:

import numpy as np
import matplotlib.pyplot as plt
from segments.utils import load_image_from_url, load_label_bitmap_from_url, get_semantic_bitmap

# Load the labels as numpy arrays
image = load_image_from_url(sample.attributes.image.url)
instance_bitmap = load_label_bitmap_from_url(label.attributes.segmentation_bitmap.url)
semantic_bitmap = get_semantic_bitmap(instance_bitmap, label.attributes.annotations)

# Visualize

plt.title(f'Instance bitmap. Values represent instance ids: {np.unique(instance_bitmap)}')

plt.title(f'Semantic bitmap. Values represent category ids: {np.unique(semantic_bitmap)}')

What's next?

The Python SDK offers many more functions besides the ones that were shown here. Have a look at the reference for the full list.

The Python SDK can also be used to upload labels into This is particularly useful for setting up model-assisted labeling workflows, where you verify and correct model predictions instead of labeling from scratch.

Last updated