📄 Awesome OCR multiple programing languages toolkits based on ONNX Runtime, OpenVINO, MNN, PaddlePaddle, TensorRT and PyTorch.
fix(rapidoicr): fixed issue #446 (#453)
SWHL01906462569f
diff --git a/python/demo.py b/python/demo.pyindex cc8808c..693248c 100644--- a/python/demo.py+++ b/python/demo.py@@ -6,7 +6,7 @@ from rapidocr import RapidOCRengine = RapidOCR()img_url = "https://img1.baidu.com/it/u=3619974146,1266987475&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=516"-result = engine(img_url)+result = engine(img_url, return_word_box=True, return_single_char_box=True)print(result)result.vis("vis_result.jpg")diff --git a/python/rapidocr/cal_rec_boxes/main.py b/python/rapidocr/cal_rec_boxes/main.pyindex 500b387..4204afb 100644--- a/python/rapidocr/cal_rec_boxes/main.py+++ b/python/rapidocr/cal_rec_boxes/main.py@@ -23,7 +23,11 @@ class CalRecBoxes:代码借鉴自PaddlePaddle/PaddleOCR和fanqie03/char-detection"""def __call__(- self, imgs: List[np.ndarray], dt_boxes: List[np.ndarray], rec_res: TextRecOutput+ self,+ imgs: List[np.ndarray],+ dt_boxes: List[np.ndarray],+ rec_res: TextRecOutput,+ return_single_char_box: bool = False,) -> TextRecOutput:word_results = []for idx, (img, box) in enumerate(zip(imgs, dt_boxes)):@@ -33,7 +37,10 @@ class CalRecBoxes:h, w = img.shape[:2]img_box = np.array([[0, 0], [w, 0], [w, h], [0, h]])word_box_content_list, word_box_list, conf_list = self.cal_ocr_word_box(- rec_res.txts[idx], img_box, rec_res.word_results[idx]+ rec_res.txts[idx],+ img_box,+ rec_res.word_results[idx],+ return_single_char_box,)word_box_list = self.adjust_box_overlap(copy.deepcopy(word_box_list))direction = self.get_box_direction(box)@@ -67,7 +74,11 @@ class CalRecBoxes:return Direction.VERTICAL if aspect_ratio >= 1.5 else Direction.HORIZONTALdef cal_ocr_word_box(- self, rec_txt: str, bbox: np.ndarray, word_info: WordInfo+ self,+ rec_txt: str,+ bbox: np.ndarray,+ word_info: WordInfo,+ return_single_char_box: bool = False,) -> Tuple[List[str], List[List[List[float]]], List[float]]:"""Calculate the detection frame for each word based on the results of recognition and detection of ocr汉字坐标是单字的@@ -87,7 +98,7 @@ class CalRecBoxes:line_cols, char_widths, word_contents = [], [], []for word, word_col in zip(word_info.words, word_info.word_cols):- if is_all_en_num:+ if is_all_en_num and not return_single_char_box:line_cols.append(word_col)word_contents.append("".join(word))else:@@ -104,7 +115,7 @@ class CalRecBoxes:char_widths, bbox_points[0], bbox_points[2], len(rec_txt))- if is_all_en_num:+ if is_all_en_num and not return_single_char_box:word_boxes = self.calc_en_num_box(line_cols, avg_char_width, avg_col_width, bbox_points)diff --git a/python/rapidocr/cli.py b/python/rapidocr/cli.pyindex ca9a8a0..fc5b6c3 100644--- a/python/rapidocr/cli.py+++ b/python/rapidocr/cli.py@@ -4,7 +4,6 @@import shutilfrom pathlib import Path-root_dir = Path(__file__).resolve().parentDEFAULT_CFG_PATH = root_dir / "config.yaml"@@ -18,7 +17,7 @@ def generate_cfg(args):def check_install(ocr_engine):- img_url = "https://github.com/RapidAI/RapidOCR/blob/a9bb7c1f44b6e00556ada90ac588f020d7637c4b/python/tests/test_files/ch_en_num.jpg?raw=true"+ img_url = "https://www.modelscope.cn/models/RapidAI/RapidOCR/resolve/v3.1.0/resources/test_files/ch_en_num.jpg"result = ocr_engine(img_url)if result.txts is None or result.txts[0] != "正品促销":diff --git a/python/rapidocr/config.yaml b/python/rapidocr/config.yamlindex 756e9f9..f385d27 100644--- a/python/rapidocr/config.yaml+++ b/python/rapidocr/config.yaml@@ -11,6 +11,7 @@ Global:min_side_len: 30return_word_box: false+ return_single_char_box: falsefont_path: nulldiff --git a/python/rapidocr/inference_engine/onnxruntime/provider_config.py b/python/rapidocr/inference_engine/onnxruntime/provider_config.pyindex 0e58704..f276239 100644--- a/python/rapidocr/inference_engine/onnxruntime/provider_config.py+++ b/python/rapidocr/inference_engine/onnxruntime/provider_config.py@@ -3,7 +3,7 @@# @Contact: [email protected]import platformfrom enum import Enum-from typing import Any, Dict, List, Sequence+from typing import Any, Dict, List, Sequence, Tuplefrom onnxruntime import get_available_providers, get_device@@ -30,7 +30,7 @@ class ProviderConfig:self.cfg = engine_cfg- def get_ep_list(self):+ def get_ep_list(self) -> List[Tuple[str, Dict[str, Any]]]:results = [(EP.CPU_EP.value, self.cpu_ep_cfg())]if self.is_cuda_available():@@ -49,10 +49,10 @@ class ProviderConfig:return resultsdef cpu_ep_cfg(self) -> Dict[str, Any]:- return self.cfg.cpu_ep_cfg+ return dict(self.cfg.cpu_ep_cfg)def cuda_ep_cfg(self) -> Dict[str, Any]:- return self.cfg.cuda_ep_cfg+ return dict(self.cfg.cuda_ep_cfg)def dml_ep_cfg(self) -> Dict[str, Any]:if self.cfg.dm_ep_cfg is not None:@@ -63,7 +63,7 @@ class ProviderConfig:return self.cpu_ep_cfg()def cann_ep_cfg(self) -> Dict[str, Any]:- return self.cfg.cann_ep_cfg+ return dict(self.cfg.cann_ep_cfg)def verify_providers(self, session_providers: Sequence[str]):if not session_providers:diff --git a/python/rapidocr/main.py b/python/rapidocr/main.pyindex 629a1e4..e67d442 100644--- a/python/rapidocr/main.py+++ b/python/rapidocr/main.py@@ -62,7 +62,8 @@ class RapidOCR:self.cal_rec_boxes = CalRecBoxes()- self.return_word_box = False+ self.return_word_box = cfg.Global.return_word_box+ self.return_single_char_box = cfg.Global.return_single_char_boxself.cfg = cfg@@ -73,6 +74,7 @@ class RapidOCR:use_cls: Optional[bool] = None,use_rec: Optional[bool] = None,return_word_box: bool = False,+ return_single_char_box: bool = False,text_score: float = 0.5,box_thresh: float = 0.5,unclip_ratio: float = 1.6,@@ -82,6 +84,7 @@ class RapidOCR:use_rec = self.use_rec if use_rec is None else use_recself.return_word_box = return_word_box+ self.return_single_char_box = return_single_char_boxself.text_score = text_scoreself.text_det.postprocess_op.box_thresh = box_threshself.text_det.postprocess_op.unclip_ratio = unclip_ratio@@ -116,7 +119,9 @@ class RapidOCR:and det_res.boxes is not Noneand all(v for v in rec_res.word_results)):- rec_res = self.cal_rec_boxes(img, det_res.boxes, rec_res)+ rec_res = self.cal_rec_boxes(+ img, det_res.boxes, rec_res, self.return_single_char_box+ )origin_words = []for one_word in rec_res.word_results:one_word_points = one_word[2]diff --git a/python/tests/test_main.py b/python/tests/test_main.pyindex 17be855..5a7ea05 100644--- a/python/tests/test_main.py+++ b/python/tests/test_main.py@@ -456,3 +456,10 @@ def test_en_word_ocr(engine, img_name: str, words: str):result = engine(img_path, return_word_box=True)txts, _, _ = list(zip(*result.word_results))assert txts[0] == words+++def test_en_return_single_char_box(engine):+ img_path = tests_dir / "en.jpg"+ result = engine(img_path, return_word_box=True, return_single_char_box=True)+ txts, _, _ = list(zip(*result.word_results))+ assert txts[:3] == ("3", "M", "o")