前面已经写过如何使用python调用tesseract API了,这里说的是如何使用tesseract的LSTM模式。tesseract 4.0已经加入LSTM了,在用命令行执行的时候,添加 “–oem 1”参数即可,但是pythonocr模块里并没有提供使用oem参数的init函数,查看tesseract的源码,capi.cpp定位到257行有:

TESS_API int TESS_CALL TessBaseAPIInit1(TessBaseAPI* handle, const char* datapath, const char* language, TessOcrEngineMode oem,
                                        char** configs, int configs_size)
{
    return handle->Init(datapath, language, oem, configs, configs_size, nullptr, nullptr, false);
}

TESS_API int TESS_CALL TessBaseAPIInit2(TessBaseAPI* handle, const char* datapath, const char* language, TessOcrEngineMode oem)
{
    return handle->Init(datapath, language, oem);
}

TESS_API int TESS_CALL TessBaseAPIInit3(TessBaseAPI* handle, const char* datapath, const char* language)
{
    return handle->Init(datapath, language);
}

其中TessBaseAPIInit2()函数就是我们需要的,其实已经导出在了tesseract.so文件中,需要我们声明一下才能使用。打开pythonocr安装目录下的tesseract_raw.py文件,定位到148行,可以看到对init1和init3的函数声明,那么加入init2的函数声明即可,修改后如下:

    g_libtesseract.TessBaseAPIInit1.argtypes = [
        ctypes.c_void_p,  # TessBaseAPI*
        ctypes.c_char_p,  # datapath
        ctypes.c_char_p,  # language
        ctypes.c_int,  # TessOcrEngineMode
        ctypes.POINTER(ctypes.c_char_p),  # configs
        ctypes.c_int,  # configs_size
    ]
    g_libtesseract.TessBaseAPIInit1.restype = ctypes.c_int

    # 添加的对init2的函数声明
    g_libtesseract.TessBaseAPIInit2.argtypes = [
        ctypes.c_void_p,  # TessBaseAPI*
        ctypes.c_char_p,  # datapath
        ctypes.c_char_p,  # language
        ctypes.c_int,  # TessOcrEngineMode
    ]
    g_libtesseract.TessBaseAPIInit2.restype = ctypes.c_int

    g_libtesseract.TessBaseAPIInit3.argtypes = [
        ctypes.c_void_p,  # TessBaseAPI*
        ctypes.c_char_p,  # datapath
        ctypes.c_char_p,  # language
    ]
    g_libtesseract.TessBaseAPIInit3.restype = ctypes.c_int

然后定位到351行,这里是pythonocr的init函数实现,修改成如下:

def init(lang=None, oem = 0):
    assert(g_libtesseract)
    handle = g_libtesseract.TessBaseAPICreate()
    try:
        if lang:
            lang = lang.encode("utf-8")
        prefix = None
        if TESSDATA_PREFIX:
            prefix = TESSDATA_PREFIX.encode("utf-8")
        g_libtesseract.TessBaseAPIInit2(
            ctypes.c_void_p(handle),
            ctypes.c_char_p(prefix),
            ctypes.c_char_p(lang),
            oem
        )
        g_libtesseract.TessBaseAPISetVariable(
            ctypes.c_void_p(handle),
            b"tessedit_zero_rejection",
            b"F"
        )
    except:
        g_libtesseract.TessBaseAPIDelete(ctypes.c_void_p(handle))
        raise
    return handle

在外部调用的时候,只需要将以前的

handle = tesseract_raw.init(lang='eng')

修改成:

handle = tesseract_raw.init(lang='eng', oem=1)

即可。下载最新支持lstm的tessdata数据包,识别结果会比之前有大大的提高!如何在调用API的时候使用多语言,就如同命令行下的 -l eng+chi这种,还在摸索中,如果谁知道,请麻烦告知,谢谢!

GitHub 加速计划 / te / tesseract
11
3
下载
tesseract-ocr/tesseract: 是一个开源的光学字符识别(OCR)引擎,适用于从图像中提取和识别文本。特点是可以识别多种语言,具有较高的识别准确率,并且支持命令行和API调用。
最近提交(Master分支:3 个月前 )
bc490ea7 Don't check for a directory, because a symbolic link is also allowed. Signed-off-by: Stefan Weil <sw@weilnetz.de> 5 个月前
2991d36a - 5 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