@@ -93,58 +93,56 @@ def from_pixel_to_vector(json_paths):
9393 mask_name = str (json_path ).replace ('___pixel.json' , '___save.png' )
9494 img = cv2 .imread (mask_name )
9595 H , W , C = img .shape
96- mask = np .zeros ((H , W ), dtype = np .uint8 )
9796
9897 sa_loader = []
9998 instances = json .load (open (json_path ))
10099 idx = 0
101- group_id_map = {}
102100 for instance in instances :
103- parts = instance ['parts' ]
104- if len (parts ) > 1 :
105- idx += 1
106- group_id = idx
107- else :
108- group_id = 0
101+ if 'parts' not in instance .keys ():
102+ if 'type' in instance .keys () and instance ['type' ] == 'meta' :
103+ sa_loader .append (instance )
104+ continue
109105
110- if group_id not in group_id_map .keys ():
111- group_id_map [group_id ] = []
106+ parts = instance ['parts' ]
112107
108+ polygons = []
113109 for part in parts :
114110 color = list (hex_to_rgb (part ['color' ]))
111+ mask = np .zeros ((H , W ), dtype = np .uint8 )
115112 mask [np .all ((img == color [::- 1 ]), axis = 2 )] = 255
116113 contours , _ = cv2 .findContours (
117114 mask , cv2 .RETR_EXTERNAL , cv2 .CHAIN_APPROX_SIMPLE
118115 )
119- if len (contours ) > 0 :
120- idx += 1
121- group_id = idx
122-
123- if group_id not in group_id_map .keys ():
124- group_id_map [group_id ] = []
125-
116+ part_polygons = []
126117 for contour in contours :
127118 segment = contour .flatten ().tolist ()
128- group_id_map [group_id ].append (segment )
119+ if len (segment ) > 6 :
120+ part_polygons .append (segment )
121+ polygons .append (part_polygons )
129122
130- temp = instance .copy ()
131- del temp ['parts' ]
132- temp ['pointLabels' ] = {}
133-
134- for key , value in group_id_map .items ():
135- for polygon in value :
136- temp ['groupId' ] = key
137- temp ['type' ] = 'polygon'
138- temp ['points' ] = polygon
139- sa_loader .append (temp .copy ())
140- temp ['type' ] = 'bbox'
141- temp ['points' ] = {
142- 'x1' : min (polygon [::2 ]),
143- 'x2' : max (polygon [::2 ]),
144- 'y1' : min (polygon [1 ::2 ]),
145- 'y2' : max (polygon [1 ::2 ])
146- }
147- sa_loader .append (temp .copy ())
123+ for part_polygons in polygons :
124+ if len (part_polygons ) > 1 :
125+ idx += 1
126+ group_id = idx
127+ else :
128+ group_id = 0
129+
130+ for polygon in part_polygons :
131+ temp = instance .copy ()
132+ del temp ['parts' ]
133+ temp ['pointLabels' ] = {}
134+ temp ['groupId' ] = group_id
135+ temp ['type' ] = 'polygon'
136+ temp ['points' ] = polygon
137+ sa_loader .append (temp .copy ())
138+ temp ['type' ] = 'bbox'
139+ temp ['points' ] = {
140+ 'x1' : min (polygon [::2 ]),
141+ 'x2' : max (polygon [::2 ]),
142+ 'y1' : min (polygon [1 ::2 ]),
143+ 'y2' : max (polygon [1 ::2 ])
144+ }
145+ sa_loader .append (temp .copy ())
148146
149147 sa_jsons [file_name ] = {'json' : sa_loader , 'mask' : None }
150148 return sa_jsons
@@ -168,6 +166,7 @@ def from_vector_to_pixel(json_paths):
168166 group_id_map = {}
169167 for idx , instance in enumerate (instances ):
170168 if instance ['type' ] == 'polygon' :
169+ temp = instance .copy ()
171170 pts = np .array (
172171 [
173172 instance ['points' ][2 * i :2 * (i + 1 )]
@@ -180,16 +179,17 @@ def from_vector_to_pixel(json_paths):
180179 group_id_map [instance ['groupId' ]].append (pts )
181180 else :
182181 group_id_map [instance ['groupId' ]] = [pts ]
182+ elif instance ['type' ] == 'meta' :
183+ sa_loader .append (instance )
183184
184- temp = instance .copy ()
185185 del temp ['type' ]
186186 del temp ['points' ]
187187 del temp ['pointLabels' ]
188188 del temp ['groupId' ]
189189
190- temp ['parts' ] = []
191190 idx = 0
192191 for key , values in group_id_map .items ():
192+ temp ['parts' ] = []
193193 if key == 0 :
194194 for polygon in values :
195195 bitmask = np .zeros ((H , W ))
0 commit comments