给定Python代码调用Tesseract的C API和使用ctypes库,在选项#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 =>(快速)[棕色]{狐狸}跳!超过了43,456.78美元的90号狗,鸭子/鹅,占电子邮件的12.5%来自aspammer@website.com是垃圾邮件。Der, schnellez - braune Fuchs春天ï她的过失猎犬。Le renard brown«rapide»炒鸡腿paresseux。拉·马罗尼·拉皮达Salta sopra il cane pigro。El佐罗Marrén répito salta sobre el perroperezoso。一种来自rzipida的raposa mardaSalta sobre o cï ' o preguicoso.
选项#2 => 7?: 5: *:> €"一个€˜——;2 a€";i3E: ?:, i3"。我:活动花絮€˜;3,一个€™f-iA©% :::一个€™::,?: =«€™::=£& lt;: 7 A€˜iA§5。你们;:€"€";:=©:一个€€™". . =。,’;2 a€™:3 a€˜: 3 _3: l。":一个€"一个€˜:€":一个£,¬:-_a€™:§3;;%§% ai5 ~«:©::3% iaa»,¬E:
备注:
- 我尝试了python-tesseract和tightocr库,它们都很好
足够,但缺乏文件 - 这里我使用opencv。我阅读是为了有可能应用数学矩阵 算法
任何想法如何传递OpenCV图像(这是numpy. narray)到Tesseract?
我在python 3中使用这个:(bw_img是一个numpy. narray)
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)