📄 Awesome OCR multiple programing languages toolkits based on ONNX Runtime, OpenVINO, MNN, PaddlePaddle, TensorRT and PyTorch.
feat(rapidocr): support to other language ocr and update visualization (#369)
SWHL202dfb91fc43
diff --git a/python/demo.py b/python/demo.pyindex aec145d..295c740 100644--- a/python/demo.py+++ b/python/demo.py@@ -10,5 +10,3 @@ result = engine(img_url)print(result)result.vis()--engine.export_config("1.yaml")diff --git a/python/rapidocr/ch_ppocr_cls/utils.py b/python/rapidocr/ch_ppocr_cls/utils.pyindex f2e09df..7b9c229 100644--- a/python/rapidocr/ch_ppocr_cls/utils.py+++ b/python/rapidocr/ch_ppocr_cls/utils.py@@ -12,10 +12,17 @@# See the License for the specific language governing permissions and# limitations under the License.from dataclasses import dataclass-from typing import List, Optional, Tuple+from pathlib import Path+from typing import List, Optional, Tuple, Unionimport numpy as np+from ..utils.logger import Logger+from ..utils.utils import save_img+from ..utils.vis_res import VisRes++logger = Logger(logger_name=__name__).get_log()+@dataclassclass TextClsOutput:@@ -28,6 +35,22 @@ class TextClsOutput:return 0return len(self.img_list)+ def vis(self, save_path: Optional[Union[str, Path]] = None) -> Optional[np.ndarray]:+ if self.img_list is None or self.cls_res is None:+ logger.warning("No image or txts to visualize.")+ return None++ txts = [f"{txt} {score:.2f}" for txt, score in self.cls_res]+ scores = [score for _, score in self.cls_res]++ vis = VisRes()+ vis_img = vis.draw_rec_res(self.img_list, txts, scores)++ if save_path is not None:+ save_img(save_path, vis_img)+ logger.info("Visualization saved as %s", save_path)+ return vis_img+class ClsPostProcess:def __init__(self, label_list: List[str]):diff --git a/python/rapidocr/ch_ppocr_det/main.py b/python/rapidocr/ch_ppocr_det/main.pyindex c432eb0..cb7e970 100644--- a/python/rapidocr/ch_ppocr_det/main.py+++ b/python/rapidocr/ch_ppocr_det/main.py@@ -63,7 +63,7 @@ class TextDetector:boxes = self.sorted_boxes(boxes)elapse = time.perf_counter() - start_time- return TextDetOutput(boxes, scores, elapse=elapse)+ return TextDetOutput(img, boxes, scores, elapse=elapse)def get_preprocess(self, max_wh: int) -> DetPreProcess:if self.limit_type == "min":diff --git a/python/rapidocr/ch_ppocr_det/utils.py b/python/rapidocr/ch_ppocr_det/utils.pyindex 8210428..4690b06 100644--- a/python/rapidocr/ch_ppocr_det/utils.py+++ b/python/rapidocr/ch_ppocr_det/utils.py@@ -9,9 +9,16 @@ import numpy as npimport pyclipperfrom shapely.geometry import Polygon+from ..utils.logger import Logger+from ..utils.utils import save_img+from ..utils.vis_res import VisRes++logger = Logger(logger_name=__name__).get_log()+@dataclassclass TextDetOutput:+ img: Optional[np.ndarray] = Noneboxes: Optional[np.ndarray] = Nonescores: Optional[Tuple[float]] = Noneelapse: float = 0.0@@ -21,6 +28,19 @@ class TextDetOutput:return 0return len(self.boxes)+ def vis(self, save_path: Optional[str] = None) -> Optional[np.ndarray]:+ if self.img is None or self.boxes is None or self.scores is None:+ logger.warning("No image or boxes to visualize.")+ return None++ vis = VisRes()+ vis_img = vis.draw_dt_boxes(self.img, self.boxes, self.scores)++ if save_path is not None:+ save_img(save_path, vis_img)+ logger.info("Visualization saved as %s", save_path)+ return vis_img+class DetPreProcess:def __init__(diff --git a/python/rapidocr/ch_ppocr_rec/main.py b/python/rapidocr/ch_ppocr_rec/main.pyindex 29a4076..7d7f988 100644--- a/python/rapidocr/ch_ppocr_rec/main.py+++ b/python/rapidocr/ch_ppocr_rec/main.py@@ -101,7 +101,7 @@ class TextRecognizer:all_line_results, all_word_results = list(zip(*rec_res))txts, scores = list(zip(*all_line_results))- return TextRecOutput(txts, scores, all_word_results, elapse)+ return TextRecOutput(img_list, txts, scores, all_word_results, elapse)def resize_norm_img(self, img: np.ndarray, max_wh_ratio: float) -> np.ndarray:img_channel, img_height, img_width = self.rec_image_shapediff --git a/python/rapidocr/ch_ppocr_rec/utils.py b/python/rapidocr/ch_ppocr_rec/utils.pyindex f06c71e..4b4ec15 100644--- a/python/rapidocr/ch_ppocr_rec/utils.py+++ b/python/rapidocr/ch_ppocr_rec/utils.py@@ -7,6 +7,12 @@ from typing import Any, List, Optional, Tuple, Unionimport numpy as np+from ..utils.logger import Logger+from ..utils.utils import save_img+from ..utils.vis_res import VisRes++logger = Logger(logger_name=__name__).get_log()+@dataclassclass TextRecConfig:@@ -29,18 +35,35 @@ class TextRecInput:@dataclassclass TextRecOutput:+ imgs: Optional[List[np.ndarray]] = Nonetxts: Optional[Tuple[str]] = Nonescores: Tuple[float] = (1.0,)word_results: Tuple[Tuple[str, float, Optional[List[List[int]]]]] = (("", 1.0, None),)elapse: Optional[float] = None+ lang_rec: Optional[str] = Nonedef __len__(self):if self.txts is None:return 0return len(self.txts)+ def vis(self, save_path: Optional[Union[str, Path]] = None) -> Optional[np.ndarray]:+ if self.imgs is None or self.txts is None:+ logger.warning("No image or txts to visualize.")+ return None++ vis = VisRes()+ vis_img = vis.draw_rec_res(+ self.imgs, self.txts, self.scores, lang_rec=self.lang_rec+ )++ if save_path is not None:+ save_img(save_path, vis_img)+ logger.info("Visualization saved as %s", save_path)+ return vis_img+class CTCLabelDecode:def __init__(diff --git a/python/rapidocr/default_models.yaml b/python/rapidocr/default_models.yamlindex 9a687bd..c9c0988 100644--- a/python/rapidocr/default_models.yaml+++ b/python/rapidocr/default_models.yaml@@ -4,7 +4,7 @@ onnxruntime:ch_PP-OCRv4_det_infer.onnx: https://www.modelscope.cn/models/RapidAI/RapidOCR/resolve/master/onnx/PP-OCRv4/det/ch_PP-OCRv4_det_infer.onnxch_PP-OCRv4_det_server_infer.onnx: https://www.modelscope.cn/models/RapidAI/RapidOCR/resolve/master/onnx/PP-OCRv4/det/ch_PP-OCRv4_det_server_infer.onnxen_PP-OCRv3_det_infer.onnx: https://www.modelscope.cn/models/RapidAI/RapidOCR/resolve/master/onnx/PP-OCRv4/det/en_PP-OCRv3_det_infer.onnx- Multilingual_PP-OCRv3_det_infer.onnx: https://www.modelscope.cn/models/RapidAI/RapidOCR/resolve/master/onnx/PP-OCRv4/det/Multilingual_PP-OCRv3_det_infer.onnx+ multi_PP-OCRv3_det_infer.onnx: https://www.modelscope.cn/models/RapidAI/RapidOCR/resolve/master/onnx/PP-OCRv4/det/Multilingual_PP-OCRv3_det_infer.onnxrec:arabic_PP-OCRv4_rec_infer.onnx: https://www.modelscope.cn/models/RapidAI/RapidOCR/resolve/master/onnx/PP-OCRv4/rec/arabic_PP-OCRv4_rec_infer.onnxch_PP-OCRv4_rec_infer.onnx: https://www.modelscope.cn/models/RapidAI/RapidOCR/resolve/master/onnx/PP-OCRv4/rec/ch_PP-OCRv4_rec_infer.onnx@@ -28,7 +28,7 @@ openvino:ch_PP-OCRv4_det_infer.onnx: https://www.modelscope.cn/models/RapidAI/RapidOCR/resolve/master/onnx/PP-OCRv4/det/ch_PP-OCRv4_det_infer.onnxch_PP-OCRv4_det_server_infer.onnx: https://www.modelscope.cn/models/RapidAI/RapidOCR/resolve/master/onnx/PP-OCRv4/det/ch_PP-OCRv4_det_server_infer.onnxen_PP-OCRv3_det_infer.onnx: https://www.modelscope.cn/models/RapidAI/RapidOCR/resolve/master/onnx/PP-OCRv4/det/en_PP-OCRv3_det_infer.onnx- Multilingual_PP-OCRv3_det_infer.onnx: https://www.modelscope.cn/models/RapidAI/RapidOCR/resolve/master/onnx/PP-OCRv4/det/Multilingual_PP-OCRv3_det_infer.onnx+ multi_PP-OCRv3_det_infer.onnx: https://www.modelscope.cn/models/RapidAI/RapidOCR/resolve/master/onnx/PP-OCRv4/det/Multilingual_PP-OCRv3_det_infer.onnxrec:arabic_PP-OCRv4_rec_infer:model_dir: https://www.modelscope.cn/models/RapidAI/RapidOCR/resolve/master/onnx/PP-OCRv4/rec/arabic_PP-OCRv4_rec_infer.onnx@@ -78,7 +78,7 @@ paddlepaddle:ch_PP-OCRv4_det_infer: https://www.modelscope.cn/models/RapidAI/RapidOCR/resolve/master/paddle/PP-OCRv4/det/ch_PP-OCRv4_det_inferch_PP-OCRv4_det_server_infer: https://www.modelscope.cn/models/RapidAI/RapidOCR/resolve/master/paddle/PP-OCRv4/det/ch_PP-OCRv4_det_server_inferen_PP-OCRv3_det_infer: https://www.modelscope.cn/models/RapidAI/RapidOCR/resolve/master/paddle/PP-OCRv4/det/en_PP-OCRv3_det_infer- Multilingual_PP-OCRv3_det_infer: https://www.modelscope.cn/models/RapidAI/RapidOCR/resolve/master/paddle/PP-OCRv4/det/Multilingual_PP-OCRv3_det_infer+ multi_PP-OCRv3_det_infer: https://www.modelscope.cn/models/RapidAI/RapidOCR/resolve/master/paddle/PP-OCRv4/det/Multilingual_PP-OCRv3_det_inferrec:arabic_PP-OCRv4_rec_infer:model_dir: https://www.modelscope.cn/models/RapidAI/RapidOCR/resolve/master/paddle/PP-OCRv4/rec/arabic_PP-OCRv4_rec_infer@@ -136,3 +136,17 @@ torch:dict_url: https://www.modelscope.cn/models/RapidAI/RapidOCR/resolve/master/paddle/PP-OCRv4/rec/ch_PP-OCRv4_rec_infer/ppocr_keys_v1.txtcls:ch_ptocr_mobile_v2.0_cls_infer.pth: https://www.modelscope.cn/models/RapidAI/RapidOCR/resolve/master/torch/ch_ptocr_mobile_v2.0_cls_infer.pth++fonts:+ ch: https://www.modelscope.cn/models/RapidAI/RapidOCR/resolve/master/resources/fonts/FZYTK.TTF+ en: https://www.modelscope.cn/models/RapidAI/RapidOCR/resolve/master/resources/fonts/FZYTK.TTF+ japan: https://www.modelscope.cn/models/RapidAI/RapidOCR/resolve/master/resources/fonts/japan.ttc+ arabic: https://www.modelscope.cn/models/RapidAI/RapidOCR/resolve/master/resources/fonts/arabic.ttf+ chinese_cht: https://www.modelscope.cn/models/RapidAI/RapidOCR/resolve/master/resources/fonts/chinese_cht.ttf+ cyrillic: https://www.modelscope.cn/models/RapidAI/RapidOCR/resolve/master/resources/fonts/cyrillic.ttf+ devanagari: https://www.modelscope.cn/models/RapidAI/RapidOCR/resolve/master/resources/fonts/devanagari_Martel-Regular.ttf+ korean: https://www.modelscope.cn/models/RapidAI/RapidOCR/resolve/master/resources/fonts/korean.ttf+ ka: https://www.modelscope.cn/models/RapidAI/RapidOCR/resolve/master/resources/fonts/kannada.ttf+ latin: https://www.modelscope.cn/models/RapidAI/RapidOCR/resolve/master/resources/fonts/latin.ttf+ ta: https://www.modelscope.cn/models/RapidAI/RapidOCR/resolve/master/resources/fonts/tamil.ttf+ te: https://www.modelscope.cn/models/RapidAI/RapidOCR/resolve/master/resources/fonts/telugu.ttf\ No newline at end of filediff --git a/python/rapidocr/main.py b/python/rapidocr/main.pyindex 2c4b264..b57ff20 100644--- a/python/rapidocr/main.py+++ b/python/rapidocr/main.py@@ -46,6 +46,8 @@ class RapidOCR:lang_det, lang_rec = ParseLang()(config.Global.lang_det, config.Global.lang_rec)+ self.lang_rec = lang_rec+self.text_score = config.Global.text_scoreself.min_height = config.Global.min_heightself.width_height_ratio = config.Global.width_height_ratio@@ -92,7 +94,7 @@ class RapidOCR:text_score: float = 0.5,box_thresh: float = 0.5,unclip_ratio: float = 1.6,- ) -> RapidOCROutput:+ ) -> Union[TextDetOutput, TextClsOutput, TextRecOutput, RapidOCROutput]:use_det = self.use_det if use_det is None else use_detuse_cls = self.use_cls if use_cls is None else use_clsuse_rec = self.use_rec if use_rec is None else use_rec@@ -151,6 +153,7 @@ class RapidOCR:det_res.boxes, op_record, raw_h, raw_w)+ rec_res.lang_rec = self.lang_recocr_res = self.get_final_res(ori_img, det_res, cls_res, rec_res)return ocr_res@@ -293,6 +296,7 @@ class RapidOCR:scores=rec_res.scores,word_results=rec_res.word_results,elapse_list=[det_res.elapse, cls_res.elapse, rec_res.elapse],+ lang_rec=self.lang_rec,)ocr_res = self.filter_by_text_score(ocr_res)if len(ocr_res) <= 0:diff --git a/python/rapidocr/utils/parse_parameters.py b/python/rapidocr/utils/parse_parameters.pyindex d1ec299..f3a6d3a 100644--- a/python/rapidocr/utils/parse_parameters.py+++ b/python/rapidocr/utils/parse_parameters.py@@ -14,117 +14,49 @@ InputType = Union[str, np.ndarray, bytes, Path, Image.Image]class ParseLang:def __init__(self):- self.latin_lang = [- "af",- "az",- "bs",- "cs",- "cy",- "da",- "de",- "es",- "et",- "fr",- "ga",- "hr",- "hu",- "id",- "is",- "it",- "ku",- "la",- "lt",- "lv",- "mi",- "ms",- "mt",- "nl",- "no",- "oc",- "pi",- "pl",- "pt",- "ro",- "rs_latin",- "sk",- "sl",- "sq",- "sv",- "sw",- "tl",- "tr",- "uz",- "vi",- "french",- "german",- ]-- self.arabic_lang = ["ar", "fa", "ug", "ur"]- self.cyrillic_lang = [- "ru",- "rs_cyrillic",- "be",- "bg",- "uk",- "mn",- "abq",- "ady",- "kbd",- "ava",- "dar",- "inh",- "che",- "lbe",- "lez",- "tab",- ]- self.devanagari_lang = [- "hi",- "mr",- "ne",- "bh",- "mai",- "ang",- "bho",- "mah",- "sck",- "new",- "gom",- "sa",- "bgc",+ self.lang_det_list = ["ch", "en", "multi"]+ self.lang_rec_list = [+ "ch",+ "en",+ "arabic",+ "chinese_cht",+ "cyrillic",+ "devanagari",+ "japan",+ "korean",+ "ka",+ "latin",+ "ta",+ "te",]def __call__(self, lang_det: str, lang_rec: str) -> Tuple[str, str]:- lang_det, det_model_type = lang_det.split("_")- lang_rec, rec_model_type = lang_rec.split("_")- return f"{lang_det}_{det_model_type}", f"{lang_rec}_{rec_model_type}"+ lang_det, det_model_type = lang_det.rsplit("_", 1)+ lang_det = self.parse_det_lang(lang_det)- def parse_det_lang(self, lang: str) -> str:- if lang == "ch":- return "ch"+ lang_rec, rec_model_type = lang_rec.rsplit("_", 1)+ lang_rec = self.parse_rec_lang(lang_rec)- if lang in ["en", "latin"]:- return "en"-- return "Multilingual"-- def parse_rec_lang(self, lang: str):- if lang in self.latin_lang:- return "latin"+ return f"{lang_det}_{det_model_type}", f"{lang_rec}_{rec_model_type}"- if lang in self.arabic_lang:- return "arabic"+ def parse_det_lang(self, lang: str) -> str:+ lang = lang.strip().lower()- if lang in self.cyrillic_lang:- return "cyrillic"+ if lang in self.lang_det_list:+ return lang+ raise ValueError(+ f"lang: {lang} is not in the supported list: {self.lang_det_list}"+ )- if lang in self.devanagari_lang:- return "devanagari"+ def parse_rec_lang(self, lang: str) -> str:+ lang = lang.strip().lower()- if lang in ["ch", "en"]:- pass+ if lang in self.lang_rec_list:+ return lang- raise ValueError(f"lang: {lang} is not in the supported list.")+ raise ValueError(+ f"lang: {lang} is not in the supported list: {self.lang_rec_list}"+ )class ParseParams(OmegaConf):diff --git a/python/rapidocr/utils/typings.py b/python/rapidocr/utils/typings.pyindex a843ea3..55e0fba 100644--- a/python/rapidocr/utils/typings.py+++ b/python/rapidocr/utils/typings.py@@ -4,10 +4,10 @@from dataclasses import dataclass, fieldfrom typing import List, Optional, Tuple, Union-import cv2import numpy as npfrom .logger import Logger+from .utils import save_imgfrom .vis_res import VisReslogger = Logger(logger_name=__name__).get_log()@@ -24,6 +24,7 @@ class RapidOCROutput:)elapse_list: List[Union[float, None]] = field(default_factory=list)elapse: float = field(init=False)+ lang_rec: Optional[str] = Nonedef __post_init__(self):self.elapse = sum(v for v in self.elapse_list if isinstance(v, float))@@ -52,22 +53,41 @@ class RapidOCROutput:final_res.append([box, list(rec)])return final_res- def vis(self):+ def vis(self, save_path: Optional[str] = None, font_path: Optional[str] = None):if self.img is None or self.boxes is None:logger.warning("No image or boxes to visualize.")returnvis = VisRes()if all(v is None for v in self.word_results):- vis_img = vis(self.img, self.boxes, self.txts, self.scores)- cv2.imwrite("vis.png", vis_img)- logger.info("Visualization saved as vis.png.")+ vis_img = vis(+ self.img,+ self.boxes,+ self.txts,+ self.scores,+ font_path=font_path,+ lang_rec=self.lang_rec,+ )+ if save_path is None:+ save_path = "vis_det_cls_rec_result.png"++ save_img(save_path, vis_img)+ logger.info("Visualization saved as %s", save_path)return# single word viswords_results = self.word_resultswords, words_scores, words_boxes = list(zip(*words_results))- vis_img = vis(self.img, words_boxes, words, words_scores)- cv2.imwrite("vis_single.png", vis_img)- logger.info("Single word visualization saved as vis_single.png.")- return+ vis_img = vis(+ self.img,+ words_boxes,+ words,+ words_scores,+ font_path=font_path,+ lang_rec=self.lang_rec,+ )+ if save_path is None:+ save_single_path = "vis_single.png"++ save_img(save_single_path, vis_img)+ logger.info("Single word visualization saved as %s", save_single_path)diff --git a/python/rapidocr/utils/utils.py b/python/rapidocr/utils/utils.pyindex e4e0fee..99416cc 100644--- a/python/rapidocr/utils/utils.py+++ b/python/rapidocr/utils/utils.py@@ -6,10 +6,19 @@ from pathlib import Pathfrom typing import Unionfrom urllib.parse import urlparse+import cv2+import numpy as npimport requestsfrom tqdm import tqdm+def save_img(save_path: Union[str, Path], img: np.ndarray):+ if not Path(save_path).parent.exists():+ Path(save_path).parent.mkdir(parents=True, exist_ok=True)++ cv2.imwrite(str(save_path), img)++def is_url(url: str) -> bool:try:result = urlparse(url)diff --git a/python/rapidocr/utils/vis_res.py b/python/rapidocr/utils/vis_res.pyindex 94598dd..eba6fe4 100644--- a/python/rapidocr/utils/vis_res.py+++ b/python/rapidocr/utils/vis_res.py@@ -8,23 +8,30 @@ from typing import List, Optional, Tuple, Unionimport cv2import numpy as np+from omegaconf import OmegaConffrom PIL import Image, ImageDraw, ImageFontfrom .load_image import LoadImage+from .logger import Loggerfrom .utils import download_fileroot_dir = Path(__file__).resolve().parent.parentInputType = Union[str, np.ndarray, bytes, Path, Image.Image]-DEFAULT_FONT_PATH = root_dir / "models" / "FZYTK.TTF"-DEFAULT_FONT_URL = "https://www.modelscope.cn/models/RapidAI/RapidOCR/resolve/master/resources/fonts/FZYTK.TTF"+DEFAULT_FONT_DIR = root_dir / "models"+DEFAULT_FONT_PATH = DEFAULT_FONT_DIR / "FZYTK.TTF"+FONT_YAML_PATH = root_dir / "default_models.yaml"class VisRes:def __init__(self, text_score: float = 0.5):+ self.logger = Logger(logger_name=__name__).get_log()+self.text_score = text_scoreself.load_img = LoadImage()+ self.font_cfg = OmegaConf.load(FONT_YAML_PATH).fonts+def __call__(self,img_content: InputType,@@ -32,15 +39,25 @@ class VisRes:txts: Optional[Union[List[str], Tuple[str]]] = None,scores: Optional[Tuple[float]] = None,font_path: Optional[str] = None,+ lang_rec: Optional[str] = None,) -> np.ndarray:if txts is None:- return self.draw_dt_boxes(img_content, dt_boxes)- return self.draw_ocr_box_txt(img_content, dt_boxes, txts, scores, font_path)+ return self.draw_dt_boxes(img_content, dt_boxes, scores)- def draw_dt_boxes(self, img_content: InputType, dt_boxes: np.ndarray) -> np.ndarray:+ font_path = self.get_font_path(font_path, lang_rec)+ return self.draw_ocr_box_txt(img_content, dt_boxes, txts, font_path, scores)++ def draw_dt_boxes(+ self,+ img_content: InputType,+ dt_boxes: np.ndarray,+ scores: Optional[Tuple[float]] = None,+ ) -> np.ndarray:img = self.load_img(img_content)+ if scores is None:+ scores = [1.0] * len(dt_boxes)- for idx, box in enumerate(dt_boxes):+ for idx, (box, score) in enumerate(zip(dt_boxes, scores)):color = self.get_random_color()points = np.array(box)@@ -48,20 +65,107 @@ class VisRes:start_point = round(points[0][0]), round(points[0][1])cv2.putText(- img, f"{idx}", start_point, cv2.FONT_HERSHEY_SIMPLEX, 1, color, 3+ img,+ f"{idx}:{score:.3f}",+ start_point,+ cv2.FONT_HERSHEY_SIMPLEX,+ 1,+ color,+ 3,)return img+ def get_font_path(+ self,+ font_path: Optional[Union[str, Path]] = None,+ lang_rec: Optional[str] = None,+ ) -> str:+ if lang_rec is None:+ # 没有指定语种,用默认字体文件+ download_file(self.font_cfg["ch"], DEFAULT_FONT_PATH, logger=self.logger)+ return str(DEFAULT_FONT_PATH)++ if font_path is None:+ # 指定了语种,但是没有指定字体文件,根据语种选择字体文件+ lang_rec = lang_rec.rsplit("_", 1)[0]+ font_url = self.font_cfg.get(lang_rec, None)+ if font_url is None:+ self.logger.warning(+ "Font file for %s is not found in the supported font list. Default font file will be used.",+ lang_rec,+ )+ download_file(+ self.font_cfg["ch"], DEFAULT_FONT_PATH, logger=self.logger+ )+ return str(DEFAULT_FONT_PATH)++ save_font_path = DEFAULT_FONT_DIR / f"{Path(font_url).name}"+ download_file(font_url, save_font_path, logger=self.logger)+ return str(save_font_path)++ return str(font_path)++ def draw_rec_res(+ self,+ imgs: List[InputType],+ txts: Union[List[str], Tuple[str]],+ scores: Tuple[float],+ lang_rec: Optional[str] = None,+ ) -> np.ndarray:+ result_imgs = []+ for img, txt, score in zip(imgs, txts, scores):+ vis_img = self.draw_one_rec_res(img, txt, score, lang_rec)+ result_imgs.append(vis_img)+ return self.concat_imgs(result_imgs, direction="vertical")++ def draw_one_rec_res(+ self,+ img_content: InputType,+ txt: str,+ score: float,+ lang_rec: Optional[str] = None,+ ) -> np.ndarray:+ font_path = self.get_font_path(None, lang_rec)++ image = Image.fromarray(self.load_img(img_content))+ h, w = image.height, image.width+ if image.mode == "L":+ image = image.convert("RGB")++ img_left = image.copy()+ img_right = Image.new("RGB", (w, h), (255, 255, 255))+ draw_right = ImageDraw.Draw(img_right)+ box = [[0, 0], [w, 0], [w, h], [0, h]]++ box_height = self.get_box_height(box)+ box_width = self.get_box_width(box)+ if box_height > 2 * box_width:+ font_size = max(int(box_width * 0.9), 10)+ font = ImageFont.truetype(font_path, font_size, encoding="utf-8")+ cur_y = box[0][1]++ for c in txt:+ draw_right.text((box[0][0] + 3, cur_y), c, fill=(0, 0, 0), font=font)+ cur_y += self.get_char_size(font, c)+ else:+ font_size = max(int(box_height * 0.8), 10)+ font = ImageFont.truetype(font_path, font_size, encoding="utf-8")+ draw_right.text([box[0][0], box[0][1]], txt, fill=(0, 0, 0), font=font)++ img_left = Image.blend(image, img_left, 0.5)+ img_show = Image.new("RGB", (w * 2, h), 255)+ img_show.paste(img_left, (0, 0, w, h))+ img_show.paste(img_right, (w, 0, w * 2, h))+ return np.array(img_show)+def draw_ocr_box_txt(self,img_content: InputType,dt_boxes: np.ndarray,txts: Union[List[str], Tuple[str]],+ font_path: str,scores: Optional[Tuple[float]] = None,- font_path: Optional[str] = None,) -> np.ndarray:- font_path = self.get_font_path(font_path)-image = Image.fromarray(self.load_img(img_content))h, w = image.height, image.widthif image.mode == "L":@@ -106,13 +210,6 @@ class VisRes:img_show.paste(img_right, (w, 0, w * 2, h))return np.array(img_show)- @staticmethod- def get_font_path(font_path: Optional[Union[str, Path]] = None) -> str:- if font_path is None or not Path(font_path).exists():- download_file(DEFAULT_FONT_URL, DEFAULT_FONT_PATH, logger=None)- return str(DEFAULT_FONT_PATH)- return str(font_path)-@staticmethoddef get_random_color() -> Tuple[int, int, int]:return (@@ -122,11 +219,11 @@ class VisRes:)@staticmethod- def get_box_height(box: List[List[float]]) -> float:+ def get_box_height(box: List[List[Union[float, int]]]) -> float:return math.sqrt((box[0][0] - box[3][0]) ** 2 + (box[0][1] - box[3][1]) ** 2)@staticmethod- def get_box_width(box: List[List[float]]) -> float:+ def get_box_width(box: List[List[Union[float, int]]]) -> float:return math.sqrt((box[0][0] - box[1][0]) ** 2 + (box[0][1] - box[1][1]) ** 2)@staticmethod@@ -143,3 +240,36 @@ class VisRes:raise ValueError("The Pillow ImageFont instance has not getsize or getlength func.")++ @staticmethod+ def concat_imgs(+ imgs: List[np.ndarray], direction: str = "horizontal"+ ) -> np.ndarray:+ img_list = [Image.fromarray(img_path) for img_path in imgs]++ img_sizes = np.array([v.size for v in img_list])++ if direction == "horizontal":+ width = np.sum(img_sizes[:, 0])+ height = np.max(img_sizes[:, 1])+ elif direction == "vertical":+ width = np.max(img_sizes[:, 0])+ height = np.sum(img_sizes[:, 1])+ else:+ raise ValueError(f"{direction} is not supported.")++ new_image = Image.new("RGB", (width, height), color="white")++ for i, img in enumerate(img_list):+ if i == 0:+ new_image.paste(img, (0, 0))+ continue++ if direction == "horizontal":+ x = np.sum(img_sizes[:, 0][:i])+ new_image.paste(img, (x, 0))+ elif direction == "vertical":+ y = np.sum(img_sizes[:, 1][:i])+ new_image.paste(img, (0, y))++ return np.array(new_image)diff --git a/python/tests/test_files/korean.jpg b/python/tests/test_files/korean.jpgnew file mode 100644index 0000000..08c4784Binary files /dev/null and b/python/tests/test_files/korean.jpg differdiff --git a/python/tests/test_main.py b/python/tests/test_main.pyindex b338d4b..499ea95 100644--- a/python/tests/test_main.py+++ b/python/tests/test_main.py@@ -38,6 +38,34 @@ def get_engine(params: Optional[Dict[str, Any]] = None):return engine+def test_vis_only_det(engine):+ img_path = tests_dir / "ch_en_num.jpg"+ result = engine(img_path, use_det=True, use_cls=False, use_rec=False)+ vis_img = result.vis()+ assert vis_img.shape == (430, 323, 3)+++def test_vis_only_rec(engine):+ img_path = tests_dir / "text_rec.jpg"+ result = engine(img_path, use_det=False, use_cls=False, use_rec=True)+ vis_img = result.vis()+ assert vis_img.shape == (78, 724, 3)+++def test_vis_only_cls(engine):+ img_path = tests_dir / "text_rec.jpg"+ result = engine(img_path, use_det=False, use_cls=True, use_rec=False)+ vis_img = result.vis()+ assert vis_img.shape == (78, 724, 3)+++def test_vis_det_cls_rec(engine):+ img_path = tests_dir / "ch_en_num.jpg"+ result = engine(img_path, use_det=False, use_cls=True, use_rec=False)+ vis_img = result.vis()+ assert vis_img.shape == (430, 323, 3)++def test_full_black_img(engine):img_path = tests_dir / "empty_black.jpg"result = engine(img_path)@@ -92,7 +120,15 @@ def test_error_lang():assert exc_info.type is ValueError-def test_lang():+def test_korean_lang():+ engine = get_engine(params={"Global.lang_rec": "korean_mobile"})+ img_path = tests_dir / "korean.jpg"+ result = engine(img_path)+ assert result.txts is not None+ assert result.txts[0] == "베이징차오양"+++def test_en_lang():engine = get_engine(params={"Global.lang_rec": "en_mobile"})img_path = tests_dir / "en.jpg"result = engine(img_path)