如何使用ctypes和tesseract 3.0.2识别数据而不是文件名?

时间:2012-10-31 03:40:49

标签: python ctypes tesseract

我使用ctypestesseract 3.0.2编写了一个代码段,引用了example

import ctypes
from PIL import Image


libname = '/opt/tesseract/lib/libtesseract.so.3.0.2'
tesseract = ctypes.cdll.LoadLibrary(libname)
api = tesseract.TessBaseAPICreate()

rc = tesseract.TessBaseAPIInit3(api, "", 'eng')
filename = '/opt/ddl.ddl.exp654.png'

text_out = tesseract.TessBaseAPIProcessPages(api, filename, None, 0)
result_text = ctypes.string_at(text_out)
print result_text

它将filename作为参数传递,我不知道在API中调用哪个方法来传递原始数据,如:

tesseract.TessBaseAPIWhichMethod(api, open(filename).read())

1 个答案:

答案 0 :(得分:0)

我不能肯定地说,但我认为你不能将复杂的python对象传递给那个特定的API,它不知道如何处理它们。你最好的选择是查看像http://code.google.com/p/python-tesseract/这样的包装器,它允许你使用文件缓冲区

import tesseract
api = tesseract.TessBaseAPI()
api.Init(".","eng",tesseract.OEM_DEFAULT)
api.SetVariable("tessedit_char_whitelist", "0123456789abcdefghijklmnopqrstuvwxyz")
api.SetPageSegMode(tesseract.PSM_AUTO)

mImgFile = "eurotext.jpg"
mBuffer=open(mImgFile,"rb").read()
result = tesseract.ProcessPagesBuffer(mBuffer,len(mBuffer),api) #YAY for buffers.
print "result(ProcessPagesBuffer)=",result

修改

http://code.google.com/p/python-tesseract/source/browse/python-tesseract-0.7.4/debian/python-tesseract/usr/share/pyshared/tesseract.py可能会为您提供所需的洞察力。

...

如果您不介意更换时会发生什么

text_out = tesseract.TessBaseAPIProcessPages(api, filename, None, 0)

text_out = tesseract.ProcessPagesBuffer(mBuffer,len(mBuffer),api)