Skip to content

Commit 7c1e460

Browse files
committed
fix: fixed boxes sorting error
1 parent 3f3fb13 commit 7c1e460

File tree

2 files changed

+17
-27
lines changed

2 files changed

+17
-27
lines changed

python/demo.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
11
# -*- encoding: utf-8 -*-
22
# @Author: SWHL
33
# @Contact: [email protected]
4-
from rapidocr import EngineType, RapidOCR
4+
from rapidocr import RapidOCR
55

6-
engine = RapidOCR(
7-
params={
8-
"Det.engine_type": EngineType.TORCH,
9-
"Cls.engine_type": EngineType.TORCH,
10-
"Rec.engine_type": EngineType.TORCH,
11-
"EngineConfig.torch.use_cuda": True, # 使用torch GPU版推理
12-
"EngineConfig.torch.gpu_id": 0, # 指定GPU id
13-
}
14-
)
6+
engine = RapidOCR()
157

168
img_url = "https://github.com/RapidAI/RapidOCR/blob/main/python/tests/test_files/ch_en_num.jpg?raw=true"
179
result = engine(img_url)

python/rapidocr/ch_ppocr_det/main.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
from .utils import DBPostProcess, DetPreProcess, TextDetOutput
2525

2626
_BOX_SORT_Y_THRESHOLD = 10
27-
_BOX_SORT_LINE_SEPARATION_FACTOR = 1e6
2827

2928

3029
class TextDetector:
@@ -82,25 +81,24 @@ def get_preprocess(self, max_wh: int) -> DetPreProcess:
8281
@staticmethod
8382
def sorted_boxes(dt_boxes: np.ndarray) -> np.ndarray:
8483
"""
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.
9085
"""
9186
if len(dt_boxes) == 0:
9287
return dt_boxes
9388

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]
9794

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)])
10199

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

Comments
 (0)