-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dc1468a
commit 2b0d214
Showing
2 changed files
with
108 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,31 @@ | ||
Pytesser | ||
======== | ||
|
||
Python wrapper for the tesseract OCR engine. The module is based on OpenCV | ||
Python wrapper for the tesseract OCR engine. The module is based on OpenCV. | ||
Article : http://robindavid.comli.com/pytesser-python-wrapper-for-the-tesseract-ocr-engine/ | ||
|
||
Informations | ||
------------ | ||
|
||
There is already multiples module called pytesser, but this one is slightly different on the following point: | ||
|
||
* It implement all the features of tesseract engine it includes the choise of the language and the page segmentation mode. | ||
* All the module is contained in one file (the others modules I have tried are quite messy. | ||
* It support OpenCV, so you can directly provide an IplImage to the module. | ||
|
||
How to use it ? | ||
--------------- | ||
|
||
There is to ways to use it. Either you give it a filename, either directly an IplImage. For a filename you can do: | ||
|
||
import pytesser | ||
txt = pytesser.image_to_string("myimage.jpg") #By default language is eng, and page seg mode auto | ||
|
||
#To give specifify parameters: | ||
txt = pytesser.image_to_string("myimage.jpg","fra",pytesser.PSM_SINGLE_WORD) #Analyse image as a single french word | ||
|
||
|
||
Or you can directly give it an IplImage like this: | ||
|
||
image = cv.LoadImage("myimage.jpg") | ||
txt = pytesser.iplimage_to_string(image) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
try: | ||
import cv2.cv as cv | ||
OPENCV_AVAILABLE = True | ||
except ImportError: | ||
OPENCV_AVAILABLE = False | ||
|
||
from subprocess import Popen, PIPE | ||
import os | ||
|
||
PROG_NAME = 'tesseract' | ||
TEMP_IMAGE = 'tmp.bmp' | ||
TEMP_FILE = 'tmp' | ||
|
||
#All the PSM arguments as a variable name (avoid having to know them) | ||
PSM_OSD_ONLY = 0 | ||
PSM_SEG_AND_OSD = 1 | ||
PSM_SEG_ONLY = 2 | ||
PSM_AUTO = 3 | ||
PSM_SINGLE_COLUMN = 4 | ||
PSM_VERTICAL_ALIGN = 5 | ||
PSM_UNIFORM_BLOCK = 6 | ||
PSM_SINGLE_LINE = 7 | ||
PSM_SINGLE_WORD = 8 | ||
PSM_SINGLE_WORD_CIRCLE = 9 | ||
PSM_SINGLE_CHAR = 10 | ||
|
||
class TesseractException(Exception): #Raised when tesseract does not return 0 | ||
pass | ||
|
||
class TesseractNotFound(Exception): #When tesseract is not found in the path | ||
pass | ||
|
||
def check_path(): #Check if tesseract is in the path raise TesseractNotFound otherwise | ||
for path in os.environ.get('PATH', '').split(':'): | ||
filepath = os.path.join(path, PROG_NAME) | ||
if os.path.exists(filepath) and not os.path.isdir(filepath): | ||
return True | ||
raise TesseractNotFound | ||
|
||
def process_request(input_file, output_file, lang=None, psm=None): | ||
args = [PROG_NAME, input_file, output_file] #Create the arguments | ||
if lang is not None: | ||
args.append("-l") | ||
args.append(lang) | ||
if psm is not None: | ||
args.append("-psm") | ||
args.append(str(psm)) | ||
proc = Popen(args, stdout=PIPE, stderr=PIPE) #Open process | ||
ret = proc.communicate() #Launch it | ||
|
||
code = proc.returncode | ||
if code != 0: | ||
if code == 2: | ||
raise TesseractException, "File not found" | ||
if code == -11: | ||
raise TesseractException, "Language code invalid: "+ret[1] | ||
else: | ||
raise TesseractException, ret[1] | ||
|
||
def iplimage_to_string(im, lang=None, psm=None): | ||
if not OPENCV_AVAILABLE: | ||
print "OpenCV not Available" | ||
return -1 | ||
else: | ||
cv.SaveImage(TEMP_IMAGE, im) | ||
txt = image_to_string(TEMP_IMAGE, lang, psm) | ||
os.remove(TEMP_IMAGE) | ||
return txt | ||
|
||
def image_to_string(file,lang=None, psm=None): | ||
check_path() #Check if tesseract available in the path | ||
process_request(file, TEMP_FILE, lang, psm) #Process command | ||
f = open(TEMP_FILE+".txt","r") #Open back the file | ||
txt = f.read() | ||
os.remove(TEMP_FILE+".txt") | ||
return txt | ||
|
||
|
||
if __name__ =='__main__': | ||
print image_to_string("image.jpg", "fra", PSM_AUTO) #Example |