From 366faf9555085d98f260f2fcf446428f0e8e5a68 Mon Sep 17 00:00:00 2001 From: GoldenAnpu Date: Mon, 29 Jul 2024 16:32:26 +0200 Subject: [PATCH] Add Workflow and upgrade SDK to v6.73.141 --- config.json | 13 +++------- dev_requirements.txt | 2 +- src/convert_sly_to_yolov5.py | 8 +++++- src/workflow.py | 50 ++++++++++++++++++++++++++++++++++++ 4 files changed, 62 insertions(+), 11 deletions(-) create mode 100644 src/workflow.py diff --git a/config.json b/config.json index 8686118..af50b55 100644 --- a/config.json +++ b/config.json @@ -2,12 +2,9 @@ "name": "Convert Supervisely to YOLO v5 format", "type": "app", "version": "2.0.0", - "categories": [ - "images", - "export" - ], + "categories": ["images", "export"], "description": "Transform project to YOLO v5 format and prepares tar archive for download", - "docker_image": "supervisely/import-export:6.73.93", + "docker_image": "supervisely/import-export:6.73.141", "instance_version": "6.9.22", "main_script": "src/convert_sly_to_yolov5.py", "modal_template": "src/modal.html", @@ -21,10 +18,8 @@ "icon": "https://i.imgur.com/pz3eSzx.png", "icon_background": "#FFFFFF", "context_menu": { - "target": [ - "images_project" - ], + "target": ["images_project"], "context_root": "Download as" }, "poster": "https://user-images.githubusercontent.com/106374579/183683758-89476d80-de3f-424f-9bfa-f1562703a168.png" -} \ No newline at end of file +} diff --git a/dev_requirements.txt b/dev_requirements.txt index a1e9b07..5e44b10 100644 --- a/dev_requirements.txt +++ b/dev_requirements.txt @@ -1 +1 @@ -supervisely==6.73.93 +supervisely==6.73.141 diff --git a/src/convert_sly_to_yolov5.py b/src/convert_sly_to_yolov5.py index 8da2fa9..d638ef2 100644 --- a/src/convert_sly_to_yolov5.py +++ b/src/convert_sly_to_yolov5.py @@ -3,6 +3,7 @@ from typing import List, Tuple from dotenv import load_dotenv import supervisely as sly +from workflow import Workflow # region constants TRAIN_TAG_NAME = "train" @@ -201,10 +202,15 @@ def _add_to_split(image_id, img_name, split_ids, split_image_paths, labels_dir, sly.logger.info("Number of images in val: {}".format(val_count)) # Archiving and uploading the directory to the TeamFiles. - sly.output.set_download(result_dir) + file_info = sly.output.set_download(result_dir) sly.logger.info("File uploaded, app stopped.") + # --------------------------------- Add Workflow Input And Output -------------------------------- # + workflow.add_input(project_id) + workflow.add_output(file_info) + # --------------------------------- Add Workflow Input And Output -------------------------------- # if __name__ == "__main__": api = sly.Api.from_env() + workflow = Workflow(api) transform(api) diff --git a/src/workflow.py b/src/workflow.py new file mode 100644 index 0000000..e22851e --- /dev/null +++ b/src/workflow.py @@ -0,0 +1,50 @@ +import supervisely as sly + + +def check_compatibility(func): + def wrapper(self, *args, **kwargs): + if self.is_compatible is None: + try: + self.is_compatible = self.check_instance_ver_compatibility() + except Exception as e: + sly.logger.error( + "Can not check compatibility with Supervisely instance. " + f"Workflow features will be disabled. Error: {repr(e)}" + ) + self.is_compatible = False + if not self.is_compatible: + return + return func(self, *args, **kwargs) + + return wrapper + + +class Workflow: + def __init__(self, api: sly.Api, min_instance_version: str = None): + self.is_compatible = None + self.api = api + self._min_instance_version = ( + "6.9.31" if min_instance_version is None else min_instance_version + ) + + def check_instance_ver_compatibility(self): + if not self.api.is_version_supported(self._min_instance_version): + sly.logger.info( + f"Supervisely instance version {self.api.instance_version} does not support workflow features." + ) + if not sly.is_community(): + sly.logger.info( + f"To use them, please update your instance to version {self._min_instance_version} or higher." + ) + return False + return True + + @check_compatibility + def add_input(self, project_id: int): + self.api.app.workflow.add_input_project(project_id) + sly.logger.debug(f"Workflow: Input project - {project_id}") + + @check_compatibility + def add_output(self, file_info: sly.api.file_api.FileInfo): + self.api.app.workflow.add_output_file(file_info) + sly.logger.debug(f"Workflow: Output file - {file_info.id if file_info else None}")