如何在Docling(Python)中设置Tesseract的PSM模式?
在Docling中设置Tesseract页面分割模式(PSM)
针对TesseractCliOcrOptions的解决方案
直接通过extra_args属性添加Tesseract命令行参数:
from docling.ocr.tesseract_cli_ocr import TesseractCliOcrOptions ocr_options = TesseractCliOcrOptions() ocr_options.extra_args = ["--psm", "6"] # 将该选项传入TesseractCliOcr实例使用
针对TesseractOcrOptions的解决方案
由于该类基于Tesseract Python API,可通过两种方式设置PSM:
- 全局配置pytesseract
import pytesseract # 直接指定PSM参数 pytesseract.pytesseract.tesseract_cmd = "tesseract" custom_config = r"--psm 6" # 若Docling的OCR处理器支持传递自定义配置,可在此传入
- 扩展
TesseractOcrOptions类
from docling.ocr.tesseract_ocr import TesseractOcrOptions class CustomTesseractOcrOptions(TesseractOcrOptions): def __init__(self, psm=6, **kwargs): super().__init__(**kwargs) self._psm = psm def build_tesseract_config(self): config = super().build_tesseract_config() config.extend(["--psm", str(self._psm)]) return config # 使用自定义选项 ocr_options = CustomTesseractOcrOptions(psm=6)
验证方法
- 使用
TesseractCliOcr时,开启日志可查看完整命令行,确认--psm 6是否存在 - API方式可打印生成的配置列表,检查参数是否已添加
内容的提问来源于stack exchange,提问作者Pamudu Ranasinghe




