2015-05-22 36 views
2

鉴于Python代码调用Tesseract的C API和使用ctypes库,在选项#1图像正在由Tesseract加载,它工作正常!问题是在选项#2,当我尝试路过的OpenCV的超正方体加载的图像返回垃圾:如何将OpenCV图像传递给python中的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 =>的(快速)[棕色] {狐狸}跳跃! 超过$ 43,456.78#90狗 &鸭/鹅,因为12.5%的电子邮件 来自[email protected]是垃圾邮件。 Der ,,schnelle–braune Fuchs弹簧 ï¼¼berber faulen Hund。 Le renard brun «rapide»saute par-dessus le chien paresseux。 La volpe marrone rapida salta sopra il cane pigro。 El zorro marrénrépido salta sobre el perro perezoso。 raposa marrom rzipida salta sobre ocï¬ opreguicoso。

选项#2 => 7?:5:*:> \ - “ - ; 2”; i3E:?:i3“.i:ii”; 3;“f -i©%::: ::: ::?:=«:: :: = <:7→5 >> <:'': - Ã::'.. = .: a,'; 2':3'':3_3:'。:':'::£: -_’:§3;;%§%AI5〜一«:é:: 3%的IAA»â,¬E:

备注:

  • 我试过python- tesseract和tigh TOCR库,这是很好的
    足够,但缺乏文档
  • 在这里我才能有可能对矩阵应用数学 算法

任何想法如何通过OpenCV的图像(这是使用opencv.imread numpy.ndarray)到Tesseract?任何帮助都会有用。

+0

惊人......事实证明,它解决了,[链接](http://stackoverflow.com/questions/21745205/using-c-api-of-tesseract-3-02-with-ctypes-and-cv2-in-python?rq = 1)..感谢@eryksun和stackoverflow! – Agv

回答

8

我用这与Python 3:(bw_img是numpy.ndarray)

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) 
相关问题