Skip to content

Commit d8ea95c

Browse files
Merge pull request #226 from fastlabel/feature/7597-fix-coco-keypoints
COCO形式のnum_keypoints算出ロジックを修正
2 parents c8ef314 + 613c9af commit d8ea95c

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

fastlabel/converters.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,13 +260,30 @@ def __get_coco_annotation_keypoints(keypoints: list, category_keypoints: list) -
260260
if keypoint["value"]
261261
}
262262
for category_key in category_keypoints:
263-
value = keypoint_values.get(category_key, [0, 0, 0])
263+
value = keypoint_values.get(category_key)
264+
if not value:
265+
coco_annotation_keypoints.extend([0, 0, 0])
266+
continue
264267
# Adjust fastlabel data definition to coco format
265268
visibility = 2 if value[2] == 1 else 1
266269
coco_annotation_keypoints.extend([value[0], value[1], visibility])
267270
return coco_annotation_keypoints
268271

269272

273+
COCO_KEYPOINT_V_INDEX = 2
274+
275+
276+
def __get_coco_num_keypoints(keypoints: list) -> int:
277+
# https://cocodataset.org/#format-data
278+
if not keypoints:
279+
return 0
280+
num_keypoints = 0
281+
for keypoint in keypoints:
282+
if keypoint["value"]:
283+
num_keypoints += 1
284+
return num_keypoints
285+
286+
270287
def __get_coco_annotation(
271288
id_: int,
272289
points: list,
@@ -278,7 +295,7 @@ def __get_coco_annotation(
278295
rotation: int,
279296
) -> dict:
280297
annotation = {}
281-
annotation["num_keypoints"] = len(keypoints) if keypoints else 0
298+
annotation["num_keypoints"] = __get_coco_num_keypoints(keypoints)
282299
annotation["keypoints"] = (
283300
__get_coco_annotation_keypoints(keypoints, category["keypoints"])
284301
if keypoints

0 commit comments

Comments
 (0)