Dataset

class Dataset(directory, mode=None, parents=None, dataset_id=None, api=None)[source]

Bases: KeyObject

A dataset directory inside a local Supervisely project.

Provides access to items (e.g. images), their annotations and related metadata on disk.

Dataset is where your labeled and unlabeled images and other data files live. Dataset object is immutable.

Parameters:
directory : str

Path to dataset directory.

mode=None

Determines working mode for the given dataset.

parents : List[str]

List of parent directories, e.g. [“ds1”, “ds2”, “ds3”].

dataset_id : Optional[int]

Dataset ID if the Dataset is opened in API mode. If dataset_id is specified then api must be specified as well.

api=None

API object if the Dataset is opened in API mode.

Usage Example:
import os
from dotenv import load_dotenv

import supervisely as sly

# Load secrets and create API object from .env file (recommended)
# Learn more here: https://developer.supervisely.com/getting-started/basics-of-authentication
if sly.is_development():
    load_dotenv(os.path.expanduser("~/supervisely.env"))

api = sly.Api.from_env()

dataset_path = "/home/admin/work/supervisely/projects/lemons_annotated/ds1"

# Open Dataset locally in read mode
ds = sly.Dataset(dataset_path, sly.OpenMode.READ)

# Open Dataset with API
ds = sly.Dataset(dataset_path, dataset_id=1, api=api)

Methods

add_item_file

Adds given item file to dataset items directory, and adds given annotation to dataset annotations directory.

add_item_file_async

Adds given item file to dataset items directory, and adds given annotation to dataset annotations directory.

add_item_np

Adds given numpy matrix as an image to dataset items directory, and adds given annotation to dataset ann directory.

add_item_raw_bytes

Adds given binary object as an image to dataset items directory, and adds given annotation to dataset ann directory.

add_item_raw_bytes_async

Adds given binary object as an image to dataset items directory, and adds given annotation to dataset ann directory.

datasets_dir

Returns the name of the datasets directory.

delete_item

Delete image, image info and annotation from Dataset.

generate_item_path

Generates full path to the given item.

get_ann

Read annotation of item from json.

get_ann_path

Path to the given annotation json file.

get_blob_img_bytes

Get image bytes from blob file.

get_blob_img_np

Get image as numpy array from blob file.

get_classes_stats

Get classes stats for the project.

get_image_info

Information for Item with given name.

get_img_info_path

Get path to the image info json file without checking if the file exists.

get_img_path

Path to the given image.

get_item_info

Information for Item with given name.

get_item_info_path

Get path to the item info json file without checking if the file exists.

get_item_meta_path

Get path to the item info json file without checking if the file exists.

get_item_path

Path to the given item.

get_item_paths

Generates ItemPaths object with paths to item and annotation directories for item with given name.

get_items_names

List of dataset item names.

get_seg_path

Get path to the png segmentation mask file without checking if the file exists.

get_url

Get URL to dataset items list in Supervisely.

ignorable_dirs

Returns the list of ignorable directories.

item_exists

Checks if given item name belongs to the dataset.

items

This method is used to iterate over dataset items, receiving item name, path to image and path to annotation json file.

key

Returns the key (name) of the dataset.

set_ann

Replaces given annotation for given item name to dataset annotations directory in json format.

set_ann_async

Replaces given annotation for given item name to dataset annotations directory in json format.

set_ann_dict

Replaces given annotation json for given item name to dataset annotations directory in json format.

set_ann_dict_async

Replaces given annotation json for given item name to dataset annotations directory in json format.

set_ann_file

Replaces given annotation json file for given item name to dataset annotations directory in json format.

set_ann_file_async

Replaces given annotation json file for given item name to dataset annotations directory in json format.

to_coco

Convert Supervisely dataset to COCO format.

to_pascal_voc

Convert Supervisely dataset to Pascal VOC format.

to_yolo

Convert Supervisely dataset to YOLO format.

Attributes

ann_dir

Path to the dataset annotations directory.

ann_dir_name

blob_dir_name

blob_offsets

List of paths to the dataset blob offset files.

datasets_dir_name

directory

Path to the dataset directory.

image_infos

If the dataset is opened from the API, returns the list of ImageInfo objects.

img_dir

Path to the dataset images directory.

img_info_dir

Path to the dataset image info directory.

item_dir

Path to the dataset items directory.

item_dir_name

item_info_dir

Path to the dataset item info directory.

item_info_dir_name

meta_dir

Path to the dataset segmentation masks directory.

meta_dir_name

name

Full Dataset name, which includes it's parents, e.g. ds1/ds2/ds3.

path

Returns a relative local path to the dataset.

project_dir

Path to the project containing the dataset.

seg_dir

Path to the dataset segmentation masks directory.

seg_dir_name

short_name

Short dataset name, which does not include it's parents.

annotation_class

