4
4
except ImportError :
5
5
OPENCV_AVAILABLE = False
6
6
7
+ try :
8
+ import cv2
9
+ OPENCV2_AVAILABLE = True
10
+ except ImportError :
11
+ OPENCV2_AVAILABLE = False
12
+
7
13
from subprocess import Popen , PIPE
8
14
import os
9
15
@@ -47,8 +53,8 @@ def process_request(input_file, output_file, lang=None, psm=None):
47
53
args .append (str (psm ))
48
54
proc = Popen (args , stdout = PIPE , stderr = PIPE ) #Open process
49
55
ret = proc .communicate () #Launch it
50
-
51
- code = proc .returncode
56
+
57
+ code = proc .returncode
52
58
if code != 0 :
53
59
if code == 2 :
54
60
raise TesseractException , "File not found"
@@ -66,15 +72,24 @@ def iplimage_to_string(im, lang=None, psm=None):
66
72
txt = image_to_string (TEMP_IMAGE , lang , psm )
67
73
os .remove (TEMP_IMAGE )
68
74
return txt
69
-
75
+
70
76
def image_to_string (file ,lang = None , psm = None ):
71
77
check_path () #Check if tesseract available in the path
72
78
process_request (file , TEMP_FILE , lang , psm ) #Process command
73
79
f = open (TEMP_FILE + ".txt" ,"r" ) #Open back the file
74
80
txt = f .read ()
75
81
os .remove (TEMP_FILE + ".txt" )
76
82
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
+
79
94
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