diff --git a/config.json b/config.json index 3e73e54..6b2f8f6 100644 --- a/config.json +++ b/config.json @@ -3,18 +3,15 @@ "type": "app", "version": "2.0.0", "description": "Converts Supervisely format to COCO Keypoints", - "categories": [ - "images", - "export" - ], + "categories": ["images", "export"], "main_script": "src/main.py", "headless": true, "icon": "https://github.com/supervisely-ecosystem/export-coco-keypoints/assets/119248312/c915fbea-a418-4e6b-ab74-899bcb6edd3b", "icon_cover": true, "poster": "https://github.com/supervisely-ecosystem/export-coco-keypoints/assets/119248312/5777a6fb-efe5-41c3-93b9-4abe92006b77", "modal_template": "src/modal.html", - "docker_image": "supervisely/import-export:6.73.93", - "min_instance_version": "6.9.22", + "docker_image": "supervisely/import-export:6.73.157", + "min_instance_version": "6.10.0", "task_location": "workspace_tasks", "modal_template_state": { "allDatasets": true, @@ -23,10 +20,7 @@ "selectedOutput": "images" }, "context_menu": { - "target": [ - "images_project", - "images_dataset" - ], + "target": ["images_project", "images_dataset"], "context_root": "Download as" } -} \ No newline at end of file +} diff --git a/dev_requirements.txt b/dev_requirements.txt index 691715d..ad7a272 100644 --- a/dev_requirements.txt +++ b/dev_requirements.txt @@ -1,6 +1,5 @@ python-dotenv -supervisely==6.73.93 - +supervisely==6.73.157 # formatter black diff --git a/src/main.py b/src/main.py index a282851..6e0a241 100644 --- a/src/main.py +++ b/src/main.py @@ -8,6 +8,7 @@ from dotenv import load_dotenv import functions as f +import workflow as w if sly.is_development(): load_dotenv("local.env") @@ -20,18 +21,23 @@ all_datasets = bool(strtobool(os.getenv("modal.state.allDatasets"))) selected_datasets = ast.literal_eval(os.environ.get("modal.state.datasets", [])) +api = sly.Api.from_env() class MyExport(sly.app.Export): def process(self, context: sly.app.Export.Context): - api = sly.Api.from_env() - project = api.project.get_info_by_id(id=context.project_id) if context.dataset_id is not None: datasets = [api.dataset.get_info_by_id(context.dataset_id)] + w.workflow_input(api, datasets[0].id, type="dataset") elif len(selected_datasets) > 0 and not all_datasets: - datasets = [api.dataset.get_info_by_id(dataset_id) for dataset_id in selected_datasets] + datasets = [api.dataset.get_info_by_id(dataset_id) for dataset_id in selected_datasets] + if len(datasets) == 1: + w.workflow_input(api, datasets[0].id, type="dataset") + else: + w.workflow_input(api, project.id, type="project") else: datasets = api.dataset.get_list(project.id) + w.workflow_input(api, project.id, type="project") project_meta = sly.ProjectMeta.from_json(api.project.get_meta(project.id)) categories_mapping = f.get_categories_map_from_meta(project_meta) @@ -86,6 +92,7 @@ def main(): try: app = MyExport() app.run() + w.workflow_output(api, app.output_file) except Exception as e: exception_handler = handle_exception(e) if exception_handler: diff --git a/src/workflow.py b/src/workflow.py new file mode 100644 index 0000000..6c76598 --- /dev/null +++ b/src/workflow.py @@ -0,0 +1,33 @@ +# This module contains the functions that are used to configure the input and output of the workflow for the current app. + +from typing import Union, Literal + +import supervisely as sly + + +def workflow_input(api: sly.Api, id: int, type: Literal["project", "dataset"]): + if type == "project": + api.app.workflow.add_input_project(id) + sly.logger.debug(f"Workflow: Input project - {id}") + elif type == "dataset": + api.app.workflow.add_input_dataset(id) + sly.logger.debug(f"Workflow: Input dataset - {id}") + + +def workflow_output(api: sly.Api, file: Union[int, sly.api.file_api.FileInfo]): + try: + if isinstance(file, int): + file = api.file.get_info_by_id(file) + relation_settings = sly.WorkflowSettings( + title=file.name, + icon="archive", + icon_color="#33c94c", + icon_bg_color="#d9f7e4", + url=f"/files/{file.id}/true/?teamId={file.team_id}", + url_title="Download", + ) + meta = sly.WorkflowMeta(relation_settings=relation_settings) + api.app.workflow.add_output_file(file, meta=meta) + sly.logger.debug(f"Workflow: Output file - {file}") + except Exception as e: + sly.logger.debug(f"Failed to add output to the workflow: {repr(e)}")