Python cPickle无法加载OCR模型库

时间:2015-12-19 16:11:21

标签: python ocr

我刚在Windows 7机器上安装了所有依赖项的ocropus OCR。 (我使用32位python 2.7)它似乎工作正常,除了我无法加载默认的OCR模型:en-default.pyrnn.gz。 ,并收到一个Traceback。我使用以下语法:

python ocropus-rpred -m en-default.pyrnn.gz book\0001\*.png

这是错误

INFO: #inputs47
# loading object /usr/local/share/ocropus/en-default.pyrnn.gz
Traceback (most recent call last):
    File "ocropus-rpred" line 109, in <module>
        network = ocrolib.load_object(args.model,verbose=1)
    File "C:\anaconda32\lib\site-packages\ocrolib\common.py", line 513, in load_object
        return unpickler.load()
    EOFError

我检查过的文件不是空的;还检查了启用的二进制模式标志,即“wb”和“rb”;还使用dos2unix转换了common.py的换行符。我无法解决这个问题。如果有人遇到类似的问题,请分享。

import cPickle
import gzip

def save_object(fname,obj,zip=0):
    if zip==0 and fname.endswith(".gz"):
        zip = 1
    if zip>0:
        # with gzip.GzipFile(fname,"wb") as stream:
        with os.popen("gzip -9 > '%s'"%fname,"wb") as stream:
            cPickle.dump(obj,stream,2)
    else:
        with open(fname,"wb") as stream:
            cPickle.dump(obj,stream,2)

def unpickle_find_global(mname,cname):
    if mname=="lstm.lstm":
        return getattr(lstm,cname)
    if not mname in sys.modules.keys():
        exec "import "+mname
    return getattr(sys.modules[mname],cname)

def load_object(fname,zip=0,nofind=0,verbose=0):
    """Loads an object from disk. By default, this handles zipped files
    and searches in the usual places for OCRopus. It also handles some
    class names that have changed."""
    if not nofind:
        fname = ocropus_find_file(fname)
    if verbose:
        print "# loading object",fname
    if zip==0 and fname.endswith(".gz"):
        zip = 1
    if zip>0:
        # with gzip.GzipFile(fname,"rb") as stream:
        with os.popen("gunzip < '%s'"%fname,"rb") as stream:
            unpickler = cPickle.Unpickler(stream)
            unpickler.find_global = unpickle_find_global
            return unpickler.load()
    else:
        with open(fname,"rb") as stream:
            unpickler = cPickle.Unpickler(stream)
            unpickler.find_global = unpickle_find_global
            return unpickler.load()

更新:嗨,请注意我使用了Python的本机gzip,它运行正常。谢谢你指出这一点。以下是适用于Windows的正确语法:{with gzip.GzipFile(fname,“rb”)as stream:}

0 个答案:

没有答案