From 6bcbc937ed60d05e9271a21646d8429e38d23ac7 Mon Sep 17 00:00:00 2001 From: "koki.murasawa" Date: Fri, 18 Aug 2023 18:05:47 +0900 Subject: [PATCH 1/5] Update export_coco method to include attributes --- fastlabel/converters.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/fastlabel/converters.py b/fastlabel/converters.py index a99b7a6..b78ec82 100644 --- a/fastlabel/converters.py +++ b/fastlabel/converters.py @@ -74,6 +74,7 @@ def get_annotation_points(anno, _): "annotation_type": annotation["type"], "annotation_points": get_annotation_points(annotation, index), "annotation_keypoints": annotation.get("keypoints"), + "annotation_attributes": annotation.get("attributes"), "categories": categories, "image_id": task_image["id"], } @@ -204,6 +205,7 @@ def __to_coco_annotation(data: dict) -> dict: annotation_type = data["annotation_type"] annotation_value = data["annotation_value"] annotation_id = 0 + annotation_attributes = data["annotation_attributes"] if annotation_type not in [ AnnotationType.bbox.value, @@ -226,7 +228,7 @@ def __to_coco_annotation(data: dict) -> dict: return None return __get_coco_annotation( - annotation_id, points, keypoints, category["id"], image_id, annotation_type + annotation_id, points, keypoints, category["id"], image_id, annotation_type, annotation_attributes ) @@ -259,6 +261,7 @@ def __get_coco_annotation( category_id: int, image_id: str, annotation_type: str, + annotation_attributes: dict[str, any], ) -> dict: annotation = {} annotation["num_keypoints"] = len(keypoints) if keypoints else 0 @@ -272,6 +275,7 @@ def __get_coco_annotation( annotation["bbox"] = __to_bbox(annotation_type, points) annotation["category_id"] = category_id annotation["id"] = id_ + annotation["attributes"] = annotation_attributes return annotation From 27a5e5115afdbd78952377e6f7f91b36d9ee9e9f Mon Sep 17 00:00:00 2001 From: "koki.murasawa" Date: Fri, 18 Aug 2023 19:07:08 +0900 Subject: [PATCH 2/5] Update convert_coco_to_fastlabel method to include attributes --- fastlabel/converters.py | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/fastlabel/converters.py b/fastlabel/converters.py index b78ec82..5840418 100644 --- a/fastlabel/converters.py +++ b/fastlabel/converters.py @@ -74,7 +74,9 @@ def get_annotation_points(anno, _): "annotation_type": annotation["type"], "annotation_points": get_annotation_points(annotation, index), "annotation_keypoints": annotation.get("keypoints"), - "annotation_attributes": annotation.get("attributes"), + "annotation_attributes": _get_coco_annotation_attributes( + annotation + ), "categories": categories, "image_id": task_image["id"], } @@ -228,7 +230,13 @@ def __to_coco_annotation(data: dict) -> dict: return None return __get_coco_annotation( - annotation_id, points, keypoints, category["id"], image_id, annotation_type, annotation_attributes + annotation_id, + points, + keypoints, + category["id"], + image_id, + annotation_type, + annotation_attributes, ) @@ -852,6 +860,12 @@ def execute_coco_to_fastlabel(coco: dict, annotation_type: str) -> dict: annotations = [] for target_coco_annotation in target_coco_annotations: + attributes = [ + {"key": attribute_key, "value": attribute_value} + for attribute_key, attribute_value in target_coco_annotation[ + "attributes" + ].items() + ] category_name = coco_categories[target_coco_annotation["category_id"]] if not category_name: return @@ -871,6 +885,7 @@ def execute_coco_to_fastlabel(coco: dict, annotation_type: str) -> dict: "value": category_name, "points": segmentation, "type": annotation_type, + "attributes": attributes, } ) elif annotation_type == AnnotationType.pose_estimation.value: @@ -902,6 +917,7 @@ def execute_coco_to_fastlabel(coco: dict, annotation_type: str) -> dict: "value": category_name, "type": annotation_type, "keypoints": keypoints, + "attributes": attributes, } ) else: @@ -1111,3 +1127,13 @@ def _get_annotation_points_for_video_annotation(annotation: dict, index: int): def _get_annotation_points_for_image_annotation(annotation: dict): return annotation.get("points") + + +def _get_coco_annotation_attributes(annotation: dict) -> dict[str, any]: + coco_attributes = {} + attributes = annotation.get("attributes") + if not attributes: + return coco_attributes + for attribute in attributes: + coco_attributes[attribute["key"]] = attribute["value"] + return coco_attributes From 97b6d371eca4fb056b76a5a983b9453b7ceecb78 Mon Sep 17 00:00:00 2001 From: "koki.murasawa" Date: Mon, 21 Aug 2023 13:38:56 +0900 Subject: [PATCH 3/5] Fixed type of attributes value --- fastlabel/const.py | 7 +++++-- fastlabel/converters.py | 6 +++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/fastlabel/const.py b/fastlabel/const.py index c5882de..48f8b78 100644 --- a/fastlabel/const.py +++ b/fastlabel/const.py @@ -1,4 +1,5 @@ import math +from typing import Union from enum import Enum # only 57 types @@ -221,6 +222,8 @@ SUPPORTED_INFERENCE_IMAGE_SIZE = 6 * math.pow(1024, 2) +AttributeValue = Union[str, list[str], float, list[float]] + class AnnotationType(Enum): bbox = "bbox" @@ -232,9 +235,9 @@ class AnnotationType(Enum): classification = "classification" pose_estimation = "pose_estimation" + class Priority(Enum): none = 0 low = 10 medium = 20 - high = 30 - + high = 30 \ No newline at end of file diff --git a/fastlabel/converters.py b/fastlabel/converters.py index 5840418..2882af1 100644 --- a/fastlabel/converters.py +++ b/fastlabel/converters.py @@ -15,7 +15,7 @@ import numpy as np import requests -from fastlabel.const import AnnotationType +from fastlabel.const import AnnotationType, AttributeValue from fastlabel.exceptions import FastLabelInvalidException from fastlabel.utils import is_video_project_type @@ -269,7 +269,7 @@ def __get_coco_annotation( category_id: int, image_id: str, annotation_type: str, - annotation_attributes: dict[str, any], + annotation_attributes: dict[str, AttributeValue], ) -> dict: annotation = {} annotation["num_keypoints"] = len(keypoints) if keypoints else 0 @@ -1129,7 +1129,7 @@ def _get_annotation_points_for_image_annotation(annotation: dict): return annotation.get("points") -def _get_coco_annotation_attributes(annotation: dict) -> dict[str, any]: +def _get_coco_annotation_attributes(annotation: dict) -> dict[str, AttributeValue]: coco_attributes = {} attributes = annotation.get("attributes") if not attributes: From 6c117e73eba49c0700db55b0f42110b21265d912 Mon Sep 17 00:00:00 2001 From: "koki.murasawa" Date: Mon, 21 Aug 2023 14:40:38 +0900 Subject: [PATCH 4/5] Updated attribute value type annotations for Python 3.7 compatibility --- fastlabel/const.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fastlabel/const.py b/fastlabel/const.py index 48f8b78..113a3da 100644 --- a/fastlabel/const.py +++ b/fastlabel/const.py @@ -1,5 +1,5 @@ import math -from typing import Union +from typing import Union, List from enum import Enum # only 57 types @@ -222,7 +222,7 @@ SUPPORTED_INFERENCE_IMAGE_SIZE = 6 * math.pow(1024, 2) -AttributeValue = Union[str, list[str], float, list[float]] +AttributeValue = Union[str, List[str], float, List[float]] class AnnotationType(Enum): From f54c7f20aefb66afbc9fe0b9fb49a003f37eed02 Mon Sep 17 00:00:00 2001 From: "koki.murasawa" Date: Mon, 21 Aug 2023 14:48:27 +0900 Subject: [PATCH 5/5] Added handling for cases where 'attributes' key is absent --- fastlabel/converters.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/fastlabel/converters.py b/fastlabel/converters.py index 2882af1..2fa8350 100644 --- a/fastlabel/converters.py +++ b/fastlabel/converters.py @@ -860,11 +860,10 @@ def execute_coco_to_fastlabel(coco: dict, annotation_type: str) -> dict: annotations = [] for target_coco_annotation in target_coco_annotations: + attributes_items = target_coco_annotation.get("attributes", {}) attributes = [ {"key": attribute_key, "value": attribute_value} - for attribute_key, attribute_value in target_coco_annotation[ - "attributes" - ].items() + for attribute_key, attribute_value in attributes_items.items() ] category_name = coco_categories[target_coco_annotation["category_id"]] if not category_name: