Project

class supervisely_lib.project.project.ItemPaths(img_path, ann_path)

Bases: tuple

ann_path

Alias for field number 1

img_path

Alias for field number 0

class supervisely_lib.project.project.OpenMode(value)[source]

Bases: enum.Enum

Вefines the mode of using the Project and Dataset.

READ = 1
CREATE = 2
class supervisely_lib.project.project.Dataset(directory: str, mode: supervisely_lib.project.project.OpenMode)[source]

Bases: supervisely_lib.collection.key_indexed_collection.KeyObject

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

Parameters
  • directory (str) – Path to dataset directory.

  • mode (OpenMode) – Determines working mode for the given dataset.

Usage example
from supervisely_lib.project.project import Dataset
dataset_path = "/home/admin/work/supervisely/projects/lemons_annotated"
ds = sly.project.project.Dataset(dataset_path, sly.OpenMode.READ)
item_dir_name = 'img'
annotation_class

alias of supervisely_lib.annotation.annotation.Annotation

property name

Dataset name.

Returns

Dataset Name

Return type

class

str

Usage example
from supervisely_lib.project.project import Dataset
dataset_path = "/home/admin/work/supervisely/projects/lemons_annotated"
ds = sly.project.project.Dataset(dataset_path, sly.OpenMode.READ)
print(dataset.name)
# Output: "ds1"
property directory

Path to Dataset directory.

Returns

Path to dataset directory

Return type

str

Usage example
from supervisely_lib.project.project import Dataset
dataset_path = "/home/admin/work/supervisely/projects/lemons_annotated"
ds = sly.project.project.Dataset(dataset_path, sly.OpenMode.READ)

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

Path to the dataset items directory.

Returns

Path to the dataset directory with items

Return type

str

Usage example
from supervisely_lib.project.project import Dataset
dataset_path = "/home/admin/work/supervisely/projects/lemons_annotated"
ds = sly.project.project.Dataset(dataset_path, sly.OpenMode.READ)

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

Path to the dataset annotations directory.

Returns

Path to the dataset directory with annotations

Return type

str

Usage example
from supervisely_lib.project.project import Dataset
dataset_path = "/home/admin/work/supervisely/projects/lemons_annotated"
ds = sly.project.project.Dataset(dataset_path, sly.OpenMode.READ)

