A step-by-step tutorial of how to create custom export app from SDK export template class `sly.app.Export`.
Introduction
In this tutorial, you will learn how to create custom export app for exporting your data from Supervisely platform using an export template class sly.app.Export that we have prepared for you.
Create a class that inherits from sly.app.Export and write process method that will handle the team_id, workspace_id, project_id and dataset_id that you specified in the local.env. In this example our class called MyExport.
sly.app.Export class will handle export routines for you:
it will check that selected project or dataset exist and that you have access to work with it,
it will upload your result data to Team Files and clean temporary folder, containing result archive in remote container or local hard drive if you are debugging your app.
Your application must return string, containing path to result archive or folder. If you return path to folder - this folder will be automatically archived.
sly.app.Export has a Context subclass which contains all required information that you need for exporting your data from Supervisely platform:
Team ID - shows team id where exporting project or dataset is located
Workspace ID - shows workspace id where exporting project or dataset is located
Project ID - id of exporting project
Dataset ID - id of exporting dataset (detected only if the export is performed from dataset context menu)
context variable is passed as an argument to process method of class MyExport and context object will be created automatically when you execute export script.
class MyExport(sly.app.Export):
def process(self, context: sly.app.Export.Context):
print(context)
STORAGE_DIR = sly.app.get_data_dir() # path to directory for temp files and result archive
ANN_FILE_NAME = "labels.json"
class MyExport(sly.app.Export):
def process(self, context: sly.app.Export.Context):
# create api object to communicate with Supervisely Server
api = sly.Api.from_env()
# get project info from server
project_info = api.project.get_info_by_id(id=context.project_id)
# make project directory path
data_dir = os.path.join(STORAGE_DIR, f"{project_info.id}_{project_info.name}")
# get project meta
meta_json = api.project.get_meta(id=context.project_id)
project_meta = sly.ProjectMeta.from_json(meta_json)
# Check if the app runs from the context menu of the dataset.
if context.dataset_id is not None:
# If so, get the dataset info from the server.
dataset_infos = [api.dataset.get_info_by_id(context.dataset_id)]
else:
# If it does not, obtain all datasets infos from the current project.
dataset_infos = api.dataset.get_list(context.project_id)
# iterate over datasets in project
for dataset in dataset_infos:
result_anns = {}
# get dataset images info
images_infos = api.image.get_list(dataset.id)
# track progress using Tqdm
with tqdm(total=dataset.items_count) as pbar:
# iterate over images in dataset
for image_info in images_infos:
labels = []
# create path for each image and download it from server
image_path = os.path.join(data_dir, dataset.name, image_info.name)
api.image.download(image_info.id, image_path)
# download annotation for current image
ann_json = api.annotation.download_json(image_info.id)
ann = sly.Annotation.from_json(ann_json, project_meta)
# iterate over labels in current annotation
for label in ann.labels:
# get obj class name
name = label.obj_class.name
# get bounding box coordinates for label
bbox = label.geometry.to_bbox()
labels.append(
{
"class_name": name,
"coordinates": [
bbox.top,
bbox.left,
bbox.bottom,
bbox.right,
],
}
)
result_anns[image_info.name] = labels
# increment the current progress counter by 1
pbar.update(1)
# create JSON annotation in new format
filename = os.path.join(data_dir, dataset.name, ANN_FILE_NAME)
with open(filename, "w") as file:
json.dump(result_anns, file, indent=2)
return data_dir
Create MyExport object and execute run method to start export
app = MyExport()
app.run()
Debug export app
In this tutorial, we will be using the Run & Debug section of the VSCode to debug our export app.
The export template has 2 launch options for debugging: Debug and Advanced Debug. The settings for these options are configured in the launch.json file. Lets start from option #1 - Debug
This option is a good starting point. In this case, the resulting archive or folder with the exported data will remain on your computer and be saved in the path that we defined in the local.env file (SLY_APP_DATA_DIR="results/").
In addition to the regular debug option, this template also includes setting for Advanced debugging.
The advanced debugging option is somewhat identical, however it will upload result archive or folder with data to Team Files instead (Path to result archive - /tmp/supervisely/export/Supervisely App/<SESSION ID>/<PROJECT_ID>_<PROJECT_NAME>.tar). This option is an example of how production apps work in Supervisely platform.
Submitting an app to the Supervisely Ecosystem isn’t as simple as pushing code to github repository, but it’s not as complicated as you may think of it either.
Please follow this link for instructions on adding your app. We have produced a step-by-step guide on how to add your application to the Supervisely Ecosystem.