@@ -209,41 +209,30 @@ def generate_json(self, img_list, save_dir):
209209 points = np .array (shape .get ("points" ))
210210
211211 if shape_type == RECTANGLE :
212+ seg_points = [np .ravel (points , order = "C" ).tolist ()]
213+
212214 x0 , y0 = np .min (points , axis = 0 )
213215 x1 , y1 = np .max (points , axis = 0 )
214216 w , h = x1 - x1 , y1 - y0
217+ bbox_points = [x0 , y0 , w , h ]
215218 area = w * h
216219
217- seg_points = [np .ravel (points , order = "C" ).tolist ()]
218-
219- one_anno_dict = {
220- "segmentation" : seg_points ,
221- "area" : area ,
222- "iscrowd" : 0 ,
223- "image_id" : img_id ,
224- "bbox" : [x0 , y0 , w , h ],
225- "category_id" : label_id ,
226- "id" : self .object_id ,
227- }
228220 elif shape_type == POLYGON :
229- mask = np .zeros ((img_h , img_w ), dtype = "uint8" )
230- img_mask = cv2 .fillPoly (mask , np .int32 ([points ]), 255 )
231- contours , _ = cv2 .findContours (
232- img_mask , cv2 .RETR_LIST , cv2 .CHAIN_APPROX_SIMPLE
233- )
234- contour = contours [0 ]
235- bbox_points = self .get_mini_boxes (contour )
236- area = cv2 .contourArea (contour )
237-
238- one_anno_dict = {
239- "segmentation" : points .tolist (),
240- "area" : area ,
241- "iscrowd" : 0 ,
242- "image_id" : img_id ,
243- "bbox" : bbox_points ,
244- "category_id" : label_id ,
245- "id" : self .object_id ,
246- }
221+ seg_points = points .tolist ()
222+ bbox_points , area = self .cvt_poly_to_rect (img_h , img_w , points )
223+ else :
224+ print (f"Current { shape_type } is not supported!" )
225+ continue
226+
227+ one_anno_dict = {
228+ "segmentation" : seg_points ,
229+ "area" : area ,
230+ "iscrowd" : 0 ,
231+ "image_id" : img_id ,
232+ "bbox" : bbox_points ,
233+ "category_id" : label_id ,
234+ "id" : self .object_id ,
235+ }
247236
248237 anno_list .append (one_anno_dict )
249238 self .object_id += 1
@@ -276,10 +265,14 @@ def cp_file(self, file_path: Path, dst_dir: Path):
276265
277266 shutil .copy2 (str (file_path ), dst_dir )
278267
279- def convert_polygon_to_rectangle (
280- self ,
281- ):
282- pass
268+ def cvt_poly_to_rect (self , img_h : int , img_w : int , points ):
269+ mask = np .zeros ((img_h , img_w ), dtype = "uint8" )
270+ img_mask = cv2 .fillPoly (mask , np .int32 ([points ]), 255 )
271+ contours , _ = cv2 .findContours (img_mask , cv2 .RETR_LIST , cv2 .CHAIN_APPROX_SIMPLE )
272+ contour = contours [0 ]
273+ bbox_points = self .get_mini_boxes (contour )
274+ area = cv2 .contourArea (contour )
275+ return bbox_points , area
283276
284277 @staticmethod
285278 def get_mini_boxes (contour ) -> List [int ]:
0 commit comments