alias of Annotation

item_info_class

alias of ImageInfo

classmethod datasets_dir()[source]

Returns the name of the datasets directory.

classmethod ignorable_dirs()[source]

Returns the list of ignorable directories.

static get_url(project_id, dataset_id)[source]

Get URL to dataset items list in Supervisely.

Parameters:
project_id : int

Project ID in Supervisely.

dataset_id : int

Dataset ID in Supervisely.

Returns:

URL to dataset items list.

Return type:

str

Usage Example:
from supervisely import Dataset

project_id = 10093
dataset_id = 45330
ds_items_link = Dataset.get_url(project_id, dataset_id)

print(ds_items_link)
# Output: "/projects/10093/datasets/45330"
add_item_file(item_name, item_path, ann=None, _validate_item=True, _use_hardlink=False, item_info=None, img_info=None)[source]

Adds given item file to dataset items directory, and adds given annotation to dataset annotations directory. if ann is None, creates empty annotation file.

Parameters:
item_name : str

Item name.

item_path : str

Path to the item.

ann=None

Annotation object or path to annotation json file.

_validate_item : bool, optional

Checks input files format.

If True creates a hardlink pointing to src named dst, otherwise don’t.

item_info=None

ImageInfo object or ImageInfo object converted to dict or path to item info json file for copying to dataset item info directory.

img_info=None

Deprecated version of item_info parameter. Can be removed in future versions.

Returns:

None

Return type:

NoneType

Raises:

RuntimeError – if item_name already exists in dataset or item name has unsupported extension.

Usage Example:
import supervisely as sly

dataset_path = "/home/admin/work/supervisely/projects/lemons_annotated/ds1"
ds = sly.Dataset(dataset_path, sly.OpenMode.READ)

ann = "/home/admin/work/supervisely/projects/lemons_annotated/ds1/ann/IMG_8888.jpeg.json"
ds.add_item_file("IMG_8888.jpeg", "/home/admin/work/supervisely/projects/lemons_annotated/ds1/img/IMG_8888.jpeg", ann=ann)
print(ds.item_exists("IMG_8888.jpeg"))
# Output: True
async add_item_file_async(item_name, item_path, ann=None, _validate_item=True, _use_hardlink=False, item_info=None, img_info=None)[source]

Adds given item file to dataset items directory, and adds given annotation to dataset annotations directory. If ann is None, creates empty annotation file.

Parameters:
item_name : str

Item name.

item_path : str

Path to the item.

ann=None

Annotation object or path to annotation json file.

_validate_item : bool, optional

Checks input files format.

If True creates a hardlink pointing to src named dst, otherwise don’t.

item_info=None

ImageInfo object or ImageInfo object converted to dict or path to item info json file for copying to dataset item info directory.

img_info=None

Deprecated version of item_info parameter. Can be removed in future versions.

Returns:

None

Return type:

NoneType

Raises:

RuntimeError – if item_name already exists in dataset or item name has unsupported extension.

Usage Example:
import supervisely as sly

dataset_path = "/home/admin/work/supervisely/projects/lemons_annotated/ds1"
ds = sly.Dataset(dataset_path, sly.OpenMode.READ)

ann = "/home/admin/work/supervisely/projects/lemons_annotated/ds1/ann/IMG_8888.jpeg.json"
loop = sly.utils.get_or_create_event_loop()
loop.run_until_complete(
    ds.add_item_file_async("IMG_8888.jpeg", "/home/admin/work/supervisely/projects/lemons_annotated/ds1/img/IMG_8888.jpeg", ann=ann)
)
print(ds.item_exists("IMG_8888.jpeg"))
# Output: True
add_item_np(item_name, img, ann=None, img_info=None)[source]

Adds given numpy matrix as an image to dataset items directory, and adds given annotation to dataset ann directory. if ann is None, creates empty annotation file.

Parameters:
item_name : str

Item name.

img : np.ndarray

numpy Image matrix in RGB format.

ann=None

Annotation object or path to annotation json file.

img_info=None

ImageInfo object or ImageInfo object converted to dict or path to item info json file for copying to dataset item info directory.

Returns:

None

Return type:

NoneType

Raises:

RuntimeError – if item_name already exists in dataset or item name has unsupported extension

Usage Example:
import supervisely as sly

dataset_path = "/home/admin/work/supervisely/projects/lemons_annotated/ds1"
ds = sly.Dataset(dataset_path, sly.OpenMode.READ)

img_path = "/home/admin/Pictures/Clouds.jpeg"
img_np = sly.image.read(img_path)
ds.add_item_np("IMG_050.jpeg", img_np)
print(ds.item_exists("IMG_050.jpeg"))
# Output: True
add_item_raw_bytes(item_name, item_raw_bytes, ann=None, img_info=None)[source]

