From 7bd465533cd589e55d5d2f611c80074b8654001b Mon Sep 17 00:00:00 2001 From: Takahiro Tamenishi Date: Wed, 29 Jun 2022 12:05:59 +0900 Subject: [PATCH 1/6] update pose anno convert COCO to fastlabel --- fastlabel/__init__.py | 4 +-- fastlabel/converters.py | 62 +++++++++++++++++++++++++++++++---------- 2 files changed, 50 insertions(+), 16 deletions(-) diff --git a/fastlabel/__init__.py b/fastlabel/__init__.py index c94be6b..2dcacd4 100644 --- a/fastlabel/__init__.py +++ b/fastlabel/__init__.py @@ -1575,7 +1575,7 @@ def delete_task(self, task_id: str) -> None: # Convert to Fastlabel - def convert_coco_to_fastlabel(self, file_path: str) -> dict: + def convert_coco_to_fastlabel(self, file_path: str, annotation_type: str) -> dict: """ Convert COCO format to FastLabel format as annotation file. @@ -1614,7 +1614,7 @@ def convert_coco_to_fastlabel(self, file_path: str) -> dict: """ with open(file_path, "r") as f: file = f.read() - return converters.execute_coco_to_fastlabel(eval(file)) + return converters.execute_coco_to_fastlabel(eval(file.replace("false","False").replace("true","True")), annotation_type) def convert_labelme_to_fastlabel(self, folder_path: str) -> dict: """ diff --git a/fastlabel/converters.py b/fastlabel/converters.py index 9ed6148..c81e353 100644 --- a/fastlabel/converters.py +++ b/fastlabel/converters.py @@ -1,4 +1,5 @@ from concurrent.futures import ThreadPoolExecutor +from curses import keyname from datetime import datetime from decimal import Decimal from typing import List @@ -10,6 +11,8 @@ from fastlabel.const import AnnotationType import os +from fastlabel.exceptions import FastLabelInvalidException + # COCO @@ -656,14 +659,16 @@ def __get_pixel_coordinates(points: List[int or float]) -> List[int]: return new_points -def execute_coco_to_fastlabel(coco: dict) -> dict: +def execute_coco_to_fastlabel(coco: dict ,annotation_type:str) -> dict: coco_images = {} for c in coco["images"]: coco_images[c["id"]] = c["file_name"] coco_categories = {} + coco_categories_keypoints={} for c in coco["categories"]: coco_categories[c["id"]] = c["name"] + coco_categories_keypoints[c["id"]] = c["keypoints"] coco_annotations = coco["annotations"] @@ -682,19 +687,48 @@ def execute_coco_to_fastlabel(coco: dict) -> dict: if not category_name: return - segmentation = target_coco_annotation["segmentation"][0] - annotation_type = "" - if len(segmentation) == 4: - annotation_type = AnnotationType.bbox.value - if len(segmentation) > 4: - annotation_type = AnnotationType.polygon.value - annotations.append( - { - "value": category_name, - "points": segmentation, - "type": annotation_type, - } - ) + if annotation_type in ["bbox", "polygon"]: + segmentation = target_coco_annotation["segmentation"][0] + annotation_type = "" + if len(segmentation) == 4: + annotation_type = AnnotationType.bbox.value + if len(segmentation) > 4: + annotation_type = AnnotationType.polygon.value + annotations.append( + { + "value": category_name, + "points": segmentation, + "type": annotation_type, + } + ) + elif annotation_type == "pose_estimation": + keypoints = [] + target_coco_annotation_keypoints = target_coco_annotation["keypoints"] + keypoint_keys = coco_categories_keypoints[target_coco_annotation["category_id"]] + keypoint_values = [target_coco_annotation_keypoints[i:i + 3] for i in range(0, len(target_coco_annotation_keypoints), 3)] + for index, keypoint_key in enumerate(keypoint_keys): + keypoint_value = keypoint_values[index] + if keypoint_value == [0,0,0]: + raise FastLabelInvalidException(f"Keypoint value is [0,0,0]. annotationId: {target_coco_annotation['id']}", 422) + # fastlabel occulusion is 0 or 1 . coco occulusion is 1 or 2. + keypoint_value[2] = keypoint_value[2] - 1 + keypoints.append({ + "key": keypoint_key, + "value": keypoint_value + }) + + annotations.append( + { + "value": category_name, + "type": annotation_type, + "keypoints": keypoints + } + ) + else: + raise FastLabelInvalidException( + "Annotation type must be bbox or polygon ,pose_estimation.", 422 + ) + results[coco_images[coco_image_key]] = annotations return results From fca4ae83498b73ab25070cc04fa35d6fe0789873 Mon Sep 17 00:00:00 2001 From: Takahiro Tamenishi Date: Wed, 29 Jun 2022 16:30:57 +0900 Subject: [PATCH 2/6] fix read file --- fastlabel/__init__.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/fastlabel/__init__.py b/fastlabel/__init__.py index 2dcacd4..a87852c 100644 --- a/fastlabel/__init__.py +++ b/fastlabel/__init__.py @@ -1612,9 +1612,7 @@ def convert_coco_to_fastlabel(self, file_path: str, annotation_type: str) -> dic ] } """ - with open(file_path, "r") as f: - file = f.read() - return converters.execute_coco_to_fastlabel(eval(file.replace("false","False").replace("true","True")), annotation_type) + return converters.execute_coco_to_fastlabel(json.load(open(file_path)), annotation_type) def convert_labelme_to_fastlabel(self, folder_path: str) -> dict: """ From d8f8bade792b070022d68ca22ee56b2c3f6dc406 Mon Sep 17 00:00:00 2001 From: Takahiro Tamenishi Date: Thu, 30 Jun 2022 12:39:16 +0900 Subject: [PATCH 3/6] fix annotation type --- fastlabel/converters.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/fastlabel/converters.py b/fastlabel/converters.py index c81e353..711891d 100644 --- a/fastlabel/converters.py +++ b/fastlabel/converters.py @@ -687,7 +687,7 @@ def execute_coco_to_fastlabel(coco: dict ,annotation_type:str) -> dict: if not category_name: return - if annotation_type in ["bbox", "polygon"]: + if annotation_type in [AnnotationType.bbox, AnnotationType.polygon]: segmentation = target_coco_annotation["segmentation"][0] annotation_type = "" if len(segmentation) == 4: @@ -701,15 +701,16 @@ def execute_coco_to_fastlabel(coco: dict ,annotation_type:str) -> dict: "type": annotation_type, } ) - elif annotation_type == "pose_estimation": + elif annotation_type == AnnotationType.pose_estimation: keypoints = [] target_coco_annotation_keypoints = target_coco_annotation["keypoints"] keypoint_keys = coco_categories_keypoints[target_coco_annotation["category_id"]] + # coco keypoint style [100,200,1,300,400,1,500,600,2] convert to [[100,200,1],[300,400,1],[500,600,2]] keypoint_values = [target_coco_annotation_keypoints[i:i + 3] for i in range(0, len(target_coco_annotation_keypoints), 3)] for index, keypoint_key in enumerate(keypoint_keys): keypoint_value = keypoint_values[index] if keypoint_value == [0,0,0]: - raise FastLabelInvalidException(f"Keypoint value is [0,0,0]. annotationId: {target_coco_annotation['id']}", 422) + raise FastLabelInvalidException(f"Keypoint value is [0,0,0]. annotation_id: {target_coco_annotation['id']}", 422) # fastlabel occulusion is 0 or 1 . coco occulusion is 1 or 2. keypoint_value[2] = keypoint_value[2] - 1 keypoints.append({ From 8a06ef907b9abf27f14198e2a99f2efe830b6748 Mon Sep 17 00:00:00 2001 From: Takahiro Tamenishi Date: Thu, 30 Jun 2022 12:46:04 +0900 Subject: [PATCH 4/6] add readme --- README.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 2836d77..fef772b 100644 --- a/README.md +++ b/README.md @@ -1684,14 +1684,16 @@ dataset ### COCO -Supported bbox or polygon annotation type. +Supported bbox , polygon or pose_estimation annotation type. Convert annotation file of [COCO format](https://cocodataset.org/#format-data) as a Fastlabel format and create task. file_path: COCO annotation json file path ```python -annotations_map = client.convert_coco_to_fastlabel(file_path="./sample.json") +annotations_map = client.convert_coco_to_fastlabel(file_path="./sample.json", annotation_type="bbox") +# annotation_type = "bbox", "polygon" or "pose_estimation + task_id = client.create_image_task( project="YOUR_PROJECT_SLUG", name="sample.jpg", From c9668236c5298a05e2157bc1529805e3e963a846 Mon Sep 17 00:00:00 2001 From: Takahiro Tamenishi Date: Fri, 1 Jul 2022 19:09:55 +0900 Subject: [PATCH 5/6] fix validation --- fastlabel/converters.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fastlabel/converters.py b/fastlabel/converters.py index 711891d..497731b 100644 --- a/fastlabel/converters.py +++ b/fastlabel/converters.py @@ -709,7 +709,7 @@ def execute_coco_to_fastlabel(coco: dict ,annotation_type:str) -> dict: keypoint_values = [target_coco_annotation_keypoints[i:i + 3] for i in range(0, len(target_coco_annotation_keypoints), 3)] for index, keypoint_key in enumerate(keypoint_keys): keypoint_value = keypoint_values[index] - if keypoint_value == [0,0,0]: + if keypoint_value[2] == 0: raise FastLabelInvalidException(f"Keypoint value is [0,0,0]. annotation_id: {target_coco_annotation['id']}", 422) # fastlabel occulusion is 0 or 1 . coco occulusion is 1 or 2. keypoint_value[2] = keypoint_value[2] - 1 From fd6f97ba256cdbdbd1300019d33f2367e8484f22 Mon Sep 17 00:00:00 2001 From: Takahiro Tamenishi Date: Fri, 1 Jul 2022 22:46:49 +0900 Subject: [PATCH 6/6] update validation --- fastlabel/converters.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/fastlabel/converters.py b/fastlabel/converters.py index 497731b..4dbaa63 100644 --- a/fastlabel/converters.py +++ b/fastlabel/converters.py @@ -687,7 +687,7 @@ def execute_coco_to_fastlabel(coco: dict ,annotation_type:str) -> dict: if not category_name: return - if annotation_type in [AnnotationType.bbox, AnnotationType.polygon]: + if annotation_type in [AnnotationType.bbox.value, AnnotationType.polygon.value]: segmentation = target_coco_annotation["segmentation"][0] annotation_type = "" if len(segmentation) == 4: @@ -701,7 +701,7 @@ def execute_coco_to_fastlabel(coco: dict ,annotation_type:str) -> dict: "type": annotation_type, } ) - elif annotation_type == AnnotationType.pose_estimation: + elif annotation_type == AnnotationType.pose_estimation.value: keypoints = [] target_coco_annotation_keypoints = target_coco_annotation["keypoints"] keypoint_keys = coco_categories_keypoints[target_coco_annotation["category_id"]] @@ -709,8 +709,10 @@ def execute_coco_to_fastlabel(coco: dict ,annotation_type:str) -> dict: keypoint_values = [target_coco_annotation_keypoints[i:i + 3] for i in range(0, len(target_coco_annotation_keypoints), 3)] for index, keypoint_key in enumerate(keypoint_keys): keypoint_value = keypoint_values[index] - if keypoint_value[2] == 0: - raise FastLabelInvalidException(f"Keypoint value is [0,0,0]. annotation_id: {target_coco_annotation['id']}", 422) + if keypoint_value[2] == 0: + continue + if not keypoint_value[2] in [1, 2]: + raise FastLabelInvalidException(f"Visibility flag must be 0 or 1, 2 . annotation_id: {target_coco_annotation['id']}", 422) # fastlabel occulusion is 0 or 1 . coco occulusion is 1 or 2. keypoint_value[2] = keypoint_value[2] - 1 keypoints.append({