2016-12-28 7 views
0

ich verwende Python 3.5.2 und pytesseract, gibt es einen Fehler TypeError: a bytes-like object is required, not 'str', wenn ich meinen Code ausführen, (Details siehe unten):Typeerror: ein Byte-ähnliches Objekt erforderlich ist, nicht 'str' in Python 3.5.2 und pytesseract

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

Fehler:

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' 

, was soll ich tun?

Edit:

Ich habe die Trainingsdaten in C:\Program Files (x86)\Tesseract-OCR\tessdata herunterladen, wie folgt aus:

enter image description here

und ich legen Sie die Linie error_string = error_string.decode("utf-8") in get_errors(), ist der Fehler wie folgt aus:

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

Antwort

0

Dies ist eine bekannte BU siehe g in pytesseract, issue #32:

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

und

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

Die Abhilfe ist es, die Trainingsdaten für eine bestimmte Sprache zu installieren, finden Sie Tesseract running error oder durch site-packages\pytesseract\pytesseract.py Bearbeitung und legen Sie eine zusätzliche Zeile an der Spitze die get_errors() Funktion (an Leitung 109):

error_string = error_string.decode("utf-8") 

die Funktion lautet dann:

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

Es hat einige andere Probleme, siehe meine Bearbeitung. – zwl1619

+0

@ zwl1619: Ich bin nicht * das * vertraut mit, wie Pytessaract funktioniert. Das Korrigieren des Codierungsfehlers zeigt, dass die Trainingsdaten nicht so installiert werden, wie es erwartet wird. Der Fehler wurde zuvor ausgelöst, aber aufgrund des Codierproblems haben Sie ihn nie bekommen. Vielleicht ist es eine Art Erlaubnisproblem? –

Verwandte Themen