Skip to content

Commit b0d355a

Browse files
Merge pull request #174 from fastlabel/feature/add-attributes-to-coco-annotations
Add attributes to coco annotations
2 parents 9b14a9d + f54c7f2 commit b0d355a

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

fastlabel/const.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import math
2+
from typing import Union, List
23
from enum import Enum
34

45
# only 57 types
@@ -221,6 +222,8 @@
221222

222223
SUPPORTED_INFERENCE_IMAGE_SIZE = 6 * math.pow(1024, 2)
223224

225+
AttributeValue = Union[str, List[str], float, List[float]]
226+
224227

225228
class AnnotationType(Enum):
226229
bbox = "bbox"
@@ -232,9 +235,9 @@ class AnnotationType(Enum):
232235
classification = "classification"
233236
pose_estimation = "pose_estimation"
234237

238+
235239
class Priority(Enum):
236240
none = 0
237241
low = 10
238242
medium = 20
239-
high = 30
240-
243+
high = 30

fastlabel/converters.py

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import numpy as np
1616
import requests
1717

18-
from fastlabel.const import AnnotationType
18+
from fastlabel.const import AnnotationType, AttributeValue
1919
from fastlabel.exceptions import FastLabelInvalidException
2020
from fastlabel.utils import is_video_project_type
2121

@@ -74,6 +74,9 @@ def get_annotation_points(anno, _):
7474
"annotation_type": annotation["type"],
7575
"annotation_points": get_annotation_points(annotation, index),
7676
"annotation_keypoints": annotation.get("keypoints"),
77+
"annotation_attributes": _get_coco_annotation_attributes(
78+
annotation
79+
),
7780
"categories": categories,
7881
"image_id": task_image["id"],
7982
}
@@ -204,6 +207,7 @@ def __to_coco_annotation(data: dict) -> dict:
204207
annotation_type = data["annotation_type"]
205208
annotation_value = data["annotation_value"]
206209
annotation_id = 0
210+
annotation_attributes = data["annotation_attributes"]
207211

208212
if annotation_type not in [
209213
AnnotationType.bbox.value,
@@ -226,7 +230,13 @@ def __to_coco_annotation(data: dict) -> dict:
226230
return None
227231

228232
return __get_coco_annotation(
229-
annotation_id, points, keypoints, category["id"], image_id, annotation_type
233+
annotation_id,
234+
points,
235+
keypoints,
236+
category["id"],
237+
image_id,
238+
annotation_type,
239+
annotation_attributes,
230240
)
231241

232242

@@ -259,6 +269,7 @@ def __get_coco_annotation(
259269
category_id: int,
260270
image_id: str,
261271
annotation_type: str,
272+
annotation_attributes: dict[str, AttributeValue],
262273
) -> dict:
263274
annotation = {}
264275
annotation["num_keypoints"] = len(keypoints) if keypoints else 0
@@ -272,6 +283,7 @@ def __get_coco_annotation(
272283
annotation["bbox"] = __to_bbox(annotation_type, points)
273284
annotation["category_id"] = category_id
274285
annotation["id"] = id_
286+
annotation["attributes"] = annotation_attributes
275287
return annotation
276288

277289

@@ -848,6 +860,11 @@ def execute_coco_to_fastlabel(coco: dict, annotation_type: str) -> dict:
848860

849861
annotations = []
850862
for target_coco_annotation in target_coco_annotations:
863+
attributes_items = target_coco_annotation.get("attributes", {})
864+
attributes = [
865+
{"key": attribute_key, "value": attribute_value}
866+
for attribute_key, attribute_value in attributes_items.items()
867+
]
851868
category_name = coco_categories[target_coco_annotation["category_id"]]
852869
if not category_name:
853870
return
@@ -867,6 +884,7 @@ def execute_coco_to_fastlabel(coco: dict, annotation_type: str) -> dict:
867884
"value": category_name,
868885
"points": segmentation,
869886
"type": annotation_type,
887+
"attributes": attributes,
870888
}
871889
)
872890
elif annotation_type == AnnotationType.pose_estimation.value:
@@ -898,6 +916,7 @@ def execute_coco_to_fastlabel(coco: dict, annotation_type: str) -> dict:
898916
"value": category_name,
899917
"type": annotation_type,
900918
"keypoints": keypoints,
919+
"attributes": attributes,
901920
}
902921
)
903922
else:
@@ -1107,3 +1126,13 @@ def _get_annotation_points_for_video_annotation(annotation: dict, index: int):
11071126

11081127
def _get_annotation_points_for_image_annotation(annotation: dict):
11091128
return annotation.get("points")
1129+
1130+
1131+
def _get_coco_annotation_attributes(annotation: dict) -> dict[str, AttributeValue]:
1132+
coco_attributes = {}
1133+
attributes = annotation.get("attributes")
1134+
if not attributes:
1135+
return coco_attributes
1136+
for attribute in attributes:
1137+
coco_attributes[attribute["key"]] = attribute["value"]
1138+
return coco_attributes

0 commit comments

Comments
 (0)