Adds given binary object as an image to dataset items directory, and adds given annotation to dataset ann directory. if ann is None, creates empty annotation file.

Parameters:
item_name : str

Item name.

item_raw_bytes : bytes

Binary object.

ann=None

Annotation object or path to annotation json file.

img_info=None

ImageInfo object or ImageInfo object converted to dict or path to item info json file for copying to dataset item info directory.

Returns:

None

Return type:

NoneType

Raises:

RuntimeError – if item_name already exists in dataset or item name has unsupported extension

Usage Example:
import supervisely as sly

dataset_path = "/home/admin/work/supervisely/projects/lemons_annotated/ds1"
ds = sly.Dataset(dataset_path, sly.OpenMode.READ)

img_path = "/home/admin/Pictures/Clouds.jpeg"
img_np = sly.image.read(img_path)
img_bytes = sly.image.write_bytes(img_np, "jpeg")
ds.add_item_raw_bytes("IMG_050.jpeg", img_bytes)
print(ds.item_exists("IMG_050.jpeg"))
# Output: True
async add_item_raw_bytes_async(item_name, item_raw_bytes, ann=None, img_info=None)[source]

Adds given binary object as an image to dataset items directory, and adds given annotation to dataset ann directory. If ann is None, creates empty annotation file.

Parameters:
item_name : str

Item name.

item_raw_bytes : bytes

Binary object.

ann=None

Annotation object or path to annotation json file.

img_info=None

ImageInfo object or ImageInfo object converted to dict or path to item info json file for copying to dataset item info directory.

Returns:

None

Return type:

NoneType

Raises:

RuntimeError – if item_name already exists in dataset or item name has unsupported extension

Usage Example:
import supervisely as sly
from supervisely._utils import run_coroutine

dataset_path = "/home/admin/work/supervisely/projects/lemons_annotated/ds1"
ds = sly.Dataset(dataset_path, sly.OpenMode.READ)

img_path = "/home/admin/Pictures/Clouds.jpeg"
img_np = sly.image.read(img_path)
img_bytes = sly.image.write_bytes(img_np, "jpeg")
coroutine = ds.add_item_raw_bytes_async("IMG_050.jpeg", img_bytes)
run_coroutine(coroutine)

print(ds.item_exists("IMG_050.jpeg"))
# Output: True
delete_item(item_name)[source]

Delete image, image info and annotation from Dataset.

Parameters:
item_name : str

Item name.

Returns:

True if item was successfully deleted, False if item wasn’t found in dataset.

Return type:

bool

Usage Example:
import supervisely as sly

dataset_path = "/home/admin/work/supervisely/projects/lemons_annotated/ds1"
ds = sly.Dataset(dataset_path, sly.OpenMode.READ)

print(dataset.delete_item("IMG_0748"))
# Output: False

print(dataset.delete_item("IMG_0748.jpeg"))
# Output: True
generate_item_path(item_name)[source]

Generates full path to the given item.

Parameters:
item_name : str

Item name.

Returns:

Full path to the given item

Return type:

str

Usage Example:
import supervisely as sly

dataset_path = "/home/admin/work/supervisely/projects/lemons_annotated/ds1"
ds = sly.Dataset(dataset_path, sly.OpenMode.READ)

print(ds.generate_item_path("IMG_0748.jpeg"))
# Output: '/home/admin/work/supervisely/projects/lemons_annotated/ds1/img/IMG_0748.jpeg'
get_ann(item_name, project_meta)[source]

Read annotation of item from json.

Parameters:
item_name : str

Item name.

project_meta

Project meta.

Returns:

Annotation object.

Return type:

Annotation

Raises:

RuntimeError – if item not found in the project

Usage Example:
import supervisely as sly

project_path = "/home/admin/work/supervisely/projects/lemons_annotated"
project = sly.Project(project_path, sly.OpenMode.READ)

ds = project.datasets.get('ds1')

annotation = ds.get_ann("IMG_0748", project.meta)
# Output: RuntimeError: Item IMG_0748 not found in the project.

annotation = ds.get_ann("IMG_0748.jpeg", project.meta)
print(annotation.to_json())
# Output: {
#     "description": "",
#     "size": {
#         "height": 500,
#         "width": 700
#     },
#     "tags": [],
#     "objects": [],
#     "customBigData": {}
# }
get_ann_path(item_name)[source]

Path to the given annotation json file.

Parameters:
item_name : str

Item name.

Returns:

Path to the given annotation json file.

Return type:

str

Raises:

RuntimeError – if item not found in the project

Usage Example:
import supervisely as sly

dataset_path = "/home/admin/work/supervisely/projects/lemons_annotated/ds1"
ds = sly.Dataset(dataset_path, sly.OpenMode.READ)

print(ds.get_ann_path("IMG_0748"))
# Output: RuntimeError: Item IMG_0748 not found in the project.

print(ds.get_ann_path("IMG_0748.jpeg"))
# Output: '/home/admin/work/supervisely/projects/lemons_annotated/ds1/ann/IMG_0748.jpeg.json'
get_blob_img_bytes(image_name)[source]

Get image bytes from blob file.

Parameters:
image_name : str

Image name with extension.

Returns:

Bytes of the image.

Return type:

bytes

Usage Example:
import supervisely as sly

dataset_path = "/path/to/project/lemons_annotated/ds1"
dataset = sly.Dataset(dataset_path, sly.OpenMode.READ)
image_name = "IMG_0748.jpeg"

img_bytes = dataset.get_blob_img_bytes(image_name)
get_blob_img_np(image_name)[source]

Get image as numpy array from blob file.

Parameters:
image_name : str

Image name with extension.

Returns:

Numpy array of the image.

Return type:

numpy.ndarray

Usage Example:
import supervisely as sly

dataset_path = "/path/to/project/lemons_annotated/ds1"
dataset = sly.Dataset(dataset_path, sly.OpenMode.READ)
image_name = "IMG_0748.jpeg"
img_np = dataset.get_blob_img_np(image_name)
get_classes_stats(project_meta=None, return_objects_count=True, return_figures_count=True, return_items_count=True)[source]

Get classes stats for the project.

Parameters:
project_meta=None

Project meta.

return_objects_count : bool

Return objects count.

return_figures_count : bool

Return figures count.

return_items_count : bool

Return items count.

Returns:

Dictionary with classes stats.

get_image_info(item_name)[source]

Information for Item with given name.

Parameters:
item_name : str

Item name.

Returns:

ImageInfo object.

Return type:

ImageInfo

Usage Example:
import supervisely as sly

dataset_path = "/home/admin/work/supervisely/projects/lemons_annotated/ds0"
ds = sly.Dataset(dataset_path, sly.OpenMode.READ)

print(ds.get_image_info("IMG_0748.jpeg"))
# Output:
# ImageInfo(
#     id=770915,
#     name='IMG_0748.jpeg',
#     link=None,
#     hash='ZdpMD+ZMJx0R8BgsCzJcqM7qP4M8f1AEtoYc87xZmyQ=',
#     mime='image/jpeg',
#     ext='jpeg',
#     size=148388,
#     width=1067,
#     height=800,
#     labels_count=4,
#     dataset_id=2532,
#     created_at='2021-03-02T10:04:33.973Z',
#     updated_at='2021-03-02T10:04:33.973Z',
#     meta={},
#     path_original='/h5un6l2bnaz1vj8a9qgms4-public/images/original/7/h/Vo/...jpeg',
#     full_storage_url='http://app.supervisely.com/h5un6l2bnaz1vj8a9qgms4-public/images/original/7/h/Vo/...jpeg'),
#     tags=[]
# )
get_img_info_path(img_name)[source]

Get path to the image info json file without checking if the file exists. Method is alias of get_item_info_path(item_name).

Parameters:
item_name : str

Image name.

Returns:

Path to the given image info json file.

Return type:

str

Raises:

RuntimeError – if image not found in the project.

Usage Example:
import supervisely as sly

dataset_path = "/home/admin/work/supervisely/projects/lemons_annotated/ds1"
ds = sly.Dataset(dataset_path, sly.OpenMode.READ)

print(ds.get_img_info_path("IMG_0748"))
# Output: RuntimeError: Item IMG_0748 not found in the project.

print(ds.get_img_info_path("IMG_0748.jpeg"))
# Output: '/home/admin/work/supervisely/projects/lemons_annotated/ds1/img_info/IMG_0748.jpeg.json'
get_img_path(item_name)[source]

Path to the given image. Method is alias of get_item_path(item_name).

Parameters:
item_name : str

Image name.

Returns:

Path to the given image.

Return type:

str

Raises:

RuntimeError – if item not found in the project.

Usage Example:
import supervisely as sly

dataset_path = "/home/admin/work/supervisely/projects/lemons_annotated/ds1"
ds = sly.Dataset(dataset_path, sly.OpenMode.READ)

print(ds.get_img_path("IMG_0748"))
# Output: RuntimeError: Item IMG_0748 not found in the project.

print(ds.get_img_path("IMG_0748.jpeg"))
# Output: '/home/admin/work/supervisely/projects/lemons_annotated/ds1/ann/IMG_0748.jpeg.json'
get_item_info(item_name)[source]

Information for Item with given name.

Parameters:
item_name : str

Item name.

Returns:

ImageInfo object.

Return type:

ImageInfo

Usage Example:
import supervisely as sly

dataset_path = "/home/admin/work/supervisely/projects/lemons_annotated/ds0"
ds = sly.Dataset(dataset_path, sly.OpenMode.READ)

