Please refer to this blog post for a full example of working with the Python SDK.
First install the SDK.
pip install segments-ai --upgrade
Import the segments
package in your python file and and set up a client with an API key. An API key can be created on your user account page.
from segments import SegmentsClientapi_key = "eabdde840de8c8853329c086bc4165591cb3d521"client = SegmentsClient(api_key)
user = "jane"datasets = client.get_datasets(user)for dataset in datasets:print(dataset["name"], dataset["description"])
dataset_identifier = "jane/flowers"dataset = client.get_dataset(dataset_identifier)print(dataset)
dataset_name = "flowers"description = "A dataset containing flowers of all kinds."category = "garden"dataset = client.add_dataset(dataset_name, description, category)print(sample)
dataset_identifier = "jane/flowers"client.delete_dataset(dataset_identifier)
dataset = "jane/flowers"samples = client.get_samples(dataset)for sample in samples:print(sample["name"], sample["uuid"])
sample = client.get_sample(uuid="602a3eec-a61c-4a77-9fcc-3037ce5e9606")print(sample)
dataset = "jane/flowers"name = "violet.jpg"attributes = {"image": {"url": "https://example.com/violet.jpg"}}sample = client.add_sample(dataset, name, attributes)print(sample)
If the image is on your local computer, you should first upload it to a cloud storage service like Amazon S3, Google Cloud Storage, Imgur, or our asset storage service.
If you create a sample with a URL from a public S3 bucket and you see an error on the platform, make sure to properly configure your bucket's CORS settings.
client.delete_sample(uuid="602a3eec-a61c-4a77-9fcc-3037ce5e9606")
sample_uuid = "602a3eec-a61c-4a77-9fcc-3037ce5e9606"labelset = "ground-truth"label = client.get_label(sample_uuid, labelset)print(label)
A label can be added to a sample in relation to a label set, such as the default ground-truth label set, or a newly created label set for model predictions. You can create a new label set by clicking the "Add new label set" link on the Samples tab.
sample_uuid = "602a3eec-a61c-4a77-9fcc-3037ce5e9606"labelset = "ground-truth"attributes = {"segmentation_bitmap": {"url": "https://segmentsai-prod.s3.eu-west-2.amazonaws.com/assets/bert/49f6aa10-8967-4305-985c-cdc1e8f89b93.png"},"annotations": [{"id": 1,"category_id": 2},{"id": 2,"category_id": 3}]}client.add_label(sample_uuid, labelset, attributes)
Thesegmentation_bitmap_url
refers to a 32-bit RGBA png image. The alpha channel is set to 255, and the remaining 24-bit values in the RGB channels correspond toinstance_ids.
The easiest way to transform a segmentation bitmap into this format and upload it is by using the util functionbitmap2file
:
from segments.utils import bitmap2file# segmentation_bitmap is a numpy array of type np.uint32, with values corresponding to instance_idsfile = bitmap2file(segmentation_bitmap)asset = client.upload_asset(file, "label.png")segmentation_bitmap_url = asset["url"]
For a full example of uploading model-generated labels to Segments.ai, please refer to this blogpost.
sample_uuid = "602a3eec-a61c-4a77-9fcc-3037ce5e9606"labelset = "ground-truth"client.delete_label(sample_uuid, labelset)
Example with an image:
filename = "/home/jane/flowers/violet.jpg"with open(filename, "rb") as f:asset = client.upload_asset(f, filename="violet.jpg")image_url = asset["url"]