In this tutorial, we will create a simple import app that will import images from selected folder to Supervisely server. This application is headless (no GUI) and is designed to demonstrate the basic principles of creating minimalistic import applications.
Create API object to communicate with Supervisely Server and initialize application. Loads from supervisely.env file
# Create api object to communicate with Supervisely Serverapi = sly.Api.from_env()# Initialize applicationapp = sly.Application()
Step 4. Create new project and dataset on Supervisely server
project = api.project.create(WORKSPACE_ID, "My Project", change_name_if_conflict=True)dataset = api.dataset.create(project.id, "ds0", change_name_if_conflict=True)
Step 5. Download data from Supervisely server
Check if app was launched in production mode and download data from Supervisely server
# Download folder from Supervisely serverif IS_PRODUCTION isTrue: api.file.download_directory(TEAM_ID, PATH_TO_FOLDER, STORAGE_DIR)# Set path to folder with images PATH_TO_FOLDER = STORAGE_DIR
Step 6. List files in directory
Get list of files in directory and create list of images names and paths
# Process folder with images and upload them to Supervisely serverwithtqdm(total=len(images_paths))as pbar:for img_name, img_path inzip(images_names, images_paths):try:# Upload image into dataset on Supervisely server info = api.image.upload_path(dataset_id=dataset.id, name=img_name, path=img_path) sly.logger.trace(f"Image has been uploaded: id={info.id}, name={info.name}")exceptExceptionas e: sly.logger.warn("Skip image", extra={"name": img_name, "reason": repr(e)})finally:# Update progress bar pbar.update(1)# Log info about result projectsly.logger.info(f"Result project: id={project.id}, name={project.name}")
Output of the app in development mode:
{"message": "Application is running on localhost in development mode", "timestamp": "2023-06-09T17:06:12.671Z", "level": "info"}
{"message": "Application PID is 11269", "timestamp": "2023-06-09T17:06:12.671Z", "level": "info"}
Processing: 100%|██████████████████████████████████████████████████████████████████████████| 3/3 [00:01<00:00, 2.02it/s]
{"message": "Result project: id=22962, name=My Project_004", "timestamp": "2023-06-09T17:06:16.139Z", "level": "info"}
Step 3. Advanced debug
Advanced debug is for final app testing. In this case, import app will download data from Supervisely server. You can use this mode to test your app before publishing it to the Ecosystem.
To switch between local and advanced debug modes, select corresponding debug configuration in Run & Debug menu in VS Code
Open advanced.env and set up environment variables by inserting your values here for debugging.
advanced.env:
TEAM_ID=8# ⬅️ change it to your team IDWORKSPACE_ID=349# ⬅️ change it to your workspace IDFOLDER="/data/my_folder/"# ⬅️ path to folder on Supervisely serverSLY_APP_DATA_DIR="input/"# ⬅️ path to directory for local debugging
Please note that the path you specify in the SLY_APP_DATA_DIR variable will be used for storing import data.
For example:
path on your local computer could be /Users/admin/projects/import-app-from-scratch/input/
path in the current project folder on your local computer could be input/
Also note that all paths on Supervisely server are absolute and start from '/' symbol, so you need to specify the full path to the folder, for example /data/my_folder/
Don't forget to add this path to .gitignore to exclude it from the list of files tracked by Git.