The post process for Differentiable Binarization (DB).
| 49 | |
| 50 | |
| 51 | class DBPostProcess(object): |
| 52 | """ |
| 53 | The post process for Differentiable Binarization (DB). |
| 54 | """ |
| 55 | |
| 56 | def __init__(self, params): |
| 57 | self.thresh = params['thresh'] |
| 58 | self.box_thresh = params['box_thresh'] |
| 59 | self.max_candidates = params['max_candidates'] |
| 60 | self.unclip_ratio = params['unclip_ratio'] |
| 61 | self.min_size = 3 |
| 62 | |
| 63 | def boxes_from_bitmap(self, pred, _bitmap, dest_width, dest_height): |
| 64 | ''' |
| 65 | _bitmap: single map with shape (1, H, W), |
| 66 | whose values are binarized as {0, 1} |
| 67 | ''' |
| 68 | |
| 69 | bitmap = _bitmap |
| 70 | height, width = bitmap.shape |
| 71 | |
| 72 | outs = cv2.findContours((bitmap * 255).astype(np.uint8), cv2.RETR_LIST, |
| 73 | cv2.CHAIN_APPROX_SIMPLE) |
| 74 | if len(outs) == 3: |
| 75 | img, contours, _ = outs[0], outs[1], outs[2] |
| 76 | elif len(outs) == 2: |
| 77 | contours, _ = outs[0], outs[1] |
| 78 | |
| 79 | num_contours = min(len(contours), self.max_candidates) |
| 80 | boxes = np.zeros((num_contours, 4, 2), dtype=np.int16) |
| 81 | scores = np.zeros((num_contours, ), dtype=np.float32) |
| 82 | |
| 83 | for index in range(num_contours): |
| 84 | contour = contours[index] |
| 85 | points, sside = self.get_mini_boxes(contour) |
| 86 | if sside < self.min_size: |
| 87 | continue |
| 88 | points = np.array(points) |
| 89 | score = self.box_score_fast(pred, points.reshape(-1, 2)) |
| 90 | if self.box_thresh > score: |
| 91 | continue |
| 92 | |
| 93 | box = self.unclip(points).reshape(-1, 1, 2) |
| 94 | box, sside = self.get_mini_boxes(box) |
| 95 | if sside < self.min_size + 2: |
| 96 | continue |
| 97 | box = np.array(box) |
| 98 | if not isinstance(dest_width, int): |
| 99 | dest_width = dest_width.item() |
| 100 | dest_height = dest_height.item() |
| 101 | |
| 102 | box[:, 0] = np.clip( |
| 103 | np.round(box[:, 0] / width * dest_width), 0, dest_width) |
| 104 | box[:, 1] = np.clip( |
| 105 | np.round(box[:, 1] / height * dest_height), 0, dest_height) |
| 106 | boxes[index, :, :] = box.astype(np.int16) |
| 107 | scores[index] = score |
| 108 | return boxes, scores |
no outgoing calls
no test coverage detected