2016-12-28 1 views
0

Я использую питон 3.5.2 и pytesseract, есть ошибка TypeError: a bytes-like object is required, not 'str', когда я запускаю мой код, (подробности ниже):TypeError: а байты-подобный объект необходим, а не «ул» в питон 3.5.2 и pytesseract

код: File "D:/test.py"

# -*- coding: utf-8 -*- 

try: 
    import Image 
except ImportError: 
    from PIL import Image 

import pytesseract 


print(pytesseract.image_to_string(Image.open('d:/testimages/name.gif'), lang='chi_sim')) 
print(pytesseract.image_to_string(Image.open('d:/testimages/mobile.gif'))) 

ошибка:

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' 

что я должен делать?

Edit:

У меня есть скачать обучающие данные в C:\Program Files (x86)\Tesseract-OCR\tessdata, как это:

enter image description here

и я вставить строку error_string = error_string.decode("utf-8") в get_errors(), ошибка выглядит так:

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') 

ответ

0

Это известный бу г в pytesseract см issue #32:

Error parsing of tesseract output is brittle: a bytes-like object is required, not 'str'

и

There actually is an error in tesseract. But on the Python end the error occurs because error_string is returning a byte-literal, and the geterrors call appears to have trouble with it

Обойти является установка подготовки данных для данного языка, см Tesseract running error или путем редактирования site-packages\pytesseract\pytesseract.py и вставить дополнительную строку в верхней из get_errors() функции (в строке 109):

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() 
+0

У этого есть некоторые другие проблемы, см. Мое редактирование. – zwl1619

+0

@ zwl1619: Я не *, который знаком с тем, как работает pytessaract. Исправление ошибки кодирования показывает, что данные обучения не установлены так, как ожидается. Ошибка выходила раньше, но из-за проблемы с кодировкой вы ее никогда не получали. Возможно, это какая-то проблема разрешения? –

Смежные вопросы