Linux中的Python OCR模块?

21 投票
5 回答
16949 浏览
提问于 2025-04-16 16:29

我想在Linux上找到一个好用的OCR(光学字符识别)Python模块。我找到了pytesser这个模块,http://code.google.com/p/pytesser/,但是里面有一个.exe的可执行文件。

我试着把代码改成用wine来运行,确实可以,但速度太慢了,真的不是个好主意。

有没有什么在Linux上也能像它一样好用的替代品呢?

5 个回答

6

python tesseract

http://code.google.com/p/python-tesseract

import cv2.cv as cv
import tesseract

api = tesseract.TessBaseAPI()
api.Init(".","eng",tesseract.OEM_DEFAULT)
api.SetPageSegMode(tesseract.PSM_AUTO)

image=cv.LoadImage("eurotext.jpg", cv.CV_LOAD_IMAGE_GRAYSCALE)
tesseract.SetCvImage(image,api)
text=api.GetUTF8Text()
conf=api.MeanTextConf()
11

除了Blender提到的直接运行Tesseract这个程序之外,我想补充一下,还有其他一些可以用来进行文字识别(OCR)的工具,它们也可以作为外部程序来调用。

比如,ABBYY的命令行OCR工具:http://ocr4linux.com/en:start

这个工具不是免费的,所以只有在Tesseract的识别准确率不够好,或者你需要更复杂的排版分析,或者需要导出PDF、Word等文件时,才值得考虑。

更新:这里有一个关于ABBYY和Tesseract准确率的比较:http://www.splitbrain.org/blog/2010-06/15-linux_ocr_software_comparison

免责声明:我在ABBYY工作。

17

你可以把 tesseract 放在一个函数里,这样使用起来会更方便:

import os
import tempfile
import subprocess

def ocr(path):
    temp = tempfile.NamedTemporaryFile(delete=False)

    process = subprocess.Popen(['tesseract', path, temp.name], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    process.communicate()

    with open(temp.name + '.txt', 'r') as handle:
        contents = handle.read()

    os.remove(temp.name + '.txt')
    os.remove(temp.name)

    return contents

如果你想要更复杂的功能,比如文档分割,可以试试 OCRopus

撰写回答