print(ds.get_item_info("IMG_0748.jpeg"))
# Output:
# ImageInfo(
#     id=770915,
#     name='IMG_0748.jpeg',
#     link=None,
#     hash='ZdpMD+ZMJx0R8BgsCzJcqM7qP4M8f1AEtoYc87xZmyQ=',
#     mime='image/jpeg',
#     ext='jpeg',
#     size=148388,
#     width=1067,
#     height=800,
#     labels_count=4,
#     dataset_id=2532,
#     created_at='2021-03-02T10:04:33.973Z',
#     updated_at='2021-03-02T10:04:33.973Z',
#     meta={},
#     path_original='/h5un6l2bnaz1vj8a9qgms4-public/images/original/7/h/Vo/...jpeg',
#     full_storage_url='http://app.supervisely.com/h5un6l2bnaz1vj8a9qgms4-public/images/original/7/h/Vo/...jpeg'),
#     tags=[]
# )
get_item_info_path(item_name)[source]

Get path to the item info json file without checking if the file exists.

Parameters:
item_name : str

Item name.

Returns:

Path to the given item info json file.

Return type:

str

Raises:

RuntimeError – if item not found in the project.

Usage Example:
import supervisely as sly

dataset_path = "/home/admin/work/supervisely/projects/lemons_annotated/ds1"
ds = sly.Dataset(dataset_path, sly.OpenMode.READ)

print(ds.get_item_info_path("IMG_0748"))
# Output: RuntimeError: Item IMG_0748 not found in the project.

print(ds.get_item_info_path("IMG_0748.jpeg"))
# Output: '/home/admin/work/supervisely/projects/lemons_annotated/ds1/img_info/IMG_0748.jpeg.json'
get_item_meta_path(item_name)[source]

Get path to the item info json file without checking if the file exists.

Parameters:
item_name : str

Item name.

Returns:

Path to the given item info json file.

Return type:

str

Raises:

RuntimeError – if item not found in the project.

Usage Example:
import supervisely as sly

dataset_path = "/home/admin/work/supervisely/projects/lemons_annotated/ds1"
ds = sly.Dataset(dataset_path, sly.OpenMode.READ)

print(ds.get_item_info_path("IMG_0748"))
# Output: RuntimeError: Item IMG_0748 not found in the project.

print(ds.get_item_info_path("IMG_0748.jpeg"))
# Output: '/home/admin/work/supervisely/projects/lemons_annotated/ds1/img_info/IMG_0748.jpeg.json'
get_item_path(item_name)[source]

Path to the given item.

Parameters:
item_name : str

Item name.

Returns:

Path to the given item.

Return type:

str

Raises:

RuntimeError – if item not found in the project

Usage Example:
import supervisely as sly

dataset_path = "/home/admin/work/supervisely/projects/lemons_annotated/ds1"
ds = sly.Dataset(dataset_path, sly.OpenMode.READ)

print(ds.get_item_path("IMG_0748"))
# Output: RuntimeError: Item IMG_0748 not found in the project.

print(ds.get_item_path("IMG_0748.jpeg"))
# Output: '/home/admin/work/supervisely/projects/lemons_annotated/ds1/img/IMG_0748.jpeg'
get_item_paths(item_name)[source]

Generates ItemPaths object with paths to item and annotation directories for item with given name.

Parameters:
item_name : str

Item name.

Returns:

ItemPaths object

Return type:

ItemPaths

Usage Example:
import supervisely as sly

dataset_path = "/home/admin/work/supervisely/projects/lemons_annotated/ds1"
ds = sly.Dataset(dataset_path, sly.OpenMode.READ)

img_path, ann_path = dataset.get_item_paths("IMG_0748.jpeg")
print("img_path:", img_path)
print("ann_path:", ann_path)
# Output:
# img_path: /home/admin/work/supervisely/projects/lemons_annotated/ds1/img/IMG_0748.jpeg
# ann_path: /home/admin/work/supervisely/projects/lemons_annotated/ds1/ann/IMG_0748.jpeg.json
get_items_names()[source]

List of dataset item names.

Returns:

List of item names.

Return type:

List[str]

Usage Example:
import supervisely as sly

dataset_path = "/home/admin/work/supervisely/projects/lemons_annotated/ds1"
ds = sly.Dataset(dataset_path, sly.OpenMode.READ)

print(ds.get_item_names())
# Output: ['IMG_0002.jpg', 'IMG_0005.jpg', 'IMG_0008.jpg', ...]
get_seg_path(item_name)[source]

Get path to the png segmentation mask file without checking if the file exists. Use to_segmentation_task() to create segmentation masks from annotations in your project.

Parameters:
item_name : str

Item name.

Returns:

Path to the given png mask file.

Return type:

str

Raises:

RuntimeError – if item not found in the project.

Usage Example:
import supervisely as sly

