22import json
33import logging
44import shutil
5+ from pathlib import Path
56
67import cv2
78import numpy as np
@@ -23,12 +24,13 @@ def from_pixel_to_vector(json_paths, output_dir):
2324 img_names = []
2425
2526 for json_path in json_paths :
26- file_name = str (json_path .name ).replace ("___pixel.json" , "___objects.json" )
27-
28- mask_name = str (json_path ).replace ("___pixel.json" , "___save.png" )
27+ file_name = str (json_path )
28+ pixel_postfix = "___pixel.json"
29+ postfix = pixel_postfix if file_name .endswith (pixel_postfix ) else ".json"
30+ mask_name = file_name .replace (postfix , "___save.png" )
2931 img = cv2 .imread (mask_name )
3032 H , W , _ = img .shape
31- sa_json = json .load (open (json_path ))
33+ sa_json = json .load (open (file_name ))
3234 instances = sa_json ["instances" ]
3335 new_instances = []
3436 global_idx = itertools .count ()
@@ -78,21 +80,24 @@ def from_pixel_to_vector(json_paths, output_dir):
7880 new_instances .append (temp )
7981
8082 sa_json ["instances" ] = new_instances
81- write_to_json (output_dir / file_name , sa_json )
82- img_names .append (file_name .replace ("___objects.json" , "" ))
83+ write_to_json (
84+ str (output_dir / Path (file_name ).name .replace (postfix , ".json" )), sa_json
85+ )
86+ img_names .append (file_name .replace (postfix , "" ))
8387 return img_names
8488
8589
8690def from_vector_to_pixel (json_paths , output_dir ):
8791 img_names = []
8892 for json_path in json_paths :
89- file_name = str (json_path .name ).replace ("___objects.json" , "___pixel.json" )
90-
91- img_name = str (json_path ).replace ("___objects.json" , "" )
93+ file_name = str (json_path )
94+ vector_postfix = "___objects.json"
95+ postfix = vector_postfix if file_name .endswith (vector_postfix ) else ".json"
96+ img_name = file_name .replace (postfix , "" )
9297 img = cv2 .imread (img_name )
9398 H , W , _ = img .shape
9499
95- sa_json = json .load (open (json_path ))
100+ sa_json = json .load (open (file_name ))
96101 instances = sa_json ["instances" ]
97102 mask = np .zeros ((H , W , 4 ))
98103
@@ -155,16 +160,18 @@ def from_vector_to_pixel(json_paths, output_dir):
155160 instance ["parts" ] = parts
156161 sa_instances .append (instance .copy ())
157162
158- mask_name = file_name .replace ("___pixel.json" , "___save.png" )
159- cv2 .imwrite (str (output_dir .joinpath (mask_name )), mask )
163+ mask_name = file_name .replace (postfix , "___save.png" )
164+ cv2 .imwrite (str (output_dir .joinpath (Path ( mask_name ). name )), mask )
160165
161166 sa_json ["instances" ] = sa_instances
162- write_to_json (output_dir / file_name , sa_json )
163- img_names .append (file_name .replace ("___pixel.json" , "" ))
167+ write_to_json (
168+ str (output_dir / Path (file_name ).name .replace (postfix , ".json" )), sa_json
169+ )
170+ img_names .append (img_name .replace (".json" , "" ))
164171 return img_names
165172
166173
167- def sa_convert_project_type (input_dir , output_dir ):
174+ def sa_convert_project_type (input_dir , output_dir , convert_to ):
168175 json_paths = list (input_dir .glob ("*.json" ))
169176
170177 output_dir .joinpath ("classes" ).mkdir (parents = True )
@@ -173,158 +180,12 @@ def sa_convert_project_type(input_dir, output_dir):
173180 output_dir .joinpath ("classes" , "classes.json" ),
174181 )
175182
176- if "___pixel.json" in json_paths [ 0 ]. name :
183+ if convert_to == "Vector" :
177184 img_names = from_pixel_to_vector (json_paths , output_dir )
178- elif "___objects.json" in json_paths [ 0 ]. name :
185+ elif convert_to == "Pixel" :
179186 img_names = from_vector_to_pixel (json_paths , output_dir )
180- elif ".json" in json_paths [0 ].name :
181- raise AppException (DEPRICATED_DOCUMENT_VIDEO_MESSAGE )
182187 else :
183- raise AppException (
184- "'input_dir' should contain JSON files with '[IMAGE_NAME]___objects.json' or '[IMAGE_NAME]___pixel.json' names structure." ,
185- )
188+ raise AppException (DEPRICATED_DOCUMENT_VIDEO_MESSAGE )
186189
187190 for img_name in img_names :
188- copy_file (input_dir .joinpath (img_name ), output_dir .joinpath (img_name ))
189-
190-
191- def upgrade_json (input_dir , output_dir ):
192- files_list = list (input_dir .glob ("*.json" ))
193- ptype = "Vector"
194- if "___pixel" in str (files_list [0 ].name ):
195- ptype = "Pixel"
196-
197- converted_files = []
198- failed_files = []
199- for file in files_list :
200- file_name = file .name
201- try :
202- output_json = _update_json_format (file , ptype )
203- converted_files .append (file_name )
204- write_to_json (output_dir / file_name , output_json )
205- except Exception as e :
206- logger .debug (str (e ), exc_info = True )
207- failed_files .append (file_name )
208-
209- return converted_files
210-
211-
212- def degrade_json (input_dir , output_dir ):
213- files_list = list (input_dir .glob ("*.json" ))
214-
215- converted_files = []
216- failed_files = []
217- for file in files_list :
218- file_name = file .name
219- try :
220- output_json = _degrade_json_format (file )
221- converted_files .append (output_dir / file_name )
222- write_to_json (output_dir / file_name , output_json )
223- except Exception as e :
224- failed_files .append (file_name )
225-
226- return converted_files
227-
228-
229- def _update_json_format (old_json_path , project_type ):
230- old_json_data = json .load (open (old_json_path ))
231- new_json_data = {"metadata" : {}, "instances" : [], "tags" : [], "comments" : []}
232-
233- meta_keys = [
234- "name" ,
235- "width" ,
236- "height" ,
237- "status" ,
238- "pinned" ,
239- "isPredicted" ,
240- "projectId" ,
241- "annotatorEmail" ,
242- "qaEmail" ,
243- ]
244- if project_type == "Pixel" :
245- meta_keys .append ("isSegmented" )
246-
247- new_json_data ["metadata" ] = dict .fromkeys (meta_keys )
248-
249- suffix = "___objects.json" if project_type == "Vector" else "___pixel.json"
250- image_name = str (old_json_path .name ).split (suffix )[0 ]
251- metadata = new_json_data ["metadata" ]
252- metadata ["name" ] = image_name
253-
254- for item in old_json_data :
255- object_type = item .get ("type" )
256- if object_type == "meta" :
257- meta_name = item ["name" ]
258- if meta_name == "imageAttributes" :
259- metadata ["height" ] = item .get ("height" )
260- metadata ["width" ] = item .get ("width" )
261- metadata ["status" ] = item .get ("status" )
262- metadata ["pinned" ] = item .get ("pinned" )
263- if meta_name == "lastAction" :
264- metadata ["lastAction" ] = dict .fromkeys (["email" , "timestamp" ])
265- metadata ["lastAction" ]["email" ] = item .get ("userId" )
266- metadata ["lastAction" ]["timestamp" ] = item .get ("timestamp" )
267- elif object_type == "tag" :
268- new_json_data ["tags" ].append (item .get ("name" ))
269- elif object_type == "comment" :
270- item .pop ("type" )
271- item ["correspondence" ] = item ["comments" ]
272- for comment in item ["correspondence" ]:
273- comment ["email" ] = comment ["id" ]
274- comment .pop ("id" )
275- item .pop ("comments" )
276- new_json_data ["comments" ].append (item )
277- else :
278- new_json_data ["instances" ].append (item )
279-
280- return new_json_data
281-
282-
283- def _degrade_json_format (new_json_path ):
284- sa_loader = []
285- new_json_data = json .load (open (new_json_path ))
286-
287- meta = {"type" : "meta" , "name" : "imageAttributes" }
288- meta_keys = ["height" , "width" , "status" , "pinned" ]
289- for meta_key in meta_keys :
290- if meta_key in new_json_data ["metadata" ]:
291- meta [meta_key ] = new_json_data ["metadata" ][meta_key ]
292- sa_loader .append (meta )
293-
294- if "lastAction" in new_json_data ["metadata" ]:
295- meta = {
296- "type" : "meta" ,
297- "name" : "lastAction" ,
298- "userId" : new_json_data ["metadata" ]["lastAction" ]["email" ],
299- "timestamp" : new_json_data ["metadata" ]["lastAction" ]["timestamp" ],
300- }
301- sa_loader .append (meta )
302-
303- for item in new_json_data ["instances" ]:
304- sa_loader .append (item )
305-
306- for item in new_json_data ["comments" ]:
307- comments = []
308- for item2 in item ["correspondence" ]:
309- comments .append ({"text" : item2 ["text" ], "id" : item2 ["email" ]})
310- item ["comments" ] = comments
311- item ["createdAt" ] = item ["correspondence" ][0 ]["timestamp" ]
312- item ["createdBy" ] = {
313- "email" : item ["correspondence" ][0 ]["email" ],
314- "role" : item ["correspondence" ][0 ]["role" ],
315- }
316- item ["updatedAt" ] = item ["correspondence" ][- 1 ]["timestamp" ]
317- item ["updatedBy" ] = {
318- "email" : item ["correspondence" ][- 1 ]["email" ],
319- "role" : item ["correspondence" ][- 1 ]["role" ],
320- }
321- item .pop ("correspondence" )
322- item ["type" ] = "comment"
323- item ["comments" ] = comments
324- sa_loader .append (item )
325-
326- for item in new_json_data ["tags" ]:
327- tag = {"type" : "tag" , "name" : item }
328- sa_loader .append (tag )
329-
330- return sa_loader
191+ copy_file (img_name , output_dir / Path (img_name ).name )
0 commit comments