在Linux中构建便携式Tesseract OCR库

时间:2015-06-04 12:34:47

标签: python linux centos ctypes tesseract

有没有办法构建和使用Tesseract库和相应的Leptonica库(因为Tesseract依赖于Leptonica),因为它可以在Windows中完成?

我按照他们的说明编译了这些库,但似乎libtesseract.so.3.0.2包含了Leptonica共享库的固定路径:

$ ldd libtesseract.so.3.0.2

linux-vdso.so.1 =>  (0x00007fffbc5ff000)
**liblept.so.4 => /usr/local/lib/liblept.so.4 (0x00007fa8400fd000)**
libpng12.so.0 => /usr/lib64/libpng12.so.0 (0x00007fa83fcae000)
libjpeg.so.62 => /usr/lib64/libjpeg.so.62 (0x00007fa83fa5e000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x00007fa83f5e4000)
libstdc++.so.6 => /usr/lib64/libstdc++.so.6 (0x00007fa83f2de000)
libm.so.6 => /lib64/libm.so.6 (0x00007fa83f059000)
libc.so.6 => /lib64/libc.so.6 (0x00007fa83ecc5000)
libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00007fa83eaaf000)
/lib64/ld-linux-x86-64.so.2 (0x0000003080200000)

在未安装Leptonica的工作站上运行应用程序时会导致OSError:

OSError: liblept.so.4: cannot open shared object file: No such file or directory

典型用例如下(tesseract和leptonica库在同一个文件夹中):

import ctypes
import os
import sys

lang = 'eng'

os.putenv('TESSDATA_PREFIX', ".")
tessdata = os.environ.get('TESSDATA_PREFIX')
tess_libpath = "."
tess_libname = "libtesseract.so.3.0.2"
# tess_libname = "libtesseract302.dll" works in Windows, no need to add a leponica library file

os.environ["PATH"] += os.pathsep + tess_libpath
tesseract = None

try:
    tesseract = ctypes.cdll.LoadLibrary(os.path.join(tess_libpath, tess_libname))
except OSError, err:
    raise

class _TessBaseAPI(ctypes.Structure): pass
TessBaseAPI = ctypes.POINTER(_TessBaseAPI)
tesseract.TessBaseAPICreate.restype = TessBaseAPI
tesseract.TessBaseAPIDelete.restype = None
tesseract.TessBaseAPIDelete.argtypes = [TessBaseAPI]
tesseract.TessBaseAPIInit3.argtypes = [TessBaseAPI,
                                   ctypes.c_char_p,
                                   ctypes.c_char_p]
tesseract.TessBaseAPISetImage.restype = None
tesseract.TessBaseAPISetImage.argtypes = [TessBaseAPI,
                                      ctypes.c_void_p,
                                      ctypes.c_int,
                                      ctypes.c_int,
                                      ctypes.c_int,
                                      ctypes.c_int]
 tesseract.TessBaseAPIGetUTF8Text.restype = ctypes.c_char_p
 tesseract.TessBaseAPIGetUTF8Text.argtypes = [TessBaseAPI]

我在配置Tesseract时尝试添加选项--disable-shared --enable-static,但它没有成功。

在我的情况下,目标操作系统是CentOS 6.5,但我也很感激任何一般性答案。

1 个答案:

答案 0 :(得分:1)

ldd无法告诉您库中是否存在绝对路径。相反,它使用标准的共享库搜索路径并打印它找到的内容。

要检查加载是否也可以从其他文件夹中运行,请尝试以下操作:

> mkdir tmp
> cd tmp
> cp /usr/local/lib/liblept.so.4
> LD_LIBRARY_PATH=$PWD:$LD_LIBRARY_PATH ldd libtesseract.so.3.0.2

现在应该显示liblept.so.4文件夹中的tmp