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