@@ -186,7 +186,7 @@ def postprocess(self, outputs: dict, meta: dict) -> ImageResultWithSoftPredictio
186186 def get_contours (
187187 self ,
188188 prediction : ImageResultWithSoftPrediction ,
189- ) -> list :
189+ ) -> list [ Contour ] :
190190 n_layers = prediction .soft_prediction .shape [2 ]
191191
192192 if n_layers == 1 :
@@ -207,13 +207,25 @@ def get_contours(
207207 obj_group = prediction .resultImage == layer_index
208208 label_index_map = obj_group .astype (np .uint8 ) * 255
209209
210- contours , _hierarchy = cv2 .findContours (
210+ contours , hierarchy = cv2 .findContours (
211211 label_index_map ,
212- cv2 .RETR_EXTERNAL ,
212+ cv2 .RETR_CCOMP ,
213213 cv2 .CHAIN_APPROX_NONE ,
214214 )
215+ hierarchy = hierarchy .squeeze ()
215216
216- for contour in contours :
217+ for i , contour in enumerate (contours ):
218+ children = None
219+
220+ if hierarchy [i ][3 ] >= 0 :
221+ continue
222+
223+ children = []
224+ if hierarchy [i ][2 ] >= 0 :
225+ child_next_idx = hierarchy [i ][2 ]
226+ while child_next_idx >= 0 :
227+ children .append (contours [child_next_idx ])
228+ child_next_idx = hierarchy [child_next_idx ][0 ]
217229 mask = np .zeros (prediction .resultImage .shape , dtype = np .uint8 )
218230 cv2 .drawContours (
219231 mask ,
@@ -223,7 +235,7 @@ def get_contours(
223235 thickness = - 1 ,
224236 )
225237 probability = cv2 .mean (current_label_soft_prediction , mask )[0 ]
226- combined_contours .append (Contour (label , probability , contour ))
238+ combined_contours .append (Contour (label , probability , contour , children ))
227239
228240 return combined_contours
229241
0 commit comments