@@ -163,51 +163,60 @@ def from_vector_to_pixel(json_paths):
163163
164164 instances = json .load (open (json_path ))
165165 blue_colors = blue_color_generator (len (instances ))
166- group_id_map = {}
166+ instances_group = {}
167167 for idx , instance in enumerate (instances ):
168168 if instance ['type' ] == 'polygon' :
169- temp = instance .copy ()
170- pts = np .array (
171- [
172- instance ['points' ][2 * i :2 * (i + 1 )]
173- for i in range (len (instance ['points' ]) // 2 )
174- ],
175- dtype = np .int32
176- )
177-
178- if instance ['groupId' ] in group_id_map .keys ():
179- group_id_map [instance ['groupId' ]].append (pts )
169+ if instance ['groupId' ] in instances_group .keys ():
170+ instances_group [instance ['groupId' ]].append (instance )
180171 else :
181- group_id_map [instance ['groupId' ]] = [pts ]
172+ instances_group [instance ['groupId' ]] = [instance ]
182173 elif instance ['type' ] == 'meta' :
183174 sa_loader .append (instance )
184175
185- del temp ['type' ]
186- del temp ['points' ]
187- del temp ['pointLabels' ]
188- del temp ['groupId' ]
189-
190176 idx = 0
191- for key , values in group_id_map .items ():
192- temp ['parts' ] = []
177+ for key , instances in instances_group .items ():
193178 if key == 0 :
194- for polygon in values :
179+ for instance in instances :
180+ pts = np .array (
181+ [
182+ instance ['points' ][2 * i :2 * (i + 1 )]
183+ for i in range (len (instance ['points' ]) // 2 )
184+ ],
185+ dtype = np .int32
186+ )
195187 bitmask = np .zeros ((H , W ))
196- cv2 .fillPoly (bitmask , [polygon ], 1 )
188+ cv2 .fillPoly (bitmask , [pts ], 1 )
197189 mask [bitmask == 1
198190 ] = list (hex_to_rgb (blue_colors [idx ]))[::- 1 ] + [255 ]
199- temp ['parts' ].append ({'color' : blue_colors [idx ]})
200- sa_loader .append (temp .copy ())
191+ del instance ['type' ]
192+ del instance ['points' ]
193+ del instance ['pointLabels' ]
194+ del instance ['groupId' ]
195+ instance ['parts' ] = [{'color' : blue_colors [idx ]}]
196+ sa_loader .append (instance .copy ())
201197 idx += 1
202198 else :
203- for polygon in values :
199+ parts = []
200+ for instance in instances :
201+ pts = np .array (
202+ [
203+ instance ['points' ][2 * i :2 * (i + 1 )]
204+ for i in range (len (instance ['points' ]) // 2 )
205+ ],
206+ dtype = np .int32
207+ )
204208 bitmask = np .zeros ((H , W ))
205- cv2 .fillPoly (bitmask , [polygon ], 1 )
209+ cv2 .fillPoly (bitmask , [pts ], 1 )
206210 mask [bitmask == 1
207211 ] = list (hex_to_rgb (blue_colors [idx ]))[::- 1 ] + [255 ]
208- temp [ ' parts' ] .append ({'color' : blue_colors [idx ]})
212+ parts .append ({'color' : blue_colors [idx ]})
209213 idx += 1
210- sa_loader .append (temp .copy ())
214+ del instance ['type' ]
215+ del instance ['points' ]
216+ del instance ['pointLabels' ]
217+ del instance ['groupId' ]
218+ instance ['parts' ] = parts
219+ sa_loader .append (instance .copy ())
211220
212221 sa_jsons [file_name ] = {'json' : sa_loader , 'mask' : mask }
213222
0 commit comments