Skip to content

Commit bd4f4a0

Browse files
authored
Merge pull request #115 from fastlabel/842-import-pose-estimation-annotation
update pose anno convert COCO to fastlabel
2 parents 1175b77 + fd6f97b commit bd4f4a0

File tree

3 files changed

+57
-20
lines changed

3 files changed

+57
-20
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1797,14 +1797,16 @@ dataset
17971797

17981798
### COCO
17991799

1800-
Supported bbox or polygon annotation type.
1800+
Supported bbox , polygon or pose_estimation annotation type.
18011801

18021802
Convert annotation file of [COCO format](https://cocodataset.org/#format-data) as a Fastlabel format and create task.
18031803

18041804
file_path: COCO annotation json file path
18051805

18061806
```python
1807-
annotations_map = client.convert_coco_to_fastlabel(file_path="./sample.json")
1807+
annotations_map = client.convert_coco_to_fastlabel(file_path="./sample.json", annotation_type="bbox")
1808+
# annotation_type = "bbox", "polygon" or "pose_estimation
1809+
18081810
task_id = client.create_image_task(
18091811
project="YOUR_PROJECT_SLUG",
18101812
name="sample.jpg",

fastlabel/__init__.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1610,7 +1610,7 @@ def find_integrated_audio_task_by_prefix(
16101610

16111611
# Convert to Fastlabel
16121612

1613-
def convert_coco_to_fastlabel(self, file_path: str) -> dict:
1613+
def convert_coco_to_fastlabel(self, file_path: str, annotation_type: str) -> dict:
16141614
"""
16151615
Convert COCO format to FastLabel format as annotation file.
16161616
@@ -1647,9 +1647,7 @@ def convert_coco_to_fastlabel(self, file_path: str) -> dict:
16471647
]
16481648
}
16491649
"""
1650-
with open(file_path, "r") as f:
1651-
file = f.read()
1652-
return converters.execute_coco_to_fastlabel(eval(file))
1650+
return converters.execute_coco_to_fastlabel(json.load(open(file_path)), annotation_type)
16531651

16541652
def convert_labelme_to_fastlabel(self, folder_path: str) -> dict:
16551653
"""

fastlabel/converters.py

Lines changed: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from concurrent.futures import ThreadPoolExecutor
2+
from curses import keyname
23
from datetime import datetime
34
from decimal import Decimal
45
from typing import List
@@ -10,6 +11,8 @@
1011
from fastlabel.const import AnnotationType
1112
import os
1213

14+
from fastlabel.exceptions import FastLabelInvalidException
15+
1316
# COCO
1417

1518

@@ -656,14 +659,16 @@ def __get_pixel_coordinates(points: List[int or float]) -> List[int]:
656659
return new_points
657660

658661

659-
def execute_coco_to_fastlabel(coco: dict) -> dict:
662+
def execute_coco_to_fastlabel(coco: dict ,annotation_type:str) -> dict:
660663
coco_images = {}
661664
for c in coco["images"]:
662665
coco_images[c["id"]] = c["file_name"]
663666

664667
coco_categories = {}
668+
coco_categories_keypoints={}
665669
for c in coco["categories"]:
666670
coco_categories[c["id"]] = c["name"]
671+
coco_categories_keypoints[c["id"]] = c["keypoints"]
667672

668673
coco_annotations = coco["annotations"]
669674

@@ -682,19 +687,51 @@ def execute_coco_to_fastlabel(coco: dict) -> dict:
682687
if not category_name:
683688
return
684689

685-
segmentation = target_coco_annotation["segmentation"][0]
686-
annotation_type = ""
687-
if len(segmentation) == 4:
688-
annotation_type = AnnotationType.bbox.value
689-
if len(segmentation) > 4:
690-
annotation_type = AnnotationType.polygon.value
691-
annotations.append(
692-
{
693-
"value": category_name,
694-
"points": segmentation,
695-
"type": annotation_type,
696-
}
697-
)
690+
if annotation_type in [AnnotationType.bbox.value, AnnotationType.polygon.value]:
691+
segmentation = target_coco_annotation["segmentation"][0]
692+
annotation_type = ""
693+
if len(segmentation) == 4:
694+
annotation_type = AnnotationType.bbox.value
695+
if len(segmentation) > 4:
696+
annotation_type = AnnotationType.polygon.value
697+
annotations.append(
698+
{
699+
"value": category_name,
700+
"points": segmentation,
701+
"type": annotation_type,
702+
}
703+
)
704+
elif annotation_type == AnnotationType.pose_estimation.value:
705+
keypoints = []
706+
target_coco_annotation_keypoints = target_coco_annotation["keypoints"]
707+
keypoint_keys = coco_categories_keypoints[target_coco_annotation["category_id"]]
708+
# coco keypoint style [100,200,1,300,400,1,500,600,2] convert to [[100,200,1],[300,400,1],[500,600,2]]
709+
keypoint_values = [target_coco_annotation_keypoints[i:i + 3] for i in range(0, len(target_coco_annotation_keypoints), 3)]
710+
for index, keypoint_key in enumerate(keypoint_keys):
711+
keypoint_value = keypoint_values[index]
712+
if keypoint_value[2] == 0:
713+
continue
714+
if not keypoint_value[2] in [1, 2]:
715+
raise FastLabelInvalidException(f"Visibility flag must be 0 or 1, 2 . annotation_id: {target_coco_annotation['id']}", 422)
716+
# fastlabel occulusion is 0 or 1 . coco occulusion is 1 or 2.
717+
keypoint_value[2] = keypoint_value[2] - 1
718+
keypoints.append({
719+
"key": keypoint_key,
720+
"value": keypoint_value
721+
})
722+
723+
annotations.append(
724+
{
725+
"value": category_name,
726+
"type": annotation_type,
727+
"keypoints": keypoints
728+
}
729+
)
730+
else:
731+
raise FastLabelInvalidException(
732+
"Annotation type must be bbox or polygon ,pose_estimation.", 422
733+
)
734+
698735
results[coco_images[coco_image_key]] = annotations
699736
return results
700737

0 commit comments

Comments
 (0)