Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
94 views

Code Snippets

This document contains 3 examples of using the Python Tesseract OCR library. The first example loads an image, initializes the Tesseract API, and prints the recognized text and confidence level. The second example initializes the API, sets character whitelisting, performs OCR on a buffer, and prints the word confidence data. The third example enhances an image using edge detection, blurring, and thresholding before using Tesseract to recognize the text.

Uploaded by

FIKRUL ISLAMY
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
94 views

Code Snippets

This document contains 3 examples of using the Python Tesseract OCR library. The first example loads an image, initializes the Tesseract API, and prints the recognized text and confidence level. The second example initializes the API, sets character whitelisting, performs OCR on a buffer, and prints the word confidence data. The third example enhances an image using edge detection, blurring, and thresholding before using Tesseract to recognize the text.

Uploaded by

FIKRUL ISLAMY
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

python-tesseract

Code Snippets
Example 1
import cv2.cv as cv import tesseract image=cv.LoadImage("foo.png", cv.CV_LOAD_IMAGE_GRAYSCALE) api = tesseract.TessBaseAPI() api.Init(".","eng",tesseract.OEM_DEFAULT) #api.SetPageSegMode(tesseract.PSM_SINGLE_WORD) api.SetPageSegMode(tesseract.PSM_AUTO) tesseract.SetCvImage(image,api) text=api.GetUTF8Text() conf=api.MeanTextConf() print text

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 intPtr=api.AllWordConfidences() print str(intPtr) pyPtrRaw=tesseract.cdata(intPtr,300) print len(pyPtrRaw),len(mBuffer) pyPtr=[ord(data) for i,data in enumerate(pyPtrRaw)] print pyPtr tesseract.delete_intp(intPtr)

Example 3 How to do image edge enhancing and tesseract ocr


import import import import tesseract cv2 cv2.cv as cv numpy as np

scale = 1 delta = 0 ddepth = cv2.CV_16S gray=cv2.imread("an91cut.jpg")

### trim the edges cut_offset=23 gray=gray[cut_offset:-cut_offset,cut_offset:-cut_offset] ### convert to gray color gray = cv2.cvtColor(gray,cv2.COLOR_BGR2GRAY) ### edge enhancing by Sobeling # Gradient-X grad_x = cv2.Sobel(gray,ddepth,1,0,ksize = 3, scale = scale, delta = delta,borderType = cv2.BORDER_DEFAULT) #grad_x = cv2.Scharr(gray,ddepth,1,0) # Gradient-Y grad_y = cv2.Sobel(gray,ddepth,0,1,ksize = 3, scale = scale, delta = delta, borderType = cv2.BORDER_DEFAULT) #grad_y = cv2.Scharr(gray,ddepth,0,1)
abs_grad_x = cv2.convertScaleAbs(grad_x) # converting back to uint8 abs_grad_y = cv2.convertScaleAbs(grad_y) gray = cv2.addWeighted(abs_grad_x,0.5,abs_grad_y,0.5,0)

### Bluring image1 = cv2.medianBlur(gray,5) image1[image1 < 50]= 255 image1 = cv2.GaussianBlur(image1,(31,13),0) color_offset=230 image1[image1 >= color_offset]= 255 image1[image1 < color_offset ] = 0 #black #### Insert White Border offset=30

1 of 3

2/4/2014 5:24 PM

CodeSnippets - python-tesseract - Reusable Code Snippets - python w...

http://code.google.com/p/python-tesseract/wiki/CodeSnippets

height,width = image1.shape image1=cv2.copyMakeBorder(image1,offset,offset,offset,offset,cv2.BORDER_CONSTANT,value=(255,255,255)) cv2.namedWindow("Test") cv2.imshow("Test", image1) cv2.imwrite("an91cut_decoded.jpg",image1) cv2.waitKey(0) cv2.destroyWindow("Test") ### tesseract OCR api = tesseract.TessBaseAPI() api.Init(".","eng",tesseract.OEM_DEFAULT) #api.SetPageSegMode(tesseract.PSM_AUTO) #as suggested by zdenko podobny <zdenop@gmail.com>, #using PSM_SINGLE_BLOCK will be more reliable for ocr-ing a line of word. api.SetPageSegMode(tesseract.PSM_SINGLE_BLOCK) height1,width1 = image1.shape channel1=1 image = cv.CreateImageHeader((width1,height1), cv.IPL_DEPTH_8U, channel1) cv.SetData(image, image1.tostring(),image1.dtype.itemsize * channel1 * (width1)) tesseract.SetCvImage(image,api) text=api.GetUTF8Text() conf=api.MeanTextConf() image=None print "..............." print "Ocred Text: %s"%text print "Cofidence Level: %d %%"%conf

an91cut.jpg enhanced image-->

2 of 3

2/4/2014 5:24 PM

You might also like