Skip to content

Commit a5cb7b0

Browse files
committed
add cv2 support, mat_to_text()
1 parent d055249 commit a5cb7b0

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

pytesser.py

+21-6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
except ImportError:
55
OPENCV_AVAILABLE = False
66

7+
try:
8+
import cv2
9+
OPENCV2_AVAILABLE = True
10+
except ImportError:
11+
OPENCV2_AVAILABLE = False
12+
713
from subprocess import Popen, PIPE
814
import os
915

@@ -47,8 +53,8 @@ def process_request(input_file, output_file, lang=None, psm=None):
4753
args.append(str(psm))
4854
proc = Popen(args, stdout=PIPE, stderr=PIPE) #Open process
4955
ret = proc.communicate() #Launch it
50-
51-
code = proc.returncode
56+
57+
code = proc.returncode
5258
if code != 0:
5359
if code == 2:
5460
raise TesseractException, "File not found"
@@ -66,15 +72,24 @@ def iplimage_to_string(im, lang=None, psm=None):
6672
txt = image_to_string(TEMP_IMAGE, lang, psm)
6773
os.remove(TEMP_IMAGE)
6874
return txt
69-
75+
7076
def image_to_string(file,lang=None, psm=None):
7177
check_path() #Check if tesseract available in the path
7278
process_request(file, TEMP_FILE, lang, psm) #Process command
7379
f = open(TEMP_FILE+".txt","r") #Open back the file
7480
txt = f.read()
7581
os.remove(TEMP_FILE+".txt")
7682
return txt
77-
78-
83+
84+
def mat_to_string(im, lang=None, psm=None):
85+
if not OPENCV2_AVAILABLE:
86+
print "cv2 is not Available"
87+
return -1
88+
else:
89+
cv2.imwrite(TEMP_IMAGE, im)
90+
txt = image_to_string(TEMP_IMAGE, lang, psm)
91+
os.remove(TEMP_IMAGE)
92+
return txt
93+
7994
if __name__ =='__main__':
80-
print image_to_string("image.jpg", "fra", PSM_AUTO) #Example
95+
print image_to_string("image.jpg", "fra", PSM_AUTO) #Example

0 commit comments

Comments
 (0)