|
@@ -77,6 +77,7 @@ class DBPostProcess(object):
|
|
continue
|
|
continue
|
|
|
|
|
|
score = self.box_score_fast(pred, points.reshape(-1, 2))
|
|
score = self.box_score_fast(pred, points.reshape(-1, 2))
|
|
|
|
+
|
|
if self.box_thresh > score:
|
|
if self.box_thresh > score:
|
|
continue
|
|
continue
|
|
|
|
|
|
@@ -101,7 +102,7 @@ class DBPostProcess(object):
|
|
scores.append(score)
|
|
scores.append(score)
|
|
return boxes, scores
|
|
return boxes, scores
|
|
|
|
|
|
- def boxes_from_bitmap(self, pred, _bitmap, dest_width, dest_height):
|
|
|
|
|
|
+ def boxes_from_bitmap(self, pred, _bitmap,classes, dest_width, dest_height):
|
|
'''
|
|
'''
|
|
_bitmap: single map with shape (1, H, W),
|
|
_bitmap: single map with shape (1, H, W),
|
|
whose values are binarized as {0, 1}
|
|
whose values are binarized as {0, 1}
|
|
@@ -118,9 +119,11 @@ class DBPostProcess(object):
|
|
contours, _ = outs[0], outs[1]
|
|
contours, _ = outs[0], outs[1]
|
|
|
|
|
|
num_contours = min(len(contours), self.max_candidates)
|
|
num_contours = min(len(contours), self.max_candidates)
|
|
-
|
|
|
|
|
|
+
|
|
boxes = []
|
|
boxes = []
|
|
scores = []
|
|
scores = []
|
|
|
|
+ class_indexes = []
|
|
|
|
+ class_scores = []
|
|
for index in range(num_contours):
|
|
for index in range(num_contours):
|
|
contour = contours[index]
|
|
contour = contours[index]
|
|
points, sside = self.get_mini_boxes(contour)
|
|
points, sside = self.get_mini_boxes(contour)
|
|
@@ -128,9 +131,12 @@ class DBPostProcess(object):
|
|
continue
|
|
continue
|
|
points = np.array(points)
|
|
points = np.array(points)
|
|
if self.score_mode == "fast":
|
|
if self.score_mode == "fast":
|
|
- score = self.box_score_fast(pred, points.reshape(-1, 2))
|
|
|
|
|
|
+ score, class_index, class_score = self.box_score_fast(pred, points.reshape(-1, 2), classes)
|
|
else:
|
|
else:
|
|
- score = self.box_score_slow(pred, contour)
|
|
|
|
|
|
+ score, class_index, class_score = self.box_score_slow(pred, contour, classes)
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ print("origin score:" + str(score))
|
|
if self.box_thresh > score:
|
|
if self.box_thresh > score:
|
|
continue
|
|
continue
|
|
|
|
|
|
@@ -146,7 +152,15 @@ class DBPostProcess(object):
|
|
np.round(box[:, 1] / height * dest_height), 0, dest_height)
|
|
np.round(box[:, 1] / height * dest_height), 0, dest_height)
|
|
boxes.append(box.astype("int32"))
|
|
boxes.append(box.astype("int32"))
|
|
scores.append(score)
|
|
scores.append(score)
|
|
- return np.array(boxes, dtype="int32"), scores
|
|
|
|
|
|
+
|
|
|
|
+ class_indexes.append(class_index)
|
|
|
|
+ class_scores.append(class_score)
|
|
|
|
+
|
|
|
|
+ if classes is None:
|
|
|
|
+ return np.array(boxes, dtype="int32"), scores
|
|
|
|
+ else:
|
|
|
|
+ return np.array(boxes, dtype="int32"), scores, class_indexes, class_scores
|
|
|
|
+
|
|
|
|
|
|
def unclip(self, box, unclip_ratio):
|
|
def unclip(self, box, unclip_ratio):
|
|
poly = Polygon(box)
|
|
poly = Polygon(box)
|
|
@@ -179,24 +193,53 @@ class DBPostProcess(object):
|
|
]
|
|
]
|
|
return box, min(bounding_box[1])
|
|
return box, min(bounding_box[1])
|
|
|
|
|
|
- def box_score_fast(self, bitmap, _box):
|
|
|
|
|
|
+ def box_score_fast(self, bitmap, _box,classes):
|
|
'''
|
|
'''
|
|
box_score_fast: use bbox mean score as the mean score
|
|
box_score_fast: use bbox mean score as the mean score
|
|
'''
|
|
'''
|
|
|
|
+ # print(classes)
|
|
h, w = bitmap.shape[:2]
|
|
h, w = bitmap.shape[:2]
|
|
box = _box.copy()
|
|
box = _box.copy()
|
|
|
|
+
|
|
xmin = np.clip(np.floor(box[:, 0].min()).astype("int32"), 0, w - 1)
|
|
xmin = np.clip(np.floor(box[:, 0].min()).astype("int32"), 0, w - 1)
|
|
xmax = np.clip(np.ceil(box[:, 0].max()).astype("int32"), 0, w - 1)
|
|
xmax = np.clip(np.ceil(box[:, 0].max()).astype("int32"), 0, w - 1)
|
|
ymin = np.clip(np.floor(box[:, 1].min()).astype("int32"), 0, h - 1)
|
|
ymin = np.clip(np.floor(box[:, 1].min()).astype("int32"), 0, h - 1)
|
|
ymax = np.clip(np.ceil(box[:, 1].max()).astype("int32"), 0, h - 1)
|
|
ymax = np.clip(np.ceil(box[:, 1].max()).astype("int32"), 0, h - 1)
|
|
|
|
|
|
|
|
+ # box__ = box.reshape(1, -1, 2)
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
mask = np.zeros((ymax - ymin + 1, xmax - xmin + 1), dtype=np.uint8)
|
|
mask = np.zeros((ymax - ymin + 1, xmax - xmin + 1), dtype=np.uint8)
|
|
box[:, 0] = box[:, 0] - xmin
|
|
box[:, 0] = box[:, 0] - xmin
|
|
box[:, 1] = box[:, 1] - ymin
|
|
box[:, 1] = box[:, 1] - ymin
|
|
cv2.fillPoly(mask, box.reshape(1, -1, 2).astype("int32"), 1)
|
|
cv2.fillPoly(mask, box.reshape(1, -1, 2).astype("int32"), 1)
|
|
- return cv2.mean(bitmap[ymin:ymax + 1, xmin:xmax + 1], mask)[0]
|
|
|
|
|
|
+ if classes is None:
|
|
|
|
+ return cv2.mean(bitmap[ymin:ymax + 1, xmin:xmax + 1], mask)[0], None, None
|
|
|
|
+ else:
|
|
|
|
+ k = 255
|
|
|
|
+ class_mask = np.full((ymax - ymin + 1, xmax - xmin + 1), k, dtype=np.int32)
|
|
|
|
+
|
|
|
|
+ cv2.fillPoly(class_mask, box.reshape(1, -1, 2).astype(np.int32), 0)
|
|
|
|
+ classes = classes[ymin:ymax + 1, xmin:xmax + 1]
|
|
|
|
+
|
|
|
|
+ new_classes = classes + class_mask
|
|
|
|
+
|
|
|
|
+ # 拉平
|
|
|
|
+ a = new_classes.reshape(-1)
|
|
|
|
+ b = np.where(a >= k)
|
|
|
|
+ # print(len(b[0].tolist()))
|
|
|
|
+ classes = np.delete(a, b[0].tolist())
|
|
|
|
+
|
|
|
|
+ class_index = np.argmax(np.bincount(classes))
|
|
|
|
+ print(class_index)
|
|
|
|
+ class_score = np.sum(classes == class_index) / len(classes)
|
|
|
|
+ print(class_score)
|
|
|
|
+ return cv2.mean(bitmap[ymin:ymax + 1, xmin:xmax + 1], mask)[0], class_index, class_score
|
|
|
|
+
|
|
|
|
+ def box_score_slow(self, bitmap, contour,classes):
|
|
|
|
|
|
- def box_score_slow(self, bitmap, contour):
|
|
|
|
'''
|
|
'''
|
|
box_score_slow: use polyon mean score as the mean score
|
|
box_score_slow: use polyon mean score as the mean score
|
|
'''
|
|
'''
|
|
@@ -215,7 +258,27 @@ class DBPostProcess(object):
|
|
contour[:, 1] = contour[:, 1] - ymin
|
|
contour[:, 1] = contour[:, 1] - ymin
|
|
|
|
|
|
cv2.fillPoly(mask, contour.reshape(1, -1, 2).astype("int32"), 1)
|
|
cv2.fillPoly(mask, contour.reshape(1, -1, 2).astype("int32"), 1)
|
|
- return cv2.mean(bitmap[ymin:ymax + 1, xmin:xmax + 1], mask)[0]
|
|
|
|
|
|
+ if classes is None:
|
|
|
|
+ return cv2.mean(bitmap[ymin:ymax + 1, xmin:xmax + 1], mask)[0], None, None
|
|
|
|
+ else:
|
|
|
|
+ k = 999
|
|
|
|
+ class_mask = np.full((ymax - ymin + 1, xmax - xmin + 1), k, dtype=np.int32)
|
|
|
|
+
|
|
|
|
+ cv2.fillPoly(class_mask, contour.reshape(1, -1, 2).astype("int32"), 0)
|
|
|
|
+ classes = classes[ymin:ymax + 1, xmin:xmax + 1]
|
|
|
|
+
|
|
|
|
+ new_classes = classes + class_mask
|
|
|
|
+
|
|
|
|
+ # 拉平
|
|
|
|
+ a = new_classes.reshape(-1)
|
|
|
|
+ b = np.where(a >= k)
|
|
|
|
+ classes = np.delete(a, b[0].tolist())
|
|
|
|
+
|
|
|
|
+ class_index = np.argmax(np.bincount(classes))
|
|
|
|
+ class_score = np.sum(classes == class_index) / len(classes)
|
|
|
|
+
|
|
|
|
+ return cv2.mean(bitmap[ymin:ymax + 1, xmin:xmax + 1], mask)[0], class_index, class_score
|
|
|
|
+
|
|
|
|
|
|
def __call__(self, outs_dict, shape_list):
|
|
def __call__(self, outs_dict, shape_list):
|
|
pred = outs_dict['maps']
|
|
pred = outs_dict['maps']
|
|
@@ -224,6 +287,29 @@ class DBPostProcess(object):
|
|
pred = pred[:, 0, :, :]
|
|
pred = pred[:, 0, :, :]
|
|
segmentation = pred > self.thresh
|
|
segmentation = pred > self.thresh
|
|
|
|
|
|
|
|
+ print(pred.shape)
|
|
|
|
+
|
|
|
|
+ if "classes" in outs_dict:
|
|
|
|
+ classes = outs_dict['classes']
|
|
|
|
+ # print(classes)
|
|
|
|
+ # print("jerome1")
|
|
|
|
+ # print(classes.shape)
|
|
|
|
+ # print(classes)
|
|
|
|
+
|
|
|
|
+ # np.set_printoptions(threshold=np.inf)
|
|
|
|
+
|
|
|
|
+ if isinstance(classes, paddle.Tensor):
|
|
|
|
+ # classes = paddle.argmax(classes, axis=1, dtype='int32')
|
|
|
|
+ classes = classes.numpy()
|
|
|
|
+ # else:
|
|
|
|
+ # classes = np.argmax(classes, axis=1).astype(np.int32)
|
|
|
|
+
|
|
|
|
+ classes = classes[:, 0, :, :]
|
|
|
|
+ print(classes.shape)
|
|
|
|
+
|
|
|
|
+ else:
|
|
|
|
+ classes = None
|
|
|
|
+
|
|
boxes_batch = []
|
|
boxes_batch = []
|
|
for batch_index in range(pred.shape[0]):
|
|
for batch_index in range(pred.shape[0]):
|
|
src_h, src_w, ratio_h, ratio_w = shape_list[batch_index]
|
|
src_h, src_w, ratio_h, ratio_w = shape_list[batch_index]
|
|
@@ -237,8 +323,15 @@ class DBPostProcess(object):
|
|
boxes, scores = self.polygons_from_bitmap(pred[batch_index],
|
|
boxes, scores = self.polygons_from_bitmap(pred[batch_index],
|
|
mask, src_w, src_h)
|
|
mask, src_w, src_h)
|
|
elif self.box_type == 'quad':
|
|
elif self.box_type == 'quad':
|
|
- boxes, scores = self.boxes_from_bitmap(pred[batch_index], mask,
|
|
|
|
|
|
+ if classes is None:
|
|
|
|
+ boxes, scores = self.boxes_from_bitmap(pred[batch_index], mask, None,
|
|
src_w, src_h)
|
|
src_w, src_h)
|
|
|
|
+
|
|
|
|
+ else:
|
|
|
|
+ boxes, scores, class_indexes, class_scores = self.boxes_from_bitmap(pred[batch_index], mask,
|
|
|
|
+ classes[batch_index],
|
|
|
|
+ src_w, src_h)
|
|
|
|
+ boxes_batch.append({'points': boxes, "classes": class_indexes, "class_scores": class_scores})
|
|
else:
|
|
else:
|
|
raise ValueError("box_type can only be one of ['quad', 'poly']")
|
|
raise ValueError("box_type can only be one of ['quad', 'poly']")
|
|
|
|
|