44from collections import namedtuple
55from datetime import datetime
66
7+ from pathlib import Path
78from panopticapi .utils import IdGenerator , id2rgb
89from PIL import Image
910from tqdm import tqdm
1819 coco_keypoint_detection_to_sa_vector , coco_object_detection_to_sa_vector
1920)
2021from .sa_pixel_to_coco import (
21- sa_pixel_to_coco_instance_segmentation , sa_pixel_to_coco_object_detection ,
22+ sa_pixel_to_coco_instance_segmentation ,
2223 sa_pixel_to_coco_panoptic_segmentation
2324)
2425from .sa_vector_to_coco import (
@@ -44,34 +45,32 @@ def __setup_conversion_algorithm(self):
4445 def __str__ (self , ):
4546 return '{} object' .format (self .name )
4647
47- def _sa_to_coco_single (self , id_ , json_path , id_generator ):
48+ def _sa_to_coco_single (self , id_ , json_path , id_generator , cat_id_map ):
4849
4950 image_commons = self ._prepare_single_image_commons (id_ , json_path )
50- res = self .conversion_algorithm (image_commons , id_generator )
51+ res = self .conversion_algorithm (image_commons , id_generator , cat_id_map )
5152
5253 return res
5354
5455 def sa_to_output_format (self ):
5556 out_json = self ._create_skeleton ()
5657 out_json ['categories' ] = self ._create_categories (
57- os . path . join ( self .export_root , 'classes_mapper.json' )
58+ self .export_root / 'classes_mapper.json'
5859 )
5960
60- panoptic_root = os .path .join (
61- self .dataset_name , "panoptic_{}" .format (self .dataset_name )
62- )
61+ cat_id_map = json .load (open (self .export_root / 'classes_mapper.json' ))
6362
6463 images = []
6564 annotations = []
6665 id_generator = self ._make_id_generator ()
67- jsons = glob . glob (
68- os . path . join ( self .export_root , '*pixel.json' ), recursive = True
69- )
66+
67+ jsons_gen = self .export_root . glob ( '*pixel.json' )
68+ jsons = [ path for path in jsons_gen ]
7069
7170 for id_ , json_ in tqdm (enumerate (jsons , 1 )):
72- res = self ._sa_to_coco_single (id_ , json_ , id_generator )
71+ res = self ._sa_to_coco_single (id_ , json_ , id_generator , cat_id_map )
7372
74- panoptic_mask = json_ [:- len ('___pixel.json' )] + '.png'
73+ panoptic_mask = str ( json_ ) [:- len ('___pixel.json' )] + '.png'
7574
7675 Image .fromarray (id2rgb (res [2 ])).save (panoptic_mask )
7776
@@ -88,16 +87,15 @@ def sa_to_output_format(self):
8887 out_json ['images' ] = images
8988 json_data = json .dumps (out_json , indent = 4 )
9089 with open (
91- os .path .join (self .output_dir , '{}.json' .format (self .dataset_name )),
92- 'w+'
90+ self .output_dir / '{}.json' .format (self .dataset_name ), 'w+'
9391 ) as coco_json :
9492
9593 coco_json .write (json_data )
9694
9795 self .set_num_converted (len (jsons ))
9896
9997 def to_sa_format (self ):
100- json_data = os . path . join ( self .export_root , self .dataset_name + ".json" )
98+ json_data = self .export_root / ( self .dataset_name + ".json" )
10199 sa_classes = self ._create_sa_classes (json_data )
102100 sa_jsons = self .conversion_algorithm (json_data , self .output_dir )
103101 self .dump_output (sa_classes , sa_jsons )
@@ -137,7 +135,7 @@ def __setup_conversion_algorithm(self):
137135 def __str__ (self , ):
138136 return '{} object' .format (self .name )
139137
140- def _sa_to_coco_single (self , id_ , json_path , id_generator ):
138+ def _sa_to_coco_single (self , id_ , json_path , id_generator , cat_id_map ):
141139
142140 image_commons = self ._prepare_single_image_commons (id_ , json_path )
143141 annotations_per_image = []
@@ -166,23 +164,28 @@ def make_annotation(
166164 return annotation
167165
168166 res = self .conversion_algorithm (
169- make_annotation , image_commons , id_generator
167+ make_annotation , image_commons , id_generator , cat_id_map
170168 )
171169 return res
172170
173171 def sa_to_output_format (self ):
174172
175173 out_json = self ._create_skeleton ()
176174 out_json ['categories' ] = self ._create_categories (
177- os . path . join ( self .export_root , 'classes_mapper.json' )
175+ self .export_root / 'classes_mapper.json'
178176 )
177+
178+ cat_id_map = json .load (open (self .export_root / 'classes_mapper.json' ))
179+
179180 jsons = self ._load_sa_jsons ()
180181 images = []
181182 annotations = []
182183 id_generator = self ._make_id_generator ()
183184 for id_ , json_ in tqdm (enumerate (jsons )):
184185 try :
185- res = self ._sa_to_coco_single (id_ , json_ , id_generator )
186+ res = self ._sa_to_coco_single (
187+ id_ , json_ , id_generator , cat_id_map
188+ )
186189 except Exception as e :
187190 raise
188191 images .append (res [0 ])
@@ -195,16 +198,15 @@ def sa_to_output_format(self):
195198
196199 json_data = json .dumps (out_json , indent = 4 )
197200 with open (
198- os .path .join (self .output_dir , '{}.json' .format (self .dataset_name )),
199- 'w+'
201+ self .output_dir / '{}.json' .format (self .dataset_name ), 'w+'
200202 ) as coco_json :
201203 coco_json .write (json_data )
202204 print ("NUMBER OF IMAGES FAILED TO CONVERT" , self .failed_conversion_cnt )
203205
204206 self .set_num_converted (len (jsons ))
205207
206208 def to_sa_format (self ):
207- json_data = os . path . join ( self .export_root , self .dataset_name + ".json" )
209+ json_data = self .export_root / ( self .dataset_name + ".json" )
208210 sa_classes = self ._create_sa_classes (json_data )
209211 sa_jsons = self .conversion_algorithm (json_data , self .output_dir )
210212 self .dump_output (sa_classes , sa_jsons )
@@ -232,12 +234,21 @@ def __make_image_info(self, json_path, id_, source_type):
232234 elif source_type == 'Vector' :
233235 rm_len = len ('___objects.json' )
234236
235- image_path = json_path [:- rm_len ]
237+ img_width , img_height = 0 , 0
238+ json_data = json .load (open (json_path ))
239+ for annot in json_data :
240+ if 'type' in annot and annot ['type' ] == 'meta' :
241+ img_height = annot ['height' ]
242+ img_width = annot ['width' ]
243+
244+ image_path = str (json_path )[:- rm_len ]
245+
246+ if img_width == 0 and img_height == 0 :
247+ img_width , img_height = Image .open (image_path ).size
236248
237- img_width , img_height = Image .open (image_path ).size
238249 image_info = {
239250 'id' : id_ ,
240- 'file_name' : image_path [ len ( self . output_dir ):] ,
251+ 'file_name' : Path ( image_path ). name ,
241252 'height' : img_height ,
242253 'width' : img_width ,
243254 'license' : 1
@@ -266,15 +277,14 @@ def sa_to_output_format(self):
266277 json_data = json .dumps (out_json , indent = 4 )
267278
268279 with open (
269- os .path .join (self .output_dir , '{}.json' .format (self .dataset_name )),
270- 'w+'
280+ self .output_dir / '{}.json' .format (self .dataset_name ), 'w+'
271281 ) as coco_json :
272282 coco_json .write (json_data )
273283
274284 self .set_num_converted (len (out_json ['images' ]))
275285
276286 def to_sa_format (self ):
277- json_data = os . path . join ( self .export_root , self .dataset_name + ".json" )
287+ json_data = self .export_root / ( self .dataset_name + ".json" )
278288 sa_classes = self ._create_sa_classes (json_data )
279289 sa_jsons = self .conversion_algorithm (json_data , self .output_dir )
280290 self .dump_output (sa_classes , sa_jsons )
0 commit comments