dataset_path = "/home/admin/work/supervisely/projects/lemons_annotated/ds1"
ds = sly.Dataset(dataset_path, sly.OpenMode.READ)

print(ds.get_seg_path("IMG_0748"))
# Output: RuntimeError: Item IMG_0748 not found in the project.

print(ds.get_seg_path("IMG_0748.jpeg"))
# Output: '/home/admin/work/supervisely/projects/lemons_annotated/ds1/seg/IMG_0748.jpeg.png'
item_exists(item_name)[source]

Checks if given item name belongs to the dataset.

Parameters:
item_name : str

Item name.

Returns:

True if item exist, otherwise False.

Return type:

bool

Usage Example:
import supervisely as sly

dataset_path = "/home/admin/work/supervisely/projects/lemons_annotated/ds1"
ds = sly.Dataset(dataset_path, sly.OpenMode.READ)

ds.item_exists("IMG_0748")      # False
ds.item_exists("IMG_0748.jpeg") # True
items()[source]

This method is used to iterate over dataset items, receiving item name, path to image and path to annotation json file. It is useful when you need to iterate over dataset items and get paths to images and annotations.

Returns:

Generator object, that yields tuple of item name, path to image and path to annotation json file.

Return type:

Generator[Tuple[str]]

Usage Example:
import supervisely as sly

input = "path/to/local/directory"
# Creating Supervisely project from local directory.
project = sly.Project(input, sly.OpenMode.READ)

for dataset in project.datasets:
    for item_name, image_path, ann_path in dataset.items():
        print(f"Item '{item_name}': image='{image_path}', ann='{ann_path}'")
key()[source]

Returns the key (name) of the dataset.

set_ann(item_name, ann)[source]

Replaces given annotation for given item name to dataset annotations directory in json format.

Parameters:
item_name : str

Item name.

ann

Annotation object.

Returns:

None

Return type:

NoneType

Usage Example:
import supervisely as sly

dataset_path = "/home/admin/work/supervisely/projects/lemons_annotated/ds1"
ds = sly.Dataset(dataset_path, sly.OpenMode.READ)

height, width = 500, 700
new_ann = sly.Annotation((height, width))
ds.set_ann("IMG_0748.jpeg", new_ann)
async set_ann_async(item_name, ann)[source]

Replaces given annotation for given item name to dataset annotations directory in json format.

Parameters:
item_name : str

Item name.

ann

Annotation object.

Returns:

None

Return type:

NoneType

Usage Example:
import supervisely as sly
from supervisely._utils import run_coroutine

dataset_path = "/home/admin/work/supervisely/projects/lemons_annotated/ds1"
ds = sly.Dataset(dataset_path, sly.OpenMode.READ)

height, width = 500, 700
new_ann = sly.Annotation((height, width))

coroutine = ds.set_ann_async("IMG_0748.jpeg", new_ann)
run_coroutine(coroutine)
set_ann_dict(item_name, ann)[source]

Replaces given annotation json for given item name to dataset annotations directory in json format.

Parameters:
item_name : str

Item name.

ann : dict

Annotation as a dict in json format.

Returns:

None

Return type:

NoneType

Raises:

RuntimeError – if ann_path is not str

Usage Example:
import supervisely as sly

dataset_path = "/home/admin/work/supervisely/projects/lemons_annotated/ds1"
ds = sly.Dataset(dataset_path, sly.OpenMode.READ)

new_ann_json = {
    "description":"",
    "size":{"height":500, "width":700},
    "tags":[],
    "objects":[],
    "customBigData":{}
}

ds.set_ann_dict("IMG_8888.jpeg", new_ann_json)
async set_ann_dict_async(item_name, ann)[source]

Replaces given annotation json for given item name to dataset annotations directory in json format.

Parameters:
item_name : str

Item name.

ann : dict

Annotation as a dict in json format.

Returns:

None

Return type:

NoneType

Raises:

RuntimeError – if ann_path is not str

Usage Example:
import supervisely as sly
from supervisely._utils import run_coroutine

dataset_path = "/home/admin/work/supervisely/projects/lemons_annotated/ds1"
ds = sly.Dataset(dataset_path, sly.OpenMode.READ)

new_ann_json = {
    "description":"",
    "size":{"height":500, "width":700},
    "tags":[],
    "objects":[],
    "customBigData":{}
}

coroutine = ds.set_ann_dict_async("IMG_8888.jpeg", new_ann_json)
run_coroutine(coroutine)
set_ann_file(item_name, ann_path)[source]

Replaces given annotation json file for given item name to dataset annotations directory in json format.

Parameters:
item_name : str

Item Name.

ann_path : str

Path to the Annotation json file.

Returns:

None

Return type:

NoneType

Raises:

RuntimeError – if ann_path is not str

Usage Example:
import supervisely as sly

