python-tesseract


python wrapper class for tesseract OCR (Linux & Mac & Windows)

If you find python-tesseract useful, please consider buying me a coffee.

Beta Testing

Python3-Tesseract Beta Testing

Downloads

Downloads Page in BitBucket

===_version 0.9 adds support to ResultIterator and AllWordConfidences()===see example 4 below####For details, please refer to example 3 in wiki-codesnippets#### remember to install opencv and numpy_

#

Ubuntu Trusty x64 -python-tesseract_0.9-0.5ubuntu0_amd64.deb

Windows 7 x64 -python-tesseract-0.9-0.4.win-amd64-py2.7.exe<-thanks to Max Pole & Dustin Spicuzza

remember to put tessdata in the same path with your python program!!

Windows 7 x86 python-tesseract-0.9-0.4.win32-py2.7.exe

Mac 10.10 (Homebrew)

For details, refer to the wiki page. easy_install https://bitbucket.org/3togo/python-tesseract/downloads/python_tesseract-0.9.1-py2.7-macosx-10.10-x86_64.egg

Python Wrapper Class for Tesseract

(Linux & Mac OS X & Windows)

Python-tesseract is a wrapper class for Tesseract OCR that allows any conventional image files (JPG, GIF ,PNG , TIFF and etc) to be read and decoded into readable languages. No temporary file will be created during the OCR processing.

Windows versions are available now!

<BR>

remember to

<BR>

  1. set PATH: e.g. PATH=%PATH%;C:\PYTHON27 Details

<BR>

  1. set c:\python27\python.exe to be compatible to**Windows 7**even though you are using windows 7. Otherwise the program might crash during runtime Details

<BR>

  1. Download and install all of them

<BR>

python-opencv numpy

<BR>

  1. unzip the sample code and keep your fingers crossed Sample Codes

<BR>

  1. python -u test.py

<BR>

it is always safer to run python in unbuffered mode especially for windows XP

<BR>

Example 1: import tesseract api = tesseract.TessBaseAPI() api.SetOutputName("outputName"); api.Init(".","eng",tesseract.OEM_DEFAULT) api.SetPageSegMode(tesseract.PSM_AUTO) mImgFile = "eurotext.jpg" pixImage=tesseract.pixRead(mImgFile) api.SetImage(pixImage) outText=api.GetUTF8Text() print("OCR output:\n%s"%outText); api.End() Example 2: ``` import tesseract api = tesseract.TessBaseAPI() api.Init(".","eng",tesseract.OEM_DEFAULT) api.SetVariable("tessedit_char_whitelist", "0123456789abcdefghijklmnopqrstuvwxyz") api.SetPageSegMode(tesseract.PSM_AUTO)

mImgFile = "eurotext.jpg" mBuffer=open(mImgFile,"rb").read() result = tesseract.ProcessPagesBuffer(mBuffer,len(mBuffer),api) print "result(ProcessPagesBuffer)=",result api.End() **Example 3:** import cv2.cv as cv import tesseract

api = tesseract.TessBaseAPI() api.Init(".","eng",tesseract.OEM_DEFAULT) api.SetPageSegMode(tesseract.PSM_AUTO)

image=cv.LoadImage("eurotext.jpg", cv.CV_LOAD_IMAGE_GRAYSCALE) tesseract.SetCvImage(image,api) text=api.GetUTF8Text() conf=api.MeanTextConf() print text api.End() ```

Example 4: ``` import tesseract import cv2 import cv2.cv as cv

image0=cv2.imread("p.bmp")

you may need to thicken the border in order to make tesseract feel happy to ocr your image

offset=20 height,width,channel = image0.shape image1=cv2.copyMakeBorder(image0,offset,offset,offset,offset,cv2.BORDER_CONSTANT,value=(255,255,255))

cv2.namedWindow("Test")

cv2.imshow("Test", image1)

cv2.waitKey(0)

cv2.destroyWindow("Test")

#

api = tesseract.TessBaseAPI() api.Init(".","eng",tesseract.OEM_DEFAULT) api.SetPageSegMode(tesseract.PSM_AUTO) height1,width1,channel1=image1.shape print image1.shape print image1.dtype.itemsize width_step = width*image1.dtype.itemsize print width_step

method 1

iplimage = cv.CreateImageHeader((width1,height1), cv.IPL_DEPTH_8U, channel1) cv.SetData(iplimage, image1.tostring(),image1.dtype.itemsize * channel1 * (width1)) tesseract.SetCvImage(iplimage,api)

text=api.GetUTF8Text() conf=api.MeanTextConf() image=None print "..............." print "Ocred Text: %s"%text print "Cofidence Level: %d %%"%conf

method 2:

cvmat_image=cv.fromarray(image1) iplimage =cv.GetImage(cvmat_image) print iplimage

tesseract.SetCvImage(iplimage,api)

api.SetImage(m_any,width,height,channel1)

text=api.GetUTF8Text() conf=api.MeanTextConf() image=None print "..............." print "Ocred Text: %s"%text print "Cofidence Level: %d %%"%conf api.End() **Example 6:** import tesseract import cv2 import cv2.cv as cv image0=cv2.imread("eurotext.jpg") offset=20 height,width,channel = image0.shape image1=cv2.copyMakeBorder(image0,offset,offset,offset,offset,cv2.BORDER_CONSTANT,value=(255,255,255))

api = tesseract.TessBaseAPI() api.Init(".","eng",tesseract.OEM_DEFAULT) api.SetPageSegMode(tesseract.PSM_AUTO) height1,width1,channel1=image1.shape print image1.shape print image1.dtype.itemsize width_step = width*image1.dtype.itemsize print width_step

iplimage = cv.CreateImageHeader((width1,height1), cv.IPL_DEPTH_8U, channel1) cv.SetData(iplimage, image1.tostring(),image1.dtype.itemsize * channel1 * (width1)) tesseract.SetCvImage(iplimage,api) api.Recognize(None) ri=api.GetIterator() level=tesseract.RIL_WORD count=0 while (ri): word = ri.GetUTF8Text(level) conf = ri.Confidence(level) print "[%03d]:\tword(confidence)=%s(%.2f%%)"%(count,word,conf) #ri.BoundingBox(level,x1,y1,x2,y2) count+=1 if not ri.Next(level): break

iplimage=None api.End() ``` p.bmp

More Examples

Sample Codes

Project Information

Labels:
Python Library Wrapper OpenCV Tesseract Leptonica Kaptcha