Python 3.x TypeError:需要类似字节的对象,而不是';str';在python 3.5.2和PyteSeract中

Python 3.x TypeError:需要类似字节的对象,而不是';str';在python 3.5.2和PyteSeract中,python-3.x,Python 3.x,我正在使用python3.5.2和PyteSeract,有一个错误TypeError:当我运行代码时,需要一个类似字节的对象,而不是“str”(详细信息如下): 代码:文件“D:/test.py” 错误: Traceback (most recent call last): File "D:/test.py", line 11, in <module> print(pytesseract.image_to_string(Image.open('d:/testimages/

我正在使用python
3.5.2
和PyteSeract,有一个错误
TypeError:当我运行代码时,需要一个类似字节的对象,而不是“str”
(详细信息如下):

代码:
文件“D:/test.py”

错误:

Traceback (most recent call last):
  File "D:/test.py", line 11, in <module>
    print(pytesseract.image_to_string(Image.open('d:/testimages/name.gif'), lang='chi_sim'))
  File "C:\Users\dell\AppData\Local\Programs\Python\Python35\lib\site-packages\pytesseract\pytesseract.py", line 164, in image_to_string
    errors = get_errors(error_string)
  File "C:\Users\dell\AppData\Local\Programs\Python\Python35\lib\site-packages\pytesseract\pytesseract.py", line 112, in get_errors
    error_lines = tuple(line for line in lines if line.find('Error') >= 0)
  File "C:\Users\dell\AppData\Local\Programs\Python\Python35\lib\site-packages\pytesseract\pytesseract.py", line 112, in <genexpr>
    error_lines = tuple(line for line in lines if line.find('Error') >= 0)
TypeError: a bytes-like object is required, not 'str'

这是PyteSeract中的一个已知错误,请参阅:

分析tesseract输出时出现的错误很脆弱:需要类似字节的对象,而不是“str”

实际上,tesseract中有一个错误。但是在Python端发生错误是因为error_string返回字节文本,并且geterrors调用似乎有问题

解决方法是安装给定语言的培训数据,请参阅,或通过编辑
site packages\pytesseract\pytesseract.py
并在
get\u errors()
函数顶部插入一行额外内容(第109行):

然后,该函数的内容如下:

def get_errors(error_string):
    '''
    returns all lines in the error_string that start with the string "error"
    '''

    error_string = error_string.decode("utf-8")
    lines = error_string.splitlines()
    error_lines = tuple(line for line in lines if line.find('Error') >= 0)
    if len(error_lines) > 0:
        return '\n'.join(error_lines)
    else:
        return error_string.strip()
@zwl1619:我不太熟悉Pytesharact的工作原理。修复编码错误表明培训数据未按预期方式安装。以前曾抛出该错误,但由于编码问题,您从未获得该错误。也许是某种许可问题?
Traceback (most recent call last):
  File "D:/test.py", line 11, in <module>
    print(pytesseract.image_to_string(Image.open('d:/testimages/name.gif'), lang='chi_sim'))
  File "C:\Users\dell\AppData\Local\Programs\Python\Python35\lib\site-packages\pytesseract\pytesseract.py", line 165, in image_to_string
    raise TesseractError(status, errors)
pytesseract.pytesseract.TesseractError: (1, 'Error opening data file \\Program Files (x86)\\Tesseract-OCR\\tessdata/chi_sim.traineddata')
error_string = error_string.decode("utf-8")
def get_errors(error_string):
    '''
    returns all lines in the error_string that start with the string "error"
    '''

    error_string = error_string.decode("utf-8")
    lines = error_string.splitlines()
    error_lines = tuple(line for line in lines if line.find('Error') >= 0)
    if len(error_lines) > 0:
        return '\n'.join(error_lines)
    else:
        return error_string.strip()