dataset_path = "/home/admin/work/supervisely/projects/lemons_annotated/ds1"
ds = sly.Dataset(dataset_path, sly.OpenMode.READ)

new_ann = "/home/admin/work/supervisely/projects/kiwi_annotated/ds1/ann/IMG_1812.jpeg.json"
ds.set_ann_file("IMG_1812.jpeg", new_ann)
async set_ann_file_async(item_name, ann_path)[source]

Replaces given annotation json file for given item name to dataset annotations directory in json format.

Parameters:
item_name : str

Item Name.

ann_path : str

Path to the Annotation json file.

Returns:

None

Return type:

NoneType

Raises:

RuntimeError – if ann_path is not str

Usage Example:
import supervisely as sly
from supervisely._utils import run_coroutine

dataset_path = "/home/admin/work/supervisely/projects/lemons_annotated/ds1"
ds = sly.Dataset(dataset_path, sly.OpenMode.READ)
new_ann = "/home/admin/work/supervisely/projects/kiwi_annotated/ds1/ann/IMG_1812.jpeg.json"

coroutine = ds.set_ann_file_async("IMG_1812.jpeg", new_ann)
run_coroutine(coroutine)
to_coco(meta, return_type='path', dest_dir=None, copy_images=False, with_captions=False, log_progress=False, progress_cb=None)[source]

Convert Supervisely dataset to COCO format.

Note: Depending on the return_type and with_captions parameters, the function returns different values.

If return_type is “path”, the COCO annotation files will be saved to the disk. If return_type is “dict”, the function returns COCO dataset in dictionary format. If with_captions is True, the function returns Tuple (instances and captions).

Parameters:
meta

Project meta information.

return_type : str, optional

Return type (path or dict).

dest_dir : str, optional

Path to save COCO dataset.

copy_images : bool, optional

If True, copies images to the COCO dataset directory.

with_captions : bool, optional

If True, returns captions

log_progress : str, optional

If True, log progress.

progress_cb=None

Progress callback.

Returns:

COCO dataset in dictionary format.

Return type:

dict

Usage Example:
import supervisely as sly

project_path = "/home/admin/work/supervisely/projects/lemons_annotated"
project = sly.Project(project_path, sly.OpenMode.READ)

for ds in project.datasets:
    dest_dir = "/home/admin/work/supervisely/projects/lemons_annotated/ds1"
    coco: Tuple[Dict, Dict] = ds.to_coco(project.meta, dest_dir=dest_dir)
to_pascal_voc(meta, dest_dir=None, train_val_split_coef=0.8, log_progress=False, progress_cb=None)[source]

Convert Supervisely dataset to Pascal VOC format.

Parameters:
meta

Project meta information.

dest_dir : str, optional

Destination directory.

train_val_split_coef : float, optional

Coefficient for splitting images into train and validation sets.

log_progress : str, optional

If True, log progress.

progress_cb=None

Progress callback.

Returns:

None

Return type:

NoneType

Usage Example:
import supervisely as sly

project_path = "/home/admin/work/supervisely/projects/lemons_annotated"
project = sly.Project(project_path, sly.OpenMode.READ)

for ds in project.datasets:
    dest_dir = "/home/admin/work/supervisely/projects/lemons_annotated/ds1"
    ds.to_pascal_voc(project.meta, dest_dir=dest_dir)
to_yolo(meta, dest_dir=None, task_type='detect', log_progress=False, progress_cb=None, is_val=None)[source]

Convert Supervisely dataset to YOLO format.

Parameters:
meta

Project meta information.

dest_dir : str, optional

Path to save YOLO dataset.

task_type : str, optional

Task type.

log_progress : str, optional

If True, log progress.

progress_cb=None

Progress callback.

is_val : bool, optional

If True, the dataset is a validation dataset.

Returns:

YOLO dataset in dictionary format.

Return type:

dict

Usage Example:
import supervisely as sly

project_path = "/home/admin/work/supervisely/projects/lemons_annotated"
project = sly.Project(project_path, sly.OpenMode.READ)

for ds in project.datasets:
    dest_dir = "/home/admin/work/supervisely/projects/lemons_annotated/ds1"
    ds.to_yolo(project.meta, dest_dir=dest_dir)
property ann_dir : str

Path to the dataset annotations directory.

Returns:

Path to the dataset directory with annotations.

Return type:

str

Usage Example:
import supervisely as sly

dataset_path = "/home/admin/work/supervisely/projects/lemons_annotated/ds1"
ds = sly.Dataset(dataset_path, sly.OpenMode.READ)

print(ds.ann_dir)
# Output: '/home/admin/work/supervisely/projects/lemons_annotated/ds1/ann'
property blob_offsets

List of paths to the dataset blob offset files.

Returns:

List of paths to the dataset blob offset files.

Return type:

List[str]

property directory : str

