Skip to content

πŸ“„ Awesome OCR multiple programing languages toolkits based on ONNX Runtime, OpenVINO, MNN, PaddlePaddle, TensorRT and PyTorch.

feat: optimized box sorting (#587)

Mert3f3fb1323530
diff --git a/python/rapidocr/ch_ppocr_det/main.py b/python/rapidocr/ch_ppocr_det/main.py
index 9c2736f..89748a8 100644
--- a/python/rapidocr/ch_ppocr_det/main.py
+++ b/python/rapidocr/ch_ppocr_det/main.py
@@ -23,6 +23,9 @@ from rapidocr.inference_engine.base import get_engine
from .utils import DBPostProcess, DetPreProcess, TextDetOutput
+_BOX_SORT_Y_THRESHOLD = 10
+_BOX_SORT_LINE_SEPARATION_FACTOR = 1e6
+
class TextDetector:
def __init__(self, cfg: Dict[str, Any]):
@@ -85,19 +88,19 @@ class TextDetector:
return:
sorted boxes(array) with shape [4, 2]
"""
- 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 np.array(_boxes)
+ if len(dt_boxes) == 0:
+ return dt_boxes
+
+ # Sort by y, then identify lines, then sort by (line, x)
+ y_order = np.argsort(dt_boxes[:, 0, 1], kind="stable")
+ sorted_y = dt_boxes[y_order, 0, 1]
+
+ line_ids = np.empty(len(dt_boxes), dtype=np.int32)
+ line_ids[0] = 0
+ np.cumsum(np.abs(np.diff(sorted_y)) >= _BOX_SORT_Y_THRESHOLD, out=line_ids[1:])
+
+ # Create composite sort key for final ordering
+ # Shift line_ids by large factor, add x for tie-breaking
+ sort_key = line_ids[y_order] * _BOX_SORT_LINE_SEPARATION_FACTOR + dt_boxes[y_order, 0, 0]
+ final_order = np.argsort(sort_key, kind="stable")
+ return dt_boxes[y_order[final_order]]