Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python和tesseract的OCR OCr的示例代码 错误_Python_Python 3.x_Ocr_Tesseract - Fatal编程技术网

使用python和tesseract的OCR OCr的示例代码 错误

使用python和tesseract的OCR OCr的示例代码 错误,python,python-3.x,ocr,tesseract,Python,Python 3.x,Ocr,Tesseract,我使用的是Python3,我下载了tesseract库并添加到anaconda库中。但在第一次运行它时,我收到了显示打印缺少括号的错误,所以我改变了这一点,现在我发现这个错误,任何人都可以帮我,这将是非常棒的。 我还在这里添加tesseract包装器的源代码 “”“使用Google的Tesseract引擎在Python中进行OCR http://code.google.com/p/pytesser/ by Michael J.T. O'Kelly V 0.0.1, 3/10/07""" PIL

我使用的是Python3,我下载了tesseract库并添加到anaconda库中。但在第一次运行它时,我收到了显示打印缺少括号的错误,所以我改变了这一点,现在我发现这个错误,任何人都可以帮我,这将是非常棒的。 我还在这里添加tesseract包装器的源代码 “”“使用Google的Tesseract引擎在Python中进行OCR

http://code.google.com/p/pytesser/
by Michael J.T. O'Kelly
V 0.0.1, 3/10/07"""

PIL import Image
import subprocess

import util
import errors

tesseract_exe_name = 'C:\Users\SACHIN\Anaconda3\Lib\site-packages\pytesser\\tesseract' # Name of executable to be called at command line
scratch_image_name = "temp.bmp" # This file must be .bmp or other Tesseract-compatible format
scratch_text_name_root = "temp" # Leave out the .txt extension
cleanup_scratch_flag = True  # Temporary files cleaned up after OCR operation

def call_tesseract(input_filename, output_filename):
    """Calls external tesseract.exe on input file (restrictions on types),
    outputting output_filename+'txt'"""
    args = [tesseract_exe_name, input_filename, output_filename]
    proc = subprocess.Popen(args)
    retcode = proc.wait()
    if retcode!=0:
        errors.check_for_errors()

def image_to_string(im, cleanup = cleanup_scratch_flag):
    """Converts im to file, applies tesseract, and fetches resulting text.
    If cleanup=True, delete scratch files after operation."""
    try:
        util.image_to_scratch(im, scratch_image_name)
        call_tesseract(scratch_image_name, scratch_text_name_root)
        text = util.retrieve_text(scratch_text_name_root)
    finally:
        if cleanup:
            util.perform_cleanup(scratch_image_name, scratch_text_name_root)
    return text

def image_file_to_string(filename, cleanup = cleanup_scratch_flag, graceful_errors=True):
    """Applies tesseract to filename; or, if image is incompatible and graceful_errors=True,
    converts to compatible format and then applies tesseract.  Fetches resulting text.
    If cleanup=True, delete scratch files after operation."""
    try:
        try:
            call_tesseract(filename, scratch_text_name_root)
            text = util.retrieve_text(scratch_text_name_root)
        except errors.Tesser_General_Exception:
            if graceful_errors:
                im = Image.open(filename)
                text = image_to_string(im, cleanup)
            else:
                raise
    finally:
        if cleanup:
            util.perform_cleanup(scratch_image_name, scratch_text_name_root)
    return text


if __name__=='__main__':
    im = Image.open('phototest.tif')
    text = image_to_string(im)
    print (text)
    try:
        text = image_file_to_string('fnord.tif', graceful_errors=False)
    except errors.Tesser_General_Exception, value:
        print "fnord.tif is incompatible filetype.  Try graceful_errors=True"
        print value
    text = image_file_to_string('fnord.tif', graceful_errors=True)
    print ("fnord.tif contents:", text)
    text = image_file_to_string('fonts_test.png', graceful_errors=True)
    print (text)
而不是使用:

except errors.Tesser_General_Exception, value:
换成

except errors.Tesser_General_Exception as value:

它对我来说很有用。它是关于从
Python2
升级到
Python3

@user10089632和second?。我完全按照tutorialbro中的说明做了。这个问题是我在Python2-lib中使用Python2-lib,不管有什么代码会出现问题,我实际上必须调试整个库。即使在r由于一些问题持续存在,所以我决定使用python2.7,所有的事情都顺其自然
except errors.Tesser_General_Exception, value:
except errors.Tesser_General_Exception as value: