ObjectClassApi

class ObjectClassApi[source]

Bases: supervisely.api.module_api.ModuleApi

API for working with ObjClass. ObjectClassApi object is immutable.

Parameters
api : Api

API connection to the server.

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()

# Pass values into the API constructor (optional, not recommended)
# api = sly.Api(server_address="https://app.supervise.ly", token="4r47N...xaTatb")

project_id = 1951
obj_class_infos = api.object_class.get_list(project_id)

Methods

convert_info_to_json

_convert_info_to_json

exists

Checks if an entity with the given parent_id and name exists

get_free_name

Generates a free name for an entity with the given parent_id and name.

get_info_by_id

get_info_by_name

Get information about an entity by its name from the Supervisely server.

get_list

List of ObjClasses in the given Project.

get_list_all_pages

Get list of all or limited quantity entities from the Supervisely server.

get_list_all_pages_generator

This generator function retrieves a list of all or a limited quantity of entities from the Supervisely server, yielding batches of entities as they are retrieved

get_list_idx_page_async

Get the list of items for a given page number.

get_list_page_generator_async

Yields list of images in dataset asynchronously page by page.

get_name_to_id_map

type project_id

int

info_sequence

NamedTuple ObjectClassInfo information about ObjectClass.

info_tuple_name

NamedTuple name - ObjectClassInfo.

update

Update the class with the given ID on the server.

Attributes

MAX_WAIT_ATTEMPTS

Maximum number of attempts that will be made to wait for a certain condition to be met.

WAIT_ATTEMPT_TIMEOUT_SEC

Number of seconds for intervals between attempts.

InfoType

alias of supervisely.api.module_api.ObjectClassInfo

classmethod convert_info_to_json(info)

_convert_info_to_json

Return type

Dict

exists(parent_id, name)

Checks if an entity with the given parent_id and name exists

Parameters
parent_id : int

ID of the parent entity.

name : str

Name of the entity.

Returns

Returns True if entity exists, and False if not

Return type

bool

Usage example
import supervisely as sly

# You can connect to API directly
address = 'https://app.supervise.ly/'
token = 'Your Supervisely API Token'
api = sly.Api(address, token)

# Or you can use API from environment
os.environ['SERVER_ADDRESS'] = 'https://app.supervisely.com'
os.environ['API_TOKEN'] = 'Your Supervisely API Token'
api = sly.Api.from_env()


name = "IMG_0315.jpeg"
dataset_id = 55832
exists = api.image.exists(dataset_id, name)
print(exists) # True
get_free_name(parent_id, name)

Generates a free name for an entity with the given parent_id and name. Adds an increasing suffix to original name until a unique name is found.

Parameters
parent_id : int

ID of the parent entity.

name : str

Name of the entity.

Returns

Returns free name.

Return type

str

Usage example
import supervisely as sly

# You can connect to API directly
address = 'https://app.supervise.ly/'
token = 'Your Supervisely API Token'
api = sly.Api(address, token)

# Or you can use API from environment
os.environ['SERVER_ADDRESS'] = 'https://app.supervisely.com'
os.environ['API_TOKEN'] = 'Your Supervisely API Token'
api = sly.Api.from_env()


name = "IMG_0315.jpeg"
dataset_id = 55832
free_name = api.image.get_free_name(dataset_id, name)
print(free_name) # IMG_0315_001.jpeg
get_info_by_name(parent_id, name, fields=[])

Get information about an entity by its name from the Supervisely server.

Parameters
parent_id : int

ID of the parent entity.

name : str

Name of the entity for which the information is being retrieved.

fields : List[str]

The list of api fields which will be returned with the response.

Usage example
import supervisely as sly

# You can connect to API directly
address = 'https://app.supervise.ly/'
token = 'Your Supervisely API Token'
api = sly.Api(address, token)

# Or you can use API from environment
os.environ['SERVER_ADDRESS'] = 'https://app.supervisely.com'
os.environ['API_TOKEN'] = 'Your Supervisely API Token'
api = sly.Api.from_env()


dataset_id = 55832
name = "IMG_0315.jpeg"
info = api.image.get_info_by_name(dataset_id, name)
print(info)
# Output: ImageInfo(id=19369643, name='IMG_0315.jpeg', ...)
get_list(project_id, filters=None)[source]

List of ObjClasses in the given Project.

Parameters
project_id : int

Project ID in which the ObjClasses are located.

filters : List[dict], optional

List of params to sort output ObjClasses.

Returns

List of ObjClasses with information from the given Project. See info_sequence

Return type

List[NamedTuple]

Usage example
import supervisely as sly

os.environ['SERVER_ADDRESS'] = 'https://app.supervisely.com'
os.environ['API_TOKEN'] = 'Your Supervisely API Token'
api = sly.Api.from_env()

