Skip to content

Commit

Permalink
Merge pull request #1 from owlran/master
Browse files Browse the repository at this point in the history
support cv2, update README and pytesser
  • Loading branch information
RobinDavid committed Sep 13, 2014
2 parents d055249 + cb96581 commit 397171c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,8 @@ Or you can directly give it an IplImage like this:

image = cv.LoadImage("myimage.jpg")
txt = pytesser.iplimage_to_string(image)

Or give it a mat:

image = cv2.imwrite("myimage.jpg")
txt = pytesser.mat_to_string(image)
27 changes: 21 additions & 6 deletions pytesser.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
except ImportError:
OPENCV_AVAILABLE = False

try:
import cv2
OPENCV2_AVAILABLE = True
except ImportError:
OPENCV2_AVAILABLE = False

from subprocess import Popen, PIPE
import os

Expand Down Expand Up @@ -47,8 +53,8 @@ def process_request(input_file, output_file, lang=None, psm=None):
args.append(str(psm))
proc = Popen(args, stdout=PIPE, stderr=PIPE) #Open process
ret = proc.communicate() #Launch it
code = proc.returncode

code = proc.returncode
if code != 0:
if code == 2:
raise TesseractException, "File not found"
Expand All @@ -66,15 +72,24 @@ def iplimage_to_string(im, lang=None, psm=None):
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



def mat_to_string(im, lang=None, psm=None):
if not OPENCV2_AVAILABLE:
print "cv2 is not Available"
return -1
else:
cv2.imwrite(TEMP_IMAGE, im)
txt = image_to_string(TEMP_IMAGE, lang, psm)
os.remove(TEMP_IMAGE)
return txt

if __name__ =='__main__':
print image_to_string("image.jpg", "fra", PSM_AUTO) #Example
print image_to_string("image.jpg", "fra", PSM_AUTO) #Example

0 comments on commit 397171c

Please sign in to comment.