How can I pass an OpenCV image to Tesseract in python?

Considering the Python code calling the Tesseract`s API and using the ctypes library , Option # 1 sample loads Tesseract and it works great! The problem is with option # 2, when I try to pass an image loaded by OpenCV, Tesseract returns garbage:

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)

      

and the output is as follows:

Option # 1 => (fast) [brown] {fox} jumping! Over $ 43,456.78 dog # 90 and duck / goose like 12.5% ​​of email from aspammer@website.com is spam. Der, schnelleâ € ™ Braune Fuchs springt ï¬ ber den faulen Hund. Le renard brun "Rapide" saute par-dessus le chien paresseux. La volpe marrone rapida salta sopra il reed. El zorro marrén rà © pido salta sobre el perro perezoso. Raposa marrom rzipida salta sobre o cï¬ o preguicoso.

Option # 2 => 7 ?: 5: *:> \ â € "œ, 2â €"; i3E:?:; i3 ".i: iiâ € ~; 3; f- ià ©%: â € ™ ::;: = Â" â € ™ :: = A £ <: 7A € ~ i§5 <.:  € "' \ â € ";: = à ©: â € ™ â €" .. = .: a, '; 2 € ™: 3, € ~: 3_3: l': â € "â € ~: â €" :  £ â, ¬: -_ â € ™: §3 ;;% §% AI5 ~  ": à © :: 3% IAA" â, ¬E:

Notes:

  • I tried the python-tesseract and tightocr libraries which are good
    enough but lack documentation
  • here i am using opencv.imread to be able to apply math algorithms on a matrix

Any ideas on how to pass the OpenCV image (which is numpy.ndarray) to Tesseract? Any help would be helpful.

+3


source to share


1 answer


I have been using this with python 3: (bw_img is 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)

      

+9


source







All Articles