Path to the dataset directory.

Returns:

Path to the dataset directory.

Return type:

str

Usage Example:
import supervisely as sly

dataset_path = "/home/admin/work/supervisely/projects/lemons_annotated/ds1"
ds = sly.Dataset(dataset_path, sly.OpenMode.READ)

print(ds.directory)
# Output: '/home/admin/work/supervisely/projects/lemons_annotated/ds1'
property image_infos : list[supervisely.api.image_api.ImageInfo]

If the dataset is opened from the API, returns the list of ImageInfo objects. Otherwise raises an exception.

Raises:

ValueError – If the dataset is opened in local mode.

Returns:

List of ImageInfo objects.

Return type:

List[ImageInfo]

property img_dir : str

Path to the dataset images directory. Property is alias of item_dir.

Returns:

Path to the dataset directory with images.

Return type:

str

Usage Example:
import supervisely as sly

dataset_path = "/home/admin/work/supervisely/projects/lemons_annotated/ds1"
ds = sly.Dataset(dataset_path, sly.OpenMode.READ)

print(ds.img_dir)
# Output: '/home/admin/work/supervisely/projects/lemons_annotated/ds1/img'
property img_info_dir

Path to the dataset image info directory. Property is alias of item_info_dir.

Returns:

Path to the dataset directory with images info.

Return type:

str

Usage Example:
import supervisely as sly

dataset_path = "/home/admin/work/supervisely/projects/lemons_annotated/ds1"
ds = sly.Dataset(dataset_path, sly.OpenMode.READ)

print(ds.img_info_dir)
# Output: '/home/admin/work/supervisely/projects/lemons_annotated/ds1/img_info'
property item_dir : str

Path to the dataset items directory.

Returns:

Path to the dataset directory with items.

Return type:

str

Usage Example:
import supervisely as sly

dataset_path = "/home/admin/work/supervisely/projects/lemons_annotated/ds1"
ds = sly.Dataset(dataset_path, sly.OpenMode.READ)

print(ds.item_dir)
# Output: '/home/admin/work/supervisely/projects/lemons_annotated/ds1/img'
property item_info_dir

Path to the dataset item info directory.

Returns:

Path to the dataset directory with items info.

Return type:

str

Usage Example:
import supervisely as sly

dataset_path = "/home/admin/work/supervisely/projects/lemons_annotated/ds1"
ds = sly.Dataset(dataset_path, sly.OpenMode.READ)

print(ds.item_info_dir)
# Output: '/home/admin/work/supervisely/projects/lemons_annotated/ds1/img_info'
property meta_dir

Path to the dataset segmentation masks directory.

Returns:

Path to the dataset directory with masks.

Return type:

str

Usage Example:
import supervisely as sly

dataset_path = "/home/admin/work/supervisely/projects/lemons_annotated/ds1"
ds = sly.Dataset(dataset_path, sly.OpenMode.READ)

print(ds.meta_dir)
# Output: '/home/admin/work/supervisely/projects/lemons_annotated/ds1/meta'
property name : str

Full Dataset name, which includes it’s parents, e.g. ds1/ds2/ds3.

Use short_name to get only the name of the dataset.

Returns:

Dataset Name.

Return type:

str

Usage Example:
import supervisely as sly

dataset_path = "/home/admin/work/supervisely/projects/lemons_annotated/ds1"
ds = sly.Dataset(dataset_path, sly.OpenMode.READ)
print(ds.name)
# Output: "ds1"
property path : str

Returns a relative local path to the dataset.

Returns:

Relative local path to the dataset.

Return type:

str

property project_dir : str

Path to the project containing the dataset.

Returns:

Path to the project.

Return type:

str

Usage Example:
import supervisely as sly

dataset_path = "/home/admin/work/supervisely/projects/lemons_annotated/ds0"
ds = sly.Dataset(dataset_path, sly.OpenMode.READ)
print(ds.project_dir)
# Output: "/home/admin/work/supervisely/projects/lemons_annotated"
property seg_dir

Path to the dataset segmentation masks directory.

Returns:

Path to the dataset directory with masks.

Return type:

str

Usage Example:
import supervisely as sly

dataset_path = "/home/admin/work/supervisely/projects/lemons_annotated/ds1"
ds = sly.Dataset(dataset_path, sly.OpenMode.READ)

print(ds.seg_dir)
# Output: '/home/admin/work/supervisely/projects/lemons_annotated/ds1/seg'
property short_name : str

Short dataset name, which does not include it’s parents. To get the full name of the dataset, use name.

Returns:

Dataset Name.

Return type:

str

Usage Example:
import supervisely as sly

dataset_path = "/home/admin/work/supervisely/projects/lemons_annotated/ds1"
ds = sly.Dataset(dataset_path, sly.OpenMode.READ)
print(ds.name)
# Output: "ds1"