pytesseract tessedit_char_whitelist 不接受报价

pytesseract tessedit_char_whitelist not accepting quote

我在 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

它returns

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

我一直在寻找一种方法来转义单引号和双引号,并且 none 成功了。

当我 运行 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\"\'""")