-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtesseract.py
168 lines (145 loc) · 5.28 KB
/
tesseract.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/python
import cv2.cv as cv
import cv2
import numpy as np
import re
import subprocess as sp
import glob
import re
import os
import sys
import time
import video
import json
import urllib
debug = [True, False] #debug, verbose
#validChars = re.compile("[\w.:?- ]")
validChars = re.compile("[\w.? ]")
alphaNum = re.compile("[0-9A-Za-z ]")
"""
@params
s-input string
"""
def removeNonRenderable(s):
return "".join(i for i in s if ord(i)<128 and ord(i)>10)
def speak(arg, pr=True):
if isinstance(arg, str):
cmd = ["say", arg ]
elif isinstance(arg, list):
cmd=["say"] + arg
else:
cmd = None
print "Error: Unknown arg type"
if cmd:
removeNoneAlphaNum = lambda word: "".join(i for i in word if re.search(alphaNum, i))
cmd = map(removeNoneAlphaNum, cmd)
if pr: print "Speaking {}".format(" ".join(cmd))
sp.Popen(cmd, stdout=sp.PIPE, stderr=sp.PIPE)
"""
pass dir name
"""
def getImgName(dir):
if dir[-1] != "/":
dir += "/"
files = glob.glob(dir+"out[0-9]*.jpg")
maxnum = -1
for f in files:
num = int(re.search("[0-9]+", f[len(dir):]).group(0))
if num>maxnum : maxnum = num
return dir + "out{}.jpg".format(maxnum+1)
def getGoogleHits(query):
query = "".join(i for i in query if re.search(validChars, i) )
if len(query) < 3: return 0 #Do not process garabage queries
query = urllib.urlencode({'q': query})
url = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&%s' % query
search_response = urllib.urlopen(url)
search_results = search_response.read()
results = json.loads(search_results)
data = results['responseData']
if debug[1]: print "data is {}".format(data)
try:
hits = int(data['cursor']['estimatedResultCount'])
except (KeyError, TypeError) as e:
hits = sum(map(len, query))
if debug[1]: print "Query is {}, hits are {}".format(query, hits)
return hits
#TODO: For performance, make multithreaded
if __name__ == '__main__':
cam = video.create_capture(0) #cv2.VideoCapture(0)
ret, img = cam.read()
takephoto = False
imgdir = "./img/" #out.jpg" #path of outputfile
textFile = "outtext/out"
textFileExt = "outtext/out.txt"
imgfile = imgdir + "out.jpg"
start = False #Used to denote start taking pictures
startBuffer = 0
startBufferMax = 10 #step size
cand = [] #candidate texts
maxHits = 0
startTime = None
maxTime = 5 #seconds
while True:
ret, img = cam.read() #img is numpy 3-array
invimg = np.fliplr(img) #inverted
#Takes one photo
if takephoto:
imgfile = getImgName(imgdir)
cv2.imwrite(imgfile, img) #Write image file
#tesseract usage: tesseract <inputfile> <outputfile>
exitcode = sp.call(["tesseract", imgfile, textFile]) #Recognize
if exitcode:
print "Error Occured"
else:
text = open(textFileExt).readlines()
print filter(lambda s: s, map(removeNonRenderable, text) )
takephoto = False
if start:
if startBuffer == startBufferMax:
#imgfile = imgdir + "out.jpg"
cv2.imwrite(imgfile, img) #Write image file
#tesseract usage: tesseract <inputfile> <outputfile>
p = sp.Popen(["tesseract", imgfile, textFile], stdout=sp.PIPE, stderr=sp.PIPE)
if p.returncode:
print "Error Occured"
else:
text = open(textFileExt).readlines() #list of strings
renderable = map(removeNonRenderable, text) #Renderable characters
clean = filter(lambda s: s.strip() , renderable ) #non empty and non space characters
if debug[0]: print "clean is {}, cand is {}".format(clean, cand)
if clean:
hits = map(getGoogleHits, clean)
if debug[0]: print "hits is {}".format(hits)
val = sum(hits)#np.mean(hits)
print val
if val > maxHits:
maxHits = val
cand = clean
startBuffer -= 1
elif startBuffer == 0:
startBuffer = startBufferMax
else:
startBuffer -=1
if time.time() - startTime > maxTime and maxHits > 0: #End loop
start = False
speak("Ending recognition.")
if cand:
speak(cand)
cv2.imshow('img', img )
key = cv.WaitKey(10)
if key == 27:
break
elif key == ord('x'): #Single photo
takephoto = True
elif key == ord('z'): #Best guess
speak("Starting recognition")
start = not start
startBuffer = startBufferMax
cand = []
startTime = time.time()
maxHits = 0
elif key == ord('r'):
if cand: speak(cand)
cv.DestroyAllWindows()
#os.remove(imgfile)
#os.remove(textFileExt)