print(ds.ann_dir)
# Output: '/home/admin/work/supervisely/projects/lemons_annotated/ds1/ann'
item_exists(item_name: str) → bool[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
from supervisely_lib.project.project import Dataset
dataset_path = "/home/admin/work/supervisely/projects/lemons_annotated"
ds = sly.project.project.Dataset(dataset_path, sly.OpenMode.READ)

ds.item_exists("IMG_0748")      # False
ds.item_exists("IMG_0748.jpeg") # True
get_item_path(item_name: str) → str[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
from supervisely_lib.project.project import Dataset
dataset_path = "/home/admin/work/supervisely/projects/lemons_annotated"
ds = sly.project.project.Dataset(dataset_path, sly.OpenMode.READ)

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

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

Path to the given ann.

Parameters

item_name (str) – Ann name.

Returns

Path to the given ann

Return type

str

Raises

RuntimeError if item not found in the project

Usage example
from supervisely_lib.project.project import Dataset
dataset_path = "/home/admin/work/supervisely/projects/lemons_annotated"
ds = sly.project.project.Dataset(dataset_path, sly.OpenMode.READ)

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

ds.get_ann_path("IMG_0748.jpeg")
# Output: '/home/admin/work/supervisely/projects/lemons_annotated/ds1/ann/IMG_0748.jpeg.json'
add_item_file(item_name: str, item_path: str, ann: Optional[supervisely_lib.annotation.annotation.Annotation] = None, _validate_item: bool = True, _use_hardlink: bool = False) → None[source]

Adds given item file 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_path (str) – Path to the item.

  • ann (Annotation or str, optional) – Annotation object or path to annotation.json file.

  • _validate_item (bool, optional) – Checks input files format.

  • _use_hardlink (bool, optional) – If True creates a hardlink pointing to src named dst, otherwise don’t.

Returns

None

Return type

NoneType

Raises

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

Usage example
from supervisely_lib.project.project import Dataset
dataset_path = "/home/admin/work/supervisely/projects/lemons_annotated"
ds = sly.project.project.Dataset(dataset_path, sly.OpenMode.READ)

ann = "/home/admin/work/supervisely/projects/Test/IMG_8888.jpeg.json"
ds.add_item_file("IMG_777.jpeg", "/home/admin/work/supervisely/projects/Test/IMG_8888.jpeg", ann=ann)
add_item_np(item_name: str, img: numpy.ndarray, ann: Optional[supervisely_lib.annotation.annotation.Annotation] = None) → 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 (Annotation or str, optional) – Annotation object or path to annotation.json file.

Returns

None

Return type

NoneType

Raises

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

Usage example
from supervisely_lib.project.project import Dataset
dataset_path = "/home/admin/work/supervisely/projects/lemons_annotated"
ds = sly.project.project.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)
add_item_raw_bytes(item_name: str, item_raw_bytes: bytes, ann: Optional[supervisely_lib.annotation.annotation.Annotation] = None) → 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 (Annotation or str, optional) – Annotation object or path to annotation.json file.

Returns

None

Return type

NoneType

Raises

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

Usage example
from supervisely_lib.project.project import Dataset
dataset_path = "/home/admin/work/supervisely/projects/lemons_annotated"
ds = sly.project.project.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_np("IMG_050.jpeg", img_bytes)
generate_item_path(item_name: str) → str[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
from supervisely_lib.project.project import Dataset
dataset_path = "/home/admin/work/supervisely/projects/lemons_annotated"
ds = sly.project.project.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'
set_ann(item_name: str, ann: supervisely_lib.annotation.annotation.Annotation) → None[source]

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

Parameters
  • item_name (str) – Item name.

  • ann (Annotation) – Annotation object.

Returns

None

Return type

NoneType

Usage example
from supervisely_lib.project.project import Dataset
dataset_path = "/home/admin/work/supervisely/projects/lemons_annotated"
ds = sly.project.project.Dataset(dataset_path, sly.OpenMode.READ)

height, width = 500, 700
ann = sly.Annotation((height, width))
ds.set_ann("IMG_0748.jpeg", ann)
set_ann_file(item_name: str, ann_path: str) → None[source]

Clones annotation with given name from dataset to the given annotation file path.

Parameters
  • item_name (str) – Item Name.

  • ann_path (str) – Path to the annotation file.

Returns

None

Return type

NoneType

Raises

Exception if ann_path is not str

Usage example
from supervisely_lib.project.project import Dataset
dataset_path = "/home/admin/work/supervisely/projects/lemons_annotated"
ds = sly.project.project.Dataset(dataset_path, sly.OpenMode.READ)

ann_out_path = "/home/admin/work/supervisely/projects/kiwi_annotated/ds1/ann/IMG_4455.jpeg.json"
ds.set_ann_file("IMG_1812.jpeg", ann_out_path)
set_ann_dict(item_name: str, ann: dict) → None[source]

Saves given annotation with given name to dataset annotations dir in json format.

Parameters
  • item_name (str) – Item name.

  • ann (dict) – Annotation as a dict in json format.

Returns

None

Return type

NoneType

Raises

Exception if ann_path is not str

Usage example
from supervisely_lib.project.project import Dataset
dataset_path = "/home/admin/work/supervisely/projects/lemons_annotated"
ds = sly.project.project.Dataset(dataset_path, sly.OpenMode.READ)

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

ds.set_ann_dict("IMG_8888.jpeg", ann_json)
get_item_paths(item_name: str)supervisely_lib.project.project.ItemPaths[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
from supervisely_lib.project.project import Dataset
dataset_path = "/home/admin/work/supervisely/projects/lemons_annotated"
ds = sly.project.project.Dataset(dataset_path, sly.OpenMode.READ)

img_path, ann_path = dataset.get_item_paths("IMG_0748.jpeg")
print("img_path: " img_path, "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
class supervisely_lib.project.project.Project(directory: str, mode: supervisely_lib.project.project.OpenMode)[source]

Bases: object

Project is a parent directory for dataset. Project object is immutable.

Parameters
  • directory (str) – Path to project directory.

  • mode (OpenMode) – Determines working mode for the given project.

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

alias of supervisely_lib.project.project.Dataset

class DatasetDict(items: Optional[dict] = None)[source]

Bases: supervisely_lib.collection.key_indexed_collection.KeyIndexedCollection

item_type

alias of supervisely_lib.project.project.Dataset

property parent_dir

Project parent directory.

Returns

Path to project parent directory

Return type

str

Usage example
project = sly.Project("/home/admin/work/supervisely/projects/lemons_annotated", sly.OpenMode.READ)
print(project.parent_dir)
# Output: '/home/admin/work/supervisely/projects'
property name

Project name.

Returns

Name

Return type

str

Usage example
project = sly.Project("/home/admin/work/supervisely/projects/lemons_annotated", sly.OpenMode.READ)
print(project.name)
# Output: 'lemons_annotated'
property datasets

Project datasets.

Returns

Datasets

Return type

DatasetDict

Usage example
project = sly.Project("/home/admin/work/supervisely/projects/lemons_annotated", sly.OpenMode.READ)
for dataset in project.datasets:
   print(dataset.name)
   # Output: ds1
   #         ds2
property meta

Project meta.

Returns

Project meta

Return type

str

Usage example
project = sly.Project("/home/admin/work/supervisely/projects/lemons_annotated", sly.OpenMode.READ)
print(project.meta)
# Output:
# +-------+--------+----------------+--------+
# |  Name | Shape  |     Color      | Hotkey |
# +-------+--------+----------------+--------+
# |  kiwi | Bitmap |  [255, 0, 0]   |        |
# | lemon | Bitmap | [81, 198, 170] |        |
# +-------+--------+----------------+--------+
# Tags
# +------+------------+-----------------+--------+---------------+--------------------+
# | Name | Value type | Possible values | Hotkey | Applicable to | Applicable classes |
# +------+------------+-----------------+--------+---------------+--------------------+
property directory

Path to the project directory.

Returns

Path to the project directory

Return type

str

Usage example
project = sly.Project("/home/admin/work/supervisely/projects/lemons_annotated", sly.OpenMode.READ)
print(project.directory)
# Output: '/home/admin/work/supervisely/projects/lemons_annotated'
property total_items

Total number of items in project.

Returns

total number of items in project

Return type

int

Usage example
project = sly.Project("/home/admin/work/supervisely/projects/lemons_annotated", sly.OpenMode.READ)
print(project.total_items)
# Output: 12
set_meta(new_meta: supervisely_lib.project.project_meta.ProjectMeta) → None[source]

Saves given meta to project directory in json format.

Parameters

new_meta (ProjectMeta) – ProjectMeta object.

Returns

None

Return type

NoneType

Usage example
proj_lemons = sly.Project("/home/admin/work/supervisely/projects/lemons_annotated", sly.OpenMode.READ)
proj_kiwi = sly.Project("/home/admin/work/supervisely/projects/kiwi_annotated", sly.OpenMode.READ)

proj_lemons.set_meta(proj_kiwi.meta)

print(project.proj_lemons)
# Output:
# +-------+--------+----------------+--------+
# |  Name | Shape  |     Color      | Hotkey |
# +-------+--------+----------------+--------+
# |  kiwi | Bitmap |  [255, 0, 0]   |        |
# +-------+--------+----------------+--------+
create_dataset(ds_name: str)supervisely_lib.project.project.Dataset[source]

Creates a subdirectory with given name and all intermediate subdirectories for items and annotations in project directory, and also adds created dataset to the collection of all datasets in the project.

Parameters

ds_name (str) – Dataset name.

Returns

Dataset object

Return type

Dataset

Usage example
project = sly.Project("/home/admin/work/supervisely/projects/lemons_annotated", sly.OpenMode.READ)
project.create_dataset("ds3")

for dataset in project.datasets:
    print(dataset.name)

# Output: ds1
#         ds2
#         ds3
copy_data(dst_directory: str, dst_name: Optional[str] = None, _validate_item: bool = True, _use_hardlink: bool = False)supervisely_lib.project.project.Project[source]

Makes a copy of the Project.

Parameters
  • dst_directory (str) – Path to project parent directory.

  • dst_name (str, optional) – Project name.

  • _validate_item (bool, optional) – Checks input files format.

  • _use_hardlink (bool, optional) – If True creates a hardlink pointing to src named dst, otherwise don’t.

Returns

Project object

Return type

Project

Usage example
project = sly.Project("/home/admin/work/supervisely/projects/lemons_annotated", sly.OpenMode.READ)
project.copy_data("/home/admin/work/supervisely/projects/", "lemons_copy")
supervisely_lib.project.project.read_single_project(dir: str, project_class: supervisely_lib.project.project.Project)supervisely_lib.project.project.Project[source]

Read project from given directory.

Parameters
  • dir (str) – Path to project parent directory, which must contain only project folder.

  • project_class (Project) – Project object

Returns

Project class object

Return type

Project

Raises

Exception if given directory contains more than one subdirectory.

Usage example
proj_dir = "/home/admin/work/supervisely/source/project" # Is a Project's parent directory containing project folder!
project = sly.read_single_project(proj_dir)
supervisely_lib.project.project.upload_project(dir: str, api: supervisely_lib.api.api.Api, workspace_id: int, project_name: Optional[str] = None, log_progress: bool = True)[source]

Uploads project to Supervisely from the given directory.

Parameters
  • dir (str) – Path to project directory.

  • api (Api) – Supervisely API address and token.

  • workspace_id (int) – Workspace ID, where project will be uploaded.

  • project_name (str, optional) – Name of the project in Supervisely.

  • log_progress (bool, optional) – Show uploading progress bar.

Returns

Project ID and name

Return type

int, str

Usage example
# Local folder with Project
project_directory = "/home/admin/work/supervisely/source/project"
project = sly.Project(project_directory, sly.OpenMode.READ)

# Obtain server address and your api_token from environment variables
# Edit those values if you run this notebook on your own PC
address = os.environ['SERVER_ADDRESS']
token = os.environ['API_TOKEN']

# Initialize API object
api = sly.Api(address, token)

# Upload Project
sly.upload_project(project_directory, api, workspace_id=45, project_name="Lemons")
supervisely_lib.project.project.download_project(api: supervisely_lib.api.api.Api, project_id: int, dest_dir: str, dataset_ids: Optional[List[int]] = None, log_progress: bool = False, batch_size: int = 10, cache: Optional[supervisely_lib.io.fs_cache.FileCache] = None, progress_cb=None) → None[source]

Download project from Supervisely to the given directory.

Parameters
  • api (Api) – Supervisely API address and token.

  • project_id (int) – Supervisely downloadable project ID.

  • dest_dir (str) – Destination directory.

  • dataset_ids (List[int], optional) – Dataset IDs.

  • log_progress (bool, optional) – Show uploading progress bar.

  • batch_size (int, optional) – The number of images in the batch when they are loaded to a host.

  • cache (FileCache, optional) – FileCache object.

  • progress_cb (Function) – Function to check progress.

Returns

None

Return type

NoneType

Usage example
# Local destination Project folder
save_directory = "/home/admin/work/supervisely/source/project"

# Obtain server address and your api_token from environment variables
# Edit those values if you run this notebook on your own PC
address = os.environ['SERVER_ADDRESS']
token = os.environ['API_TOKEN']

# Initialize API object
api = sly.Api(address, token)
project_id = 8888

# Upload Project
sly.download_project(api, project_id, save_directory)