def detect_contours(self, bboxs, final_pred): bbox_contours = list() bbox_list = list() for idx in range(0, int(np.max(final_pred)) + 1): fg = np.where(final_pred == idx) boxes = bboxs[fg, :8].reshape((-1, 4, 2)).astype(np.int32) MBOX = boxes boundary_point = None if boxes.shape[0] > 1: center = np.mean(boxes, axis=1).astype(np.int32).tolist() paths, routes_path = minConnectPath(center) boxes = boxes[routes_path] top = np.mean(boxes[:, 0:2, :], axis=1).astype(np.int32).tolist() bot = np.mean(boxes[:, 2:4, :], axis=1).astype(np.int32).tolist() edge0 = self.select_edge(top + bot[::-1], boxes[0]) edge1 = self.select_edge(top + bot[::-1], boxes[-1]) if edge0 is not None: top.insert(0, edge0[0]) bot.insert(0, edge0[1]) if edge1 is not None: top.append(edge1[0]) bot.append(edge1[1]) boundary_point = np.array(top + bot[::-1]) elif boxes.shape[0] == 1: top = boxes[0, 0:2, :].astype(np.int32).tolist() bot = boxes[0, 2:4:-1, :].astype(np.int32).tolist() boundary_point = np.array(top + bot) if boundary_point is None: continue try: bbox_contours.append( [boundary_point, np.array(np.stack([top, bot], axis=1))]) bbox_list.append(MBOX) except: continue return bbox_contours, bbox_list
t0 = time.time() for bbox_idx in range(1, ret): bbox_mask = labels == bbox_idx text_map = tcl_mask[:, :, 0] * bbox_mask boxes = bbox_transfor_inv(radius_map, sin_map, cos_map, text_map, wclip=(2, 8)) # nms boxes = lanms.merge_quadrangle_n9(boxes.astype('float32'), 0.25) boxes = boxes[:, :8].reshape((-1, 4, 2)).astype(np.int32) if boxes.shape[0] > 1: center = np.mean(boxes, axis=1).astype(np.int32).tolist() paths, routes_path = minConnectPath(center) boxes = boxes[routes_path] top = np.mean(boxes[:, 0:2, :], axis=1).astype(np.int32).tolist() bot = np.mean(boxes[:, 2:4, :], axis=1).astype(np.int32).tolist() boundary_point = top + bot[::-1] # for index in routes: for ip, pp in enumerate(top): if ip == 0: color = (0, 255, 255) elif ip == len(top) - 1: color = (255, 255, 0) else:
def detect_contours(self, tr_pred, tcl_pred, sin_pred, cos_pred, radii_pred): # thresholding tr_pred_mask = tr_pred > self.tr_thresh tcl_pred_mask = tcl_pred > self.tcl_thresh # multiply TR and TCL tcl_mask = tcl_pred_mask * tr_pred_mask # regularize sin_pred, cos_pred = regularize_sin_cos(sin_pred, cos_pred) # find disjoint regions tcl_mask = fill_hole(tcl_mask) tcl_contours, _ = cv2.findContours(tcl_mask.astype(np.uint8), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) mask = np.zeros_like(tcl_mask) bbox_contours = list() for cont in tcl_contours: deal_map = mask.copy() cv2.drawContours(deal_map, [cont], -1, 1, -1) if deal_map.sum() <= 100: continue text_map = tr_pred * deal_map bboxs = self.bbox_transfor_inv(radii_pred, sin_pred, cos_pred, text_map, wclip=(4, 12)) # nms boxes = lanms.merge_quadrangle_n9(bboxs.astype('float32'), 0.25) boxes = boxes[:, :8].reshape((-1, 4, 2)).astype(np.int32) boundary_point = None if boxes.shape[0] > 1: center = np.mean(boxes, axis=1).astype(np.int32).tolist() paths, routes_path = minConnectPath(center) boxes = boxes[routes_path] top = np.mean(boxes[:, 0:2, :], axis=1).astype(np.int32).tolist() bot = np.mean(boxes[:, 2:4, :], axis=1).astype(np.int32).tolist() edge0 = self.select_edge(top + bot[::-1], boxes[0]) edge1 = self.select_edge(top + bot[::-1], boxes[-1]) if edge0 is not None: top.insert(0, edge0[0]) bot.insert(0, edge0[1]) if edge1 is not None: top.append(edge1[0]) bot.append(edge1[1]) boundary_point = np.array(top + bot[::-1]) elif boxes.shape[0] == 1: top = boxes[0, 0:2, :].astype(np.int32).tolist() bot = boxes[0, 2:4:-1, :].astype(np.int32).tolist() boundary_point = np.array(top + bot) if boundary_point is None: continue reconstruct_mask = mask.copy() cv2.drawContours(reconstruct_mask, [boundary_point], -1, 1, -1) if (reconstruct_mask * tr_pred_mask).sum() < reconstruct_mask.sum() * 0.5: continue # if reconstruct_mask.sum() < 200: # continue rect = cv2.minAreaRect(boundary_point) if min(rect[1][0], rect[1][1]) < 10 or rect[1][0] * rect[1][1] < 300: continue bbox_contours.append([boundary_point, np.array(np.stack([top, bot], axis=1))]) return bbox_contours