_bitmap: single map with shape (1, H, W), whose values are binarized as {0, 1}
(self, pred, _bitmap, dest_width, dest_height)
| 40 | [1, 1]]) |
| 41 | |
| 42 | def polygons_from_bitmap(self, pred, _bitmap, dest_width, dest_height): |
| 43 | """ |
| 44 | _bitmap: single map with shape (1, H, W), |
| 45 | whose values are binarized as {0, 1} |
| 46 | """ |
| 47 | |
| 48 | bitmap = _bitmap |
| 49 | height, width = bitmap.shape |
| 50 | |
| 51 | boxes = [] |
| 52 | scores = [] |
| 53 | |
| 54 | contours, _ = cv2.findContours((bitmap * 255).astype(np.uint8), |
| 55 | cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) |
| 56 | |
| 57 | for contour in contours[:self.max_candidates]: |
| 58 | epsilon = 0.002 * cv2.arcLength(contour, True) |
| 59 | approx = cv2.approxPolyDP(contour, epsilon, True) |
| 60 | points = approx.reshape((-1, 2)) |
| 61 | if points.shape[0] < 4: |
| 62 | continue |
| 63 | |
| 64 | score = self.box_score_fast(pred, points.reshape(-1, 2)) |
| 65 | if self.box_thresh > score: |
| 66 | continue |
| 67 | |
| 68 | if points.shape[0] > 2: |
| 69 | box = self.unclip(points, self.unclip_ratio) |
| 70 | if len(box) > 1: |
| 71 | continue |
| 72 | else: |
| 73 | continue |
| 74 | box = np.array(box).reshape(-1, 2) |
| 75 | if len(box) == 0: |
| 76 | continue |
| 77 | |
| 78 | _, sside = self.get_mini_boxes(box.reshape((-1, 1, 2))) |
| 79 | if sside < self.min_size + 2: |
| 80 | continue |
| 81 | |
| 82 | box = np.array(box) |
| 83 | box[:, 0] = np.clip(np.round(box[:, 0] / width * dest_width), 0, |
| 84 | dest_width) |
| 85 | box[:, 1] = np.clip(np.round(box[:, 1] / height * dest_height), 0, |
| 86 | dest_height) |
| 87 | boxes.append(box.tolist()) |
| 88 | scores.append(score) |
| 89 | return boxes, scores |
| 90 | |
| 91 | def boxes_from_bitmap(self, pred, _bitmap, dest_width, dest_height): |
| 92 | """ |
no test coverage detected