错误: 默认方向分类器会将长直线旋转 180° (文档中 2% 至 3% 的单词)
作者: theofarouk创建于 2026年10月1日更新于 2026年10月1日
原因。 TextClassifier.resize_norm_img 将一行缩放为 48 px 高,最多 192 px 宽。正文行的宽度大约是高度的 30 倍,因此它被压缩了 7.5 倍,结果接近随机。在 cls_thresh 以上,行将被旋转并反向读取。这两种情况都会发生:真正倒置的长行通常也会被忽略(下面的第二列)。
数字。 使用默认设置,在 15 页的 CC BY 文章中,在 216 dpi 下渲染的 RapidOCR()。PDF 的文本层是真实值,分数是 OCR 找到的 8,882 个单词的比例:
| 分类器 | 直立页面 | 以 180° 旋转的页面 |
|---|---|---|
| 关闭 | 99.0% | 7.1% |
| 开启(默认,v2.0 模型) | 96.0% | 59.3% |
| 开启,行中间(下面的差异) | 99.0% | 98.2% |
开启,Cls.ocr_version=PP-OCRv5 |
99.0% | 97.4% |
第二篇文章(18 页,10,885 个单词)默认情况下为 96.9% / 66.6%,使用更改后为 99.0% / 98.5%。
建议的更改,仅针对 v2.0 模型:
--- 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"]]
+ # v2.0 分类器无法读取被压缩到其输入宽度的行。
+ # PP-LCNet textline_ori (PP-OCRv5) 使用压缩的行进行训练,因此必须继续接收这些行。
+ self.crop_long_lines = cfg["ocr_version"] == OCRVersion.PPOCRV4
self.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_shape
h, w = img.shape[:2]
+ if self.crop_long_lines:
+ # 使用模型自己的宽高比来分类长行的中间部分,而不是将整行压缩到 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_w
ratio = w / float(h)
if math.ceil(img_h * ratio) > img_w:
resized_w = img_w内容来源: RapidAI/RapidOCR