11from concurrent .futures import ThreadPoolExecutor
2+ from curses import keyname
23from datetime import datetime
34from decimal import Decimal
45from typing import List
1011from fastlabel .const import AnnotationType
1112import 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