python - pytesseract tessedit_char_whitelist 不接受报价

标签 python tesseract quotes python-tesseract pytesser

我已经开始在 Python 中使用 pytesserract。当我在

中传递单引号或双引号时
from PIL import Image
import pytesseract
import numpy as np

tesseract_config = r"""-c tessedit_char_whitelist=0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ#'<>(){};:"""
tesseract_language = "eng"

text = pytesseract.image_to_string(Image.open('res/outc001.jpg'), lang=tesseract_language, config=tesseract_config)
print text

返回

Traceback (most recent call last):
  File "main.py", line 15, in <module>
    text = pytesseract.image_to_string(Image.open('res/outc001.jpg'), lang=tesseract_language, config=tesseract_config).split('\n')
  File "/usr/local/lib/python2.7/dist-packages/pytesseract/pytesseract.py", line 193, in image_to_string
    return run_and_get_output(image, 'txt', lang, config, nice)
  File "/usr/local/lib/python2.7/dist-packages/pytesseract/pytesseract.py", line 140, in run_and_get_output
    run_tesseract(**kwargs)
  File "/usr/local/lib/python2.7/dist-packages/pytesseract/pytesseract.py", line 106, in run_tesseract
    command += shlex.split(config)
  File "/usr/lib/python2.7/shlex.py", line 279, in split
    return list(lex)
  File "/usr/lib/python2.7/shlex.py", line 269, in next
    token = self.get_token()
  File "/usr/lib/python2.7/shlex.py", line 96, in get_token
    raw = self.read_token()
  File "/usr/lib/python2.7/shlex.py", line 172, in read_token
    raise ValueError, "No closing quotation"
ValueError: No closing quotation

我一直在寻找一种方法来转义单引号和双引号,但都没有用。

当我运行 tesseract 时

tesseract res/outc001.jpg tesseract_out/out001 -c "tessedit_char_whitelist=0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ#\'\"<>(){};:"

它工作得很好。

最佳答案

Pytesseract 使用 shlex分隔配置参数。

shlex 的转义符是\ , 如果你想在 shlex.split() 中插入引号函数你必须用 \ 转义它.

  • 如果你想要'仅在白名单中:

    tesseract_config = "-c tessedit_char_whitelist=blahblah\\'")
    
  • 如果你想要"仅:

    tesseract_config = '-c tessedit_char_whitelist=blahblah\\"')
    
  • 如果你想要两者'" :

    tesseract_config = '''-c tessedit_char_whitelist=blahblah\\'\\"''')
    

    tesseract_config = """-c tessedit_char_whitelist=blahblah\\"\\'""")
    

关于python - pytesseract tessedit_char_whitelist 不接受报价,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49587228/

相关文章:

python - Docker无法创建conda虚拟环境

python - 如何阻止命令提示符在 python 中关闭?

python - 使用法语时 pytesseract 挂起

php - 动态图像调整大小

python - django:从迁移中排除模型

python - 如何在没有 Visual Studio 的 Windows 上安装 leptonica+tesseract 以在 Anaconda 中使用?

Android Tesseract 应用程序在 OCR 功能上崩溃

mysql - MySQL 中何时使用单引号、双引号和反引号

windows - schtasks 命令中的单引号

bash - Bash 中单引号和双引号的区别