From c7c8bfb3055a4e8193a544abf56b3ee62e10e3de Mon Sep 17 00:00:00 2001 From: SWHL Date: Wed, 4 Dec 2024 08:12:16 +0800 Subject: [PATCH] feat(rapidocr_paddle): support en char rec --- python/rapidocr_paddle/cal_rec_boxes/main.py | 59 +++++++++++--------- python/setup_paddle.py | 2 +- python/tests/test_paddle.py | 17 ++---- 3 files changed, 40 insertions(+), 38 deletions(-) diff --git a/python/rapidocr_paddle/cal_rec_boxes/main.py b/python/rapidocr_paddle/cal_rec_boxes/main.py index 5e0d71b..cbc5294 100644 --- a/python/rapidocr_paddle/cal_rec_boxes/main.py +++ b/python/rapidocr_paddle/cal_rec_boxes/main.py @@ -77,34 +77,26 @@ class CalRecBoxes: word_box_list = [] word_box_content_list = [] cn_width_list = [] + en_width_list = [] cn_col_list = [] - for word, word_col, state in zip(word_list, word_col_list, state_list): - if state == "cn": - if len(word_col) != 1: - char_seq_length = (word_col[-1] - word_col[0] + 1) * cell_width - char_width = char_seq_length / (len(word_col) - 1) - cn_width_list.append(char_width) - cn_col_list += word_col - word_box_content_list += word - else: - cell_x_start = bbox_x_start + int(word_col[0] * cell_width) - cell_x_end = bbox_x_start + int((word_col[-1] + 1) * cell_width) - cell = [ - [cell_x_start, bbox_y_start], - [cell_x_end, bbox_y_start], - [cell_x_end, bbox_y_end], - [cell_x_start, bbox_y_end], - ] - word_box_list.append(cell) - word_box_content_list.append("".join(word)) + en_col_list = [] - if len(cn_col_list) != 0: - if len(cn_width_list) != 0: - avg_char_width = np.mean(cn_width_list) + def cal_char_width(width_list, word_col_): + if len(word_col_) == 1: + return + char_total_length = (word_col_[-1] - word_col_[0] + 1) * cell_width + char_width = char_total_length / (len(word_col_) - 1) + width_list.append(char_width) + + def cal_box(col_list, width_list, word_box_list_): + if len(col_list) == 0: + return + if len(width_list) != 0: + avg_char_width = np.mean(width_list) else: avg_char_width = (bbox_x_end - bbox_x_start) / len(rec_txt) - for center_idx in cn_col_list: + for center_idx in col_list: center_x = (center_idx + 0.5) * cell_width cell_x_start = max(int(center_x - avg_char_width / 2), 0) + bbox_x_start cell_x_end = ( @@ -117,7 +109,20 @@ class CalRecBoxes: [cell_x_end, bbox_y_end], [cell_x_start, bbox_y_end], ] - word_box_list.append(cell) + word_box_list_.append(cell) + + for word, word_col, state in zip(word_list, word_col_list, state_list): + if state == "cn": + cal_char_width(cn_width_list, word_col) + cn_col_list += word_col + word_box_content_list += word + else: + cal_char_width(en_width_list, word_col) + en_col_list += word_col + word_box_content_list += word + + cal_box(cn_col_list, cn_width_list, word_box_list) + cal_box(en_col_list, en_width_list, word_box_list) sorted_word_box_list = sorted(word_box_list, key=lambda box: box[0][0]) return word_box_content_list, sorted_word_box_list @@ -256,5 +261,9 @@ class CalRecBoxes: p23[np.where(p23[:, 1] == np.min(p23[:, 1]))], p23[np.where(p23[:, 1] == np.max(p23[:, 1]))], ) - + # 解决单字矩形框重叠导致多个相同框的情况 + p1 = p1[:1, :] + p2 = p2[:1, :] + p3 = p3[:1, :] + p4 = p4[:1, :] return np.array([p1, p2, p3, p4]).reshape((-1, 2)).tolist() diff --git a/python/setup_paddle.py b/python/setup_paddle.py index aa1e0ce..2398efb 100644 --- a/python/setup_paddle.py +++ b/python/setup_paddle.py @@ -29,7 +29,7 @@ MODULE_NAME = "rapidocr_paddle" obtainer = GetPyPiLatestVersion() try: latest_version = obtainer(MODULE_NAME) - VERSION_NUM = obtainer.version_add_one(latest_version) + VERSION_NUM = obtainer.version_add_one(latest_version, add_patch=True) except: VERSION_NUM = "0.0.1" diff --git a/python/tests/test_paddle.py b/python/tests/test_paddle.py index 4783525..f75d846 100644 --- a/python/tests/test_paddle.py +++ b/python/tests/test_paddle.py @@ -212,18 +212,11 @@ def test_input_three_ndim_one_channel(): ), ( "text_vertical_words.png", - [ - "已", - "取", - "之", - "時", - "不", - "參", - "一", - "人", - "见", - "而", - ], # rapidocr_onnxruntime: 見, rapidocr_paddle: 见 + ["已", "取", "之", "時", "不", "參", "一", "人", "见", "而"], + ), + ( + "issue_170.png", + ["T", "E", "S", "T"], ), ], )