|
|
@@ -433,11 +433,8 @@ class CropByPolys(BaseComponent):
|
|
|
"""apply"""
|
|
|
img = self._reader.read(img_path)
|
|
|
|
|
|
- # TODO
|
|
|
- # dt_boxes = self.sorted_boxes(data[K.DT_POLYS])
|
|
|
if self.det_box_type == "quad":
|
|
|
- dt_boxes = self.sorted_boxes(dt_polys)
|
|
|
- dt_boxes = np.array(dt_boxes)
|
|
|
+ dt_boxes = np.array(dt_polys)
|
|
|
output_list = []
|
|
|
for bno in range(len(dt_boxes)):
|
|
|
tmp_box = copy.deepcopy(dt_boxes[bno])
|
|
|
@@ -465,31 +462,6 @@ class CropByPolys(BaseComponent):
|
|
|
|
|
|
return output_list
|
|
|
|
|
|
- def sorted_boxes(self, dt_boxes):
|
|
|
- """
|
|
|
- Sort text boxes in order from top to bottom, left to right
|
|
|
- args:
|
|
|
- dt_boxes(array):detected text boxes with shape [4, 2]
|
|
|
- return:
|
|
|
- sorted boxes(array) with shape [4, 2]
|
|
|
- """
|
|
|
- dt_boxes = np.array(dt_boxes)
|
|
|
- num_boxes = dt_boxes.shape[0]
|
|
|
- sorted_boxes = sorted(dt_boxes, key=lambda x: (x[0][1], x[0][0]))
|
|
|
- _boxes = list(sorted_boxes)
|
|
|
-
|
|
|
- for i in range(num_boxes - 1):
|
|
|
- for j in range(i, -1, -1):
|
|
|
- if abs(_boxes[j + 1][0][1] - _boxes[j][0][1]) < 10 and (
|
|
|
- _boxes[j + 1][0][0] < _boxes[j][0][0]
|
|
|
- ):
|
|
|
- tmp = _boxes[j]
|
|
|
- _boxes[j] = _boxes[j + 1]
|
|
|
- _boxes[j + 1] = tmp
|
|
|
- else:
|
|
|
- break
|
|
|
- return _boxes
|
|
|
-
|
|
|
def get_minarea_rect_crop(self, img, points):
|
|
|
"""get_minarea_rect_crop"""
|
|
|
bounding_box = cv2.minAreaRect(np.array(points).astype(np.int32))
|
|
|
@@ -880,3 +852,38 @@ class CropByPolys(BaseComponent):
|
|
|
img = np.stack((img,) * 3, axis=-1)
|
|
|
img_crop, image = rectifier.run(img, new_points_list, mode="homography")
|
|
|
return img_crop[0]
|
|
|
+
|
|
|
+
|
|
|
+class SortBoxes(BaseComponent):
|
|
|
+
|
|
|
+ YIELD_BATCH = False
|
|
|
+
|
|
|
+ INPUT_KEYS = ["dt_polys"]
|
|
|
+ OUTPUT_KEYS = ["dt_polys"]
|
|
|
+ DEAULT_INPUTS = {"dt_polys": "dt_polys"}
|
|
|
+ DEAULT_OUTPUTS = {"dt_polys": "dt_polys"}
|
|
|
+
|
|
|
+ def apply(self, dt_polys):
|
|
|
+ """
|
|
|
+ Sort text boxes in order from top to bottom, left to right
|
|
|
+ args:
|
|
|
+ dt_boxes(array):detected text boxes with shape [4, 2]
|
|
|
+ return:
|
|
|
+ sorted boxes(array) with shape [4, 2]
|
|
|
+ """
|
|
|
+ dt_boxes = np.array(dt_polys)
|
|
|
+ num_boxes = dt_boxes.shape[0]
|
|
|
+ sorted_boxes = sorted(dt_boxes, key=lambda x: (x[0][1], x[0][0]))
|
|
|
+ _boxes = list(sorted_boxes)
|
|
|
+
|
|
|
+ for i in range(num_boxes - 1):
|
|
|
+ for j in range(i, -1, -1):
|
|
|
+ if abs(_boxes[j + 1][0][1] - _boxes[j][0][1]) < 10 and (
|
|
|
+ _boxes[j + 1][0][0] < _boxes[j][0][0]
|
|
|
+ ):
|
|
|
+ tmp = _boxes[j]
|
|
|
+ _boxes[j] = _boxes[j + 1]
|
|
|
+ _boxes[j + 1] = tmp
|
|
|
+ else:
|
|
|
+ break
|
|
|
+ return {"dt_polys": [box.tolist() for box in _boxes]}
|