Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中将OpenCV图像传递给Tesseract?_Python_Opencv_Numpy_Tesseract - Fatal编程技术网

如何在python中将OpenCV图像传递给Tesseract?

如何在python中将OpenCV图像传递给Tesseract?,python,opencv,numpy,tesseract,Python,Opencv,Numpy,Tesseract,给定调用Tesseract的C API并使用ctypes库的Python代码,在选项#1中,Tesseract正在加载图像,它工作正常!问题在于选项#2,当我尝试传递OpenCV加载的图像时,Tesseract返回垃圾: from ctypes import * import cv2 class API(Structure): _fields_ = [] lang = "eng" ts = cdll.LoadLibrary("c:/Tesseract-OCR/libtesseract

给定调用Tesseract的C API并使用ctypes库的Python代码,在选项#1中,Tesseract正在加载图像,它工作正常!问题在于选项#2,当我尝试传递OpenCV加载的图像时,Tesseract返回垃圾:

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, 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)
输出结果如下:

选项#1=>快速[棕色]{fox}跳跃! 超过$43456.78#90的狗 &鸭子/鹅,占电子邮件的12.5% 从…起aspammer@website.com这是垃圾邮件。 德,施奈尔� 布劳恩·富克斯·斯普林特 ï¬�我是福伦·亨德。勒勒纳尔布伦酒店 «rapide»爆香乐干 帕雷索。拉沃尔佩马隆拉皮达酒店 萨尔塔·索普拉·卡恩·皮格罗。埃尔佐罗 马尔©n r©皮多·萨尔塔·索布雷·埃尔·佩罗 佩雷佐索。拉波萨万豪酒店 萨尔塔·索布雷�o preguicoso

选项2=>7::5:>\;2—;i3E:?:;i:ii–欧元;3;欧元™ f-iÃ%:-欧元™::;?:=«’:: =p我将其用于Python3:(bw_img是一个numpy.ndarray)


令人惊讶的是……事实证明,它被解决了,……多亏了@eryksun和stackoverflow!
import numpy as np
import cv2
from PIL import Image
import pytesseract

...

(thresh, bw_img) = cv2.threshold(bw_img, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
...

img = Image.fromarray(bw_img)
txt = pytesseract.image_to_string(img)
print(txt)