加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

如何在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)

输出如下:

Option #1 => The (quick) [brown] {fox} jumps!
Over the $43,456.78 #90 dog
& duck/goose,as 12.5% of E-mail
from aspammer@website.com is spam.
Der,schnellea

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读