如何在Python中将OpenCV图像传递给Tesseract?
发布时间:2020-12-20 12:08:15 所属栏目:Python 来源:网络整理
导读:鉴于 Python代码调用Tesseract的C API并使用ctypes库,选项#1图像由Tesseract加载,它工作正常!问题出现在选项#2中,当我尝试传递OpenCV加载的图像时,T??esseract返回垃圾: from ctypes import *import cv2class API(Structure): _fields_ = []lang = "eng"ts
鉴于
Python代码调用Tesseract的C API并使用ctypes库,选项#1图像由Tesseract加载,它工作正常!问题出现在选项#2中,当我尝试传递OpenCV加载的图像时,T??esseract返回垃圾:
from ctypes import * import cv2 class API(Structure): _fields_ = [] lang = "eng" ts = cdll.LoadLibrary("c:/Tesseract-OCR/libtesseract302.dll") ts.TessBaseAPICreate.restype = POINTER(API) api = ts.TessBaseAPICreate() rc = ts.TessBaseAPIInit3(api,'c:/Tesseract-OCR/',lang) ##### Option #1 out = ts.TessBaseAPIProcessPages(api,'c:/Tesseract-OCR/doc/eurotext.tif',None,0) print 'Option #1 => ' + string_at(out) ##### Option #2 #TESS_API void TESS_CALL TessBaseAPISetImage(TessBaseAPI* handle,const unsigned char* imagedata,int width,int height,# int bytes_per_pixel,int bytes_per_line); im = cv2.imread('c:/Temp/Downloads/test-slim/eurotext.jpg',cv2.COLOR_BGR2GRAY) c_ubyte_p = POINTER(c_ubyte) ##ts.TessBaseAPISetImage.argtypes = [POINTER(API),c_ubyte_p,c_int,c_int] ts.TessBaseAPISetImage(api,im.ctypes.data_as(c_ubyte_p),800,1024,3,800 * 3) out = ts.TessBaseAPIGetUTF8Text(api) print 'Option #2 => ' + string_at(out) 输出如下:
|