我使用ctypes
和tesseract 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())
答案 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
修改强>
...
如果您不介意更换时会发生什么
text_out = tesseract.TessBaseAPIProcessPages(api, filename, None, 0)
带
text_out = tesseract.ProcessPagesBuffer(mBuffer,len(mBuffer),api)