导入tesseract错误

时间:2014-01-10 21:34:42

标签: python tesseract

我正在尝试使用Mac Maverick在Python中导入tesseract,但是我收到以下错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "tesseract.py", line 26, in <module>
    _tesseract = swig_import_helper()
  File "tesseract.py", line 18, in swig_import_helper
    import _tesseract
ImportError: No module named _tesseract

我按照以下步骤安装tesseract: https://code.google.com/p/python-tesseract/wiki/HowToCompilePythonTesseractForMacMountainLion

我该怎么做才能解决这个问题?

更新:

python setup.py构建的回溯

os=darwin
Current Version : tesseract
~~~cv_pc~~~
{'extra_link_args': ['not', 'found', 'sh:', 'command', 'pkg-config:']}
['extra_link_args']
Traceback (most recent call last):
  File "setup.py", line 160, in <module>
    libname="open"+item.split("libopen")[1].split(".")[0]
IndexError: list index out of range

UPDATE 2 homwbrew版本:

python setup.py install --user

的消息

OS =达尔文 当前版本:tesseract ~~~ cv_pc ~~~ {'extra_link_args':['不','也许','变量','在','包含','到','不','应该','环境','opencv','添加', '你','`opencv.pc'“,'包','PKG_CONFIG_PATH','搜索','包','是',''opencv'”,'目录','找到','', 'path。','pkg-config']} [ 'extra_link_args'] Traceback(最近一次调用最后一次):   文件“setup.py”,第160行,in     LIBNAME = “打开” + item.split( “libopen”)[1] .split( “”)[0]

IndexError:列表索引超出范围

更新3:从步骤1-3开始,一切都很好。 但是在第4步,我被困在了 python setup.py clean

打印此消息:

os=darwin
Current Version : tesseract
~~~cv_pc~~~
{'extra_link_args': ['/usr/local/Cellar/opencv/2.4.7.1/lib/libopencv_features2d.dylib', '/usr/local/Cellar/opencv/2.4.7.1/lib/libopencv_objdetect.dylib', '/usr/local/Cellar/opencv/2.4.7.1/lib/libopencv_video.dylib', '/usr/local/Cellar/opencv/2.4.7.1/lib/libopencv_imgproc.dylib', '/usr/local/Cellar/opencv/2.4.7.1/lib/libopencv_ocl.dylib', '/usr/local/Cellar/opencv/2.4.7.1/lib/libopencv_contrib.dylib', '/usr/local/Cellar/opencv/2.4.7.1/lib/libopencv_superres.dylib', '/usr/local/Cellar/opencv/2.4.7.1/lib/libopencv_nonfree.dylib', '/usr/local/Cellar/opencv/2.4.7.1/lib/libopencv_flann.dylib', '/usr/local/Cellar/opencv/2.4.7.1/lib/libopencv_legacy.dylib', '/usr/local/Cellar/opencv/2.4.7.1/lib/libopencv_core.dylib', '/usr/local/Cellar/opencv/2.4.7.1/lib/libopencv_gpu.dylib', '/usr/local/Cellar/opencv/2.4.7.1/lib/libopencv_ts.a', '/usr/local/Cellar/opencv/2.4.7.1/lib/libopencv_videostab.dylib', '/usr/local/Cellar/opencv/2.4.7.1/lib/libopencv_highgui.dylib', '/usr/local/Cellar/opencv/2.4.7.1/lib/libopencv_ml.dylib', '/usr/local/Cellar/opencv/2.4.7.1/lib/libopencv_stitching.dylib', '/usr/local/Cellar/opencv/2.4.7.1/lib/libopencv_calib3d.dylib', '/usr/local/Cellar/opencv/2.4.7.1/lib/libopencv_photo.dylib'], 'include_dirs': ['/usr/local/Cellar/opencv/2.4.7.1/include/opencv', '/usr/local/Cellar/opencv/2.4.7.1/include']}
['extra_link_args', 'include_dirs']
add lib: opencv_features2d
add lib: opencv_objdetect
add lib: opencv_video
add lib: opencv_imgproc
add lib: opencv_ocl
add lib: opencv_contrib
add lib: opencv_superres
add lib: opencv_nonfree
add lib: opencv_flann
add lib: opencv_legacy
add lib: opencv_core
add lib: opencv_gpu
add lib: opencv_ts
add lib: opencv_videostab
add lib: opencv_highgui
add lib: opencv_ml
add lib: opencv_stitching
add lib: opencv_calib3d
add lib: opencv_photo
===========['stdc++', 'tesseract', 'lept', 'opencv_features2d', 'opencv_objdetect', 'opencv_video', 'opencv_imgproc', 'opencv_ocl', 'opencv_contrib', 'opencv_superres', 'opencv_nonfree', 'opencv_flann', 'opencv_legacy', 'opencv_core', 'opencv_gpu', 'opencv_ts', 'opencv_videostab', 'opencv_highgui', 'opencv_ml', 'opencv_stitching', 'opencv_calib3d', 'opencv_photo']===========
['.', '/usr/local/include/opencv2', '/usr/local/include/opencv']
Traceback (most recent call last):
  File "setup.py", line 251, in <module>
    swig_opts=["-c++", "-I"+inclpath('tesseract'),
TypeError: cannot concatenate 'str' and 'NoneType' objects

2 个答案:

答案 0 :(得分:2)

您无法成功导入它的原因是您从未成功安装它。

setup.py的输出结果显示:它失败并出现异常。

那么,为什么你能import tesseract能够让tesseract.py中的代码失败,而不是甚至无法找到它?我的猜测是你在tesseract源代码树的中间用你当前的工作目录启动了Python,所以找到tesseract.py的源代码副本,即使没有安装任何内容。但是,你肯定可能有一个不完整的,破坏的安装,特别是因为在尝试通过setup.py进行安装之前,你尝试解析错误版本的OS X的二进制副本。

无论如何,你想知道的是如何解决它,而不是为什么它不起作用,对吧?

嗯,就在instructions you linked的最顶端,它说:

  

现在已经过时!!使用homebrew版本请编译为HomeBrew

您所关注的说明未针对Mavericks进行更新,并且链接到专门为10.8制作的二进制包(也可能是针对较旧的MacPorts代码),这是一个很好的信号,表明它们在他们说“过时”,两个感叹号和一切。

在您遵循Homebrew说明之前,除非您对MacPorts有其他需求,否则我建议您完全卸载MacPorts。您通常只需使用sudo rm -rf /opt/local执行此操作,然后修改~/.bash_profile以删除对/opt/local的所有引用,但您可能需要先查看MacPorts网站上的最新说明。如果你害怕这样做,你可以随时sudo mv /opt/local /opt/local-bak,这将留在那里,但不在路上。

此外,请勿使用现有的源代码检查。它有一个破碎的,不完整的构建,谁知道这将如何影响任何再次构建它的尝试。只需完全删除该目录,当您到达说明的svn checkout步骤时,您将获得一个全新的,干净的副本。

然后,从How to compile python-tesseract for Homebrew(Mac Mountain Lion/Maverick)页面的顶部开始。

当你进入第4步时,不要做两个步骤,只做其中一个。我建议第一个版本,从源码构建,因为它可能会更明显,更容易恢复,如果出现任何问题。但是如果你做第二个版本,安装二进制文件,请确保这次获得10.9版本,而不是10.8。

答案 1 :(得分:1)

我遇到了这个问题。唯一的问题是我必须跑:

sudo ldconfig

从命令行运行tesseract -v确认这是否是一个问题。