Skip to content

Commit 6b31063

Browse files
Merge pull request #199 from fastlabel/feature/output-keypoints-coco-format-order
Adjust annotation keypoints output to follow the defined order of keypoints
2 parents 0d408b6 + 85d4130 commit 6b31063

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

fastlabel/converters.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ def __to_coco_annotation(data: dict) -> dict:
233233
annotation_id,
234234
points,
235235
keypoints,
236-
category["id"],
236+
category,
237237
image_id,
238238
annotation_type,
239239
annotation_attributes,
@@ -249,13 +249,11 @@ def __get_coco_category_by_name(categories: list, name: str) -> Optional[dict]:
249249
return None
250250

251251

252-
def __get_coco_annotation_keypoints(keypoints: list) -> list:
252+
def __get_coco_annotation_keypoints(keypoints: list, category_keypoints: list) -> list:
253253
coco_annotation_keypoints = []
254-
for keypoint in keypoints:
255-
value = keypoint["value"]
256-
if not value:
257-
coco_annotation_keypoints.extend([0, 0, 0])
258-
continue
254+
keypoint_values = {keypoint["key"]: keypoint["value"] for keypoint in keypoints if keypoint["value"]}
255+
for category_key in category_keypoints:
256+
value = keypoint_values.get(category_key, [0, 0, 0])
259257
# Adjust fastlabel data definition to coco format
260258
visibility = 2 if value[2] == 1 else 1
261259
coco_annotation_keypoints.extend([value[0], value[1], visibility])
@@ -266,22 +264,22 @@ def __get_coco_annotation(
266264
id_: int,
267265
points: list,
268266
keypoints: list,
269-
category_id: int,
267+
category: dict,
270268
image_id: str,
271269
annotation_type: str,
272270
annotation_attributes: Dict[str, AttributeValue],
273271
) -> dict:
274272
annotation = {}
275273
annotation["num_keypoints"] = len(keypoints) if keypoints else 0
276274
annotation["keypoints"] = (
277-
__get_coco_annotation_keypoints(keypoints) if keypoints else []
275+
__get_coco_annotation_keypoints(keypoints, category["keypoints"]) if keypoints else []
278276
)
279277
annotation["segmentation"] = __to_coco_segmentation(annotation_type, points)
280278
annotation["iscrowd"] = 0
281279
annotation["area"] = __to_area(annotation_type, points)
282280
annotation["image_id"] = image_id
283281
annotation["bbox"] = __to_bbox(annotation_type, points)
284-
annotation["category_id"] = category_id
282+
annotation["category_id"] = category["id"]
285283
annotation["id"] = id_
286284
annotation["attributes"] = annotation_attributes
287285
return annotation

0 commit comments

Comments
 (0)