Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 2 additions & 4 deletions fastlabel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

COCO jsonのファイルの中に姿勢推定のデータ化を見分けるフラグがなかったので、アノテーションタイプを入力するようにしています

Copy link
Contributor

@qoone qoone Jun 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

カテゴリーの、"keypoints" の値が空かどうかで判断できると思うけど難しい?
現状利用している人に影響が出ちゃうから、インターフェスはどうしても無理な場合以外は、極力変えないようにしたい!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@qoone
このお客さんからもらったファイルを見る限りでは、セグメンテーションのプロパティに座標情報が入っているかつ、キーポイントにも座標情報が入っていたので、このファイルを見る限りでは、一つのCOCOのファイルに矩形(多角形)とキーポイントの両方の情報が入れられると認識してます。

お客さんからもらったファイル
https://drive.google.com/file/d/1rXWrO31eYFx9l4v-uU-UmnO1XHdCtfqx/view?usp=sharing

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

了解!

"""
Convert COCO format to FastLabel format as annotation file.

Expand Down Expand Up @@ -1612,9 +1612,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(json.load(open(file_path)), annotation_type)

def convert_labelme_to_fastlabel(self, folder_path: str) -> dict:
"""
Expand Down
65 changes: 51 additions & 14 deletions fastlabel/converters.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -10,6 +11,8 @@
from fastlabel.const import AnnotationType
import os

from fastlabel.exceptions import FastLabelInvalidException

# COCO


Expand Down Expand Up @@ -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"]

Expand All @@ -682,19 +687,51 @@ 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 [AnnotationType.bbox.value, AnnotationType.polygon.value]:
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 == AnnotationType.pose_estimation.value:
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)]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3つずつ配列にしてるんだと思うけどわかりにくいので、
処理前と処理後の配列とかを例にコメント追加しておいてもらえると!

for index, keypoint_key in enumerate(keypoint_keys):
keypoint_value = keypoint_values[index]
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)
Comment on lines +712 to +715
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

・"Visibility flagが0の場合はキーポイントがないのでスキップにする。
   ↓
・"Visibility flagが1,2以外の場合は、バリデーションエラーにする

# 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

Expand Down