project_id = 1951
obj_class_infos = api.object_class.get_list(project_id)
print(obj_class_infos)
# Output: [ObjectClassInfo(id=22309,
#                          name='lemon',
#                          description='',
#                          shape='bitmap',
#                          color='#51C6AA',
#                          settings={},
#                          created_at='2021-03-02T10:04:33.973Z',
#                          updated_at='2021-03-11T09:37:07.111Z'),
#  ObjectClassInfo(id=22310,
#                  name='kiwi',
#                  description='',
#                  shape='bitmap',
#                  color='#FF0000',
#                  settings={},
#                  created_at='2021-03-02T10:04:33.973Z',
#                  updated_at='2021-03-11T09:37:07.111Z')
# ]

obj_class_list = api.object_class.get_list(1951, filters=[{'field': 'name', 'operator': '=', 'value': 'lemon' }])
print(obj_class_list)
# Output: [
#     [
#         22309,
#         "lemon",
#         "",
#         "bitmap",
#         "#51C6AA",
#         {},
#         "2021-03-02T10:04:33.973Z",
#         "2021-03-11T09:37:07.111Z"
#     ]
# ]
get_list_all_pages(method, data, progress_cb=None, convert_json_info_cb=None, limit=None, return_first_response=False)

Get list of all or limited quantity entities from the Supervisely server.

Parameters
method : str

Request method name

data : dict

Dictionary with request body info

progress_cb : Progress, optional

Function for tracking download progress.

convert_json_info_cb : Callable, optional

Function for convert json info

limit : int, optional

Number of entity to retrieve

return_first_response : bool, optional

Specify if return first response

get_list_all_pages_generator(method, data, progress_cb=None, convert_json_info_cb=None, limit=None, return_first_response=False)

This generator function retrieves a list of all or a limited quantity of entities from the Supervisely server, yielding batches of entities as they are retrieved

Parameters
method : str

Request method name

data : dict

Dictionary with request body info

progress_cb : Progress, optional

Function for tracking download progress.

convert_json_info_cb : Callable, optional

Function for convert json info

limit : int, optional

Number of entity to retrieve

return_first_response : bool, optional

Specify if return first response

async get_list_idx_page_async(method, data)

Get the list of items for a given page number. Page number is specified in the data dictionary.

Parameters
method : str

Method to call for listing items.

data : dict

Data to pass to the API method.

Returns

List of items.

Return type

Tuple[int, List[NamedTuple]]

async get_list_page_generator_async(method, data, pages_count=None, semaphore=None)

Yields list of images in dataset asynchronously page by page.

Parameters
method : str

Method to call for listing items.

data : dict

Data to pass to the API method.

pages_count : int, optional

Preferred number of pages to retrieve if used with a per_page limit. Will be automatically adjusted if the pagesCount differs from the requested number.

semaphore : asyncio.Semaphore, optional

Semaphore for limiting the number of simultaneous requests.

kwargs

Additional arguments.

Returns

List of images in dataset.

Return type

AsyncGenerator[List[ImageInfo]]

Usage example
import supervisely as sly
import asyncio

os.environ['SERVER_ADDRESS'] = 'https://app.supervisely.com'
os.environ['API_TOKEN'] = 'Your Supervisely API Token'
api = sly.Api.from_env()

method = 'images.list'
data = {
    'datasetId': 123456
}

loop = sly.utils.get_or_create_event_loop()
images = loop.run_until_complete(api.image.get_list_generator_async(method, data))
get_name_to_id_map(project_id)[source]
Parameters
project_id : int

Project ID in which the ObjClasses are located.

Returns

Dictionary Key ID Map {‘key’: id}

Return type

KeyIdMap

Usage example

import supervisely as sly

os.environ['SERVER_ADDRESS'] = 'https://app.supervisely.com'
os.environ['API_TOKEN'] = 'Your Supervisely API Token'
api = sly.Api.from_env()

obj_class_map = api.object_class.get_name_to_id_map(1951)
print(obj_class_map)
# Output: {'lemon': 22309, 'kiwi': 22310, 'cucumber': 22379}
static info_sequence()[source]

NamedTuple ObjectClassInfo information about ObjectClass.

Example
ObjectClassInfo(id=22309,
                name='lemon',
                description='',
                shape='bitmap',
                color='#51C6AA',
                settings={},
                created_at='2021-03-02T10:04:33.973Z',
                updated_at='2021-03-11T09:37:07.111Z')
static info_tuple_name()[source]

NamedTuple name - ObjectClassInfo.

update(id, name=None, description=None, hotkey=None, shape=None, color=None, settings=None)[source]

Update the class with the given ID on the server. Returned object contains updated information about the class except settings.

Parameters
id : int

ID of the class to update.

name : str, optional

New name of the class.

description : str, optional

New description of the class.

hotkey : str, optional

New hotkey of the class (e.g., “K”).

shape : str, optional

New shape of the class.

color : str, optional

New color of the class in HEX format (e.g., #FFFFFF).

settings : dict, optional

New settings of the class. Do not pass “availableShapes” for shape other than “any”. Do not pass “availableShapes” that does not contain the current shape.

Returns

Updated class information

Return type

ObjectClassInfo

Usage example

import supervisely as sly

api = sly.Api.from_env()

obj_class_info = api.object_class.update(
    id=22309,
    shape='any',
    settings={
        "availableShapes": [
            "bitmap",
            "polygon",
        ],
    },
)