|
24 | 24 | from .utils import DBPostProcess, DetPreProcess, TextDetOutput |
25 | 25 |
|
26 | 26 | _BOX_SORT_Y_THRESHOLD = 10 |
27 | | -_BOX_SORT_LINE_SEPARATION_FACTOR = 1e6 |
28 | 27 |
|
29 | 28 |
|
30 | 29 | class TextDetector: |
@@ -82,25 +81,24 @@ def get_preprocess(self, max_wh: int) -> DetPreProcess: |
82 | 81 | @staticmethod |
83 | 82 | def sorted_boxes(dt_boxes: np.ndarray) -> np.ndarray: |
84 | 83 | """ |
85 | | - Sort text boxes in order from top to bottom, left to right |
86 | | - args: |
87 | | - dt_boxes(array):detected text boxes with shape [4, 2] |
88 | | - return: |
89 | | - sorted boxes(array) with shape [4, 2] |
| 84 | + Equivalent NumPy implementation of the original bubble-adjusted sort. |
90 | 85 | """ |
91 | 86 | if len(dt_boxes) == 0: |
92 | 87 | return dt_boxes |
93 | 88 |
|
94 | | - # Sort by y, then identify lines, then sort by (line, x) |
95 | | - y_order = np.argsort(dt_boxes[:, 0, 1], kind="stable") |
96 | | - sorted_y = dt_boxes[y_order, 0, 1] |
| 89 | + # Step 1: Stable sort by y (top to bottom) |
| 90 | + y_coords = dt_boxes[:, 0, 1] |
| 91 | + y_order = np.argsort(y_coords, kind="stable") |
| 92 | + boxes_y_sorted = dt_boxes[y_order] |
| 93 | + y_sorted = y_coords[y_order] |
97 | 94 |
|
98 | | - line_ids = np.empty(len(dt_boxes), dtype=np.int32) |
99 | | - line_ids[0] = 0 |
100 | | - np.cumsum(np.abs(np.diff(sorted_y)) >= _BOX_SORT_Y_THRESHOLD, out=line_ids[1:]) |
| 95 | + # Step 2: Assign line IDs based on adjacent y differences |
| 96 | + dy = np.diff(y_sorted) |
| 97 | + line_increments = (dy >= _BOX_SORT_Y_THRESHOLD).astype(np.int32) |
| 98 | + line_ids = np.concatenate([[0], np.cumsum(line_increments)]) |
101 | 99 |
|
102 | | - # Create composite sort key for final ordering |
103 | | - # Shift line_ids by large factor, add x for tie-breaking |
104 | | - sort_key = line_ids[y_order] * _BOX_SORT_LINE_SEPARATION_FACTOR + dt_boxes[y_order, 0, 0] |
105 | | - final_order = np.argsort(sort_key, kind="stable") |
106 | | - return dt_boxes[y_order[final_order]] |
| 100 | + # Now, within each line_id group, sort by x (left to right) |
| 101 | + x_coords = boxes_y_sorted[:, 0, 0] |
| 102 | + final_order_in_y_sorted = np.lexsort((x_coords, line_ids)) |
| 103 | + |
| 104 | + return boxes_y_sorted[final_order_in_y_sorted] |
0 commit comments