π Awesome OCR multiple programing languages toolkits based on ONNX Runtime, OpenVINO, MNN, PaddlePaddle, TensorRT and PyTorch.
fix(cls): classify the middle of long lines with the v2.0 model (#748)
theofarouk8ac45f4e731f
diff --git a/python/rapidocr/ch_ppocr_cls/main.py b/python/rapidocr/ch_ppocr_cls/main.pyindex ec31bdd..353f797 100644--- a/python/rapidocr/ch_ppocr_cls/main.py+++ b/python/rapidocr/ch_ppocr_cls/main.py@@ -33,6 +33,10 @@ CLS_SHAPE_BY_OCR_VERSION = {class TextClassifier:def __init__(self, cfg: Dict[str, Any]):self.cls_image_shape = CLS_SHAPE_BY_OCR_VERSION[cfg["ocr_version"]]+ # The v2.0 classifier cannot read a line squeezed to its input width.+ # PP-LCNet textline_ori (PP-OCRv5) is trained on squeezed lines and+ # must keep receiving them.+ self.crop_long_lines = cfg["ocr_version"] == OCRVersion.PPOCRV4self.cls_batch_num = cfg["cls_batch_num"]self.cls_thresh = cfg["cls_thresh"]self.postprocess_op = ClsPostProcess(cfg["label_list"])@@ -83,6 +87,14 @@ class TextClassifier:def resize_norm_img(self, img: np.ndarray) -> np.ndarray:img_c, img_h, img_w = self.cls_image_shapeh, w = img.shape[:2]+ if self.crop_long_lines:+ # Classify the middle of a long line at the model's own aspect+ # ratio instead of the whole line squeezed into img_w.+ keep_w = int(math.ceil(h * img_w / float(img_h)))+ if w > keep_w:+ left = (w - keep_w) // 2+ img = img[:, left : left + keep_w]+ w = keep_wratio = w / float(h)if math.ceil(img_h * ratio) > img_w:resized_w = img_wdiff --git a/python/tests/test_cls.py b/python/tests/test_cls.pyindex fd95ca4..fb83bb9 100644--- a/python/tests/test_cls.py+++ b/python/tests/test_cls.py@@ -4,6 +4,7 @@import sysfrom pathlib import Path+import cv2from pytest import markroot_dir = Path(__file__).resolve().parent.parent@@ -52,3 +53,20 @@ def test_ppocrv5_cls_server():assert result.cls_res is not Noneassert result.cls_res[0][0] == "180"+++def test_ppocrv4_cls_long_line():+ # A long line used to be squeezed into the 192x48 input of the v2.0 model,+ # which then missed that it was upside down (#744).+ engine = RapidOCR()+ img = cv2.imread(str(tests_dir / "test_letterbox_like.jpg"))+ thresh = engine.cfg.Cls.cls_thresh++ result = engine(img, use_det=False, use_cls=True, use_rec=False)+ label, score = result.cls_res[0]+ assert not ("180" in label and score > thresh)++ rotated = cv2.rotate(img, cv2.ROTATE_180)+ result = engine(rotated, use_det=False, use_cls=True, use_rec=False)+ label, score = result.cls_res[0]+ assert "180" in label and score > thresh