_bitmap: single map with shape (1, H, W), whose values are binarized as {0, 1}
(self, pred, _bitmap, dest_width, dest_height)
| 89 | return boxes, scores |
| 90 | |
| 91 | def boxes_from_bitmap(self, pred, _bitmap, dest_width, dest_height): |
| 92 | """ |
| 93 | _bitmap: single map with shape (1, H, W), |
| 94 | whose values are binarized as {0, 1} |
| 95 | """ |
| 96 | |
| 97 | bitmap = _bitmap |
| 98 | height, width = bitmap.shape |
| 99 | |
| 100 | outs = cv2.findContours((bitmap * 255).astype(np.uint8), cv2.RETR_LIST, |
| 101 | cv2.CHAIN_APPROX_SIMPLE) |
| 102 | if len(outs) == 3: |
| 103 | img, contours, _ = outs[0], outs[1], outs[2] |
| 104 | elif len(outs) == 2: |
| 105 | contours, _ = outs[0], outs[1] |
| 106 | |
| 107 | num_contours = min(len(contours), self.max_candidates) |
| 108 | |
| 109 | boxes = [] |
| 110 | scores = [] |
| 111 | for index in range(num_contours): |
| 112 | contour = contours[index] |
| 113 | points, sside = self.get_mini_boxes(contour) |
| 114 | if sside < self.min_size: |
| 115 | continue |
| 116 | points = np.array(points) |
| 117 | if self.score_mode == 'fast': |
| 118 | score = self.box_score_fast(pred, points.reshape(-1, 2)) |
| 119 | else: |
| 120 | score = self.box_score_slow(pred, contour) |
| 121 | if self.box_thresh > score: |
| 122 | continue |
| 123 | |
| 124 | box = self.unclip(points, self.unclip_ratio) |
| 125 | if len(box) > 1: |
| 126 | continue |
| 127 | box = np.array(box).reshape(-1, 1, 2) |
| 128 | box, sside = self.get_mini_boxes(box) |
| 129 | if sside < self.min_size + 2: |
| 130 | continue |
| 131 | box = np.array(box) |
| 132 | |
| 133 | box[:, 0] = np.clip(np.round(box[:, 0] / width * dest_width), 0, |
| 134 | dest_width) |
| 135 | box[:, 1] = np.clip(np.round(box[:, 1] / height * dest_height), 0, |
| 136 | dest_height) |
| 137 | boxes.append(box.astype('int32')) |
| 138 | scores.append(score) |
| 139 | return np.array(boxes, dtype='int32'), scores |
| 140 | |
| 141 | def unclip(self, box, unclip_ratio): |
| 142 | poly = Polygon(box) |
no test coverage detected