Skip to content

Commit 64e4515

Browse files
committed
update rapidocr version
1 parent 91b5fd9 commit 64e4515

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ dependencies = [
3737
"ftfy>=6.3.1,<7",
3838
"shapely>=2.0.7,<3",
3939
"tokenizers>=0.13.2",
40-
"rapidocr>=3.1.0,<=3.4.2",
40+
"rapidocr>=3.4.0,<=3.4.2",
4141
]
4242

4343
[project.optional-dependencies]

rapid_doc/model/ocr/rapid_ocr.py

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from rapidocr.ch_ppocr_det import TextDetOutput
1+
from typing import List, Dict, Any
22

33
from rapid_doc.model.ocr.ocr_patch import apply_ocr_patch
44

@@ -157,13 +157,62 @@ def ocr(self,
157157
op_record = {'padding_1': {'left': 0, 'top': 0}, 'preprocess': {'ratio_h': 1.0, 'ratio_w': 1.0}}
158158
raw_h, raw_w = ori_img.shape[:2]
159159
dt_boxes_np = [np.array(box, dtype=np.float32) for box in dt_boxes]
160-
word_results = self.ocr_engine.calc_word_boxes(img, TextDetOutput(boxes=dt_boxes_np), rec_result, op_record, raw_h, raw_w)
160+
word_results = self.calc_word_boxes(img, dt_boxes_np, rec_result, op_record, raw_h, raw_w)
161161
rec_res = list(zip(rec_result.txts, rec_result.scores, word_results))
162162
else:
163163
rec_res = list(zip(rec_result.txts, rec_result.scores))
164164
ocr_res.append(rec_res)
165165
return ocr_res
166166

167+
def calc_word_boxes(
168+
self,
169+
img: List[np.ndarray],
170+
dt_boxes: np.ndarray,
171+
rec_res: TextRecOutput,
172+
op_record: Dict[str, Any],
173+
raw_h: int,
174+
raw_w: int,
175+
) -> Any:
176+
rec_res = self.ocr_engine.cal_rec_boxes(
177+
img, dt_boxes, rec_res, self.ocr_engine.return_single_char_box
178+
)
179+
180+
origin_words = []
181+
for word_line in rec_res.word_results:
182+
origin_words_item = []
183+
for txt, score, bbox in word_line:
184+
if bbox is None:
185+
continue
186+
187+
origin_words_points = self.map_boxes_to_original(
188+
np.array([bbox]).astype(np.float64), op_record, raw_h, raw_w
189+
)
190+
origin_words_points = origin_words_points.astype(np.int32).tolist()[0]
191+
origin_words_item.append((txt, score, origin_words_points))
192+
193+
if origin_words_item:
194+
origin_words.append(tuple(origin_words_item))
195+
return tuple(origin_words)
196+
197+
def map_boxes_to_original(
198+
self, dt_boxes: np.ndarray, op_record: Dict[str, Any], ori_h: int, ori_w: int
199+
) -> np.ndarray:
200+
for op in reversed(list(op_record.keys())):
201+
v = op_record[op]
202+
if "padding" in op:
203+
top, left = v.get("top"), v.get("left")
204+
dt_boxes[:, :, 0] -= left
205+
dt_boxes[:, :, 1] -= top
206+
elif "preprocess" in op:
207+
ratio_h = v.get("ratio_h")
208+
ratio_w = v.get("ratio_w")
209+
dt_boxes[:, :, 0] *= ratio_w
210+
dt_boxes[:, :, 1] *= ratio_h
211+
212+
dt_boxes = np.where(dt_boxes < 0, 0, dt_boxes)
213+
dt_boxes[..., 0] = np.where(dt_boxes[..., 0] > ori_w, ori_w, dt_boxes[..., 0])
214+
dt_boxes[..., 1] = np.where(dt_boxes[..., 1] > ori_h, ori_h, dt_boxes[..., 1])
215+
return dt_boxes
167216

168217
def __call__(self, img, mfd_res=None):
169218

0 commit comments

Comments
 (0)