-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
303 lines (220 loc) · 6.48 KB
/
main.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# -*- coding: utf-8 -*-
"""
Created on Sat Dec 6 16:57:01 2014
@author: rafik
"""
import numpy as np
import cv2
import cv2.cv as cv
from PIL import Image
import sys
import pyocr
import pyocr.builders
#import pyperclip
cap = cv2.VideoCapture(1)
tools = pyocr.get_available_tools()
if len(tools) == 0:
print("No OCR tool found")
sys.exit(1)
tool = tools[0]
print("Will use tool '%s'" % (tool.get_name()))
# Ex: Will use tool 'tesseract'
langs = tool.get_available_languages()
print("Available languages: %s" % ", ".join(langs))
if 'eng' in langs:
lang = 'eng'
else:
lang = langs[0]
print("Will use lang '%s'" % (lang))
builder = pyocr.builders.TextBuilder()
builder.tesseract_configs.append('bank')
print "init"
print "test"
def getText(frame):
img = Image.fromarray(frame)
txt = tool.image_to_string(img,
lang=lang,
builder=builder)
# word_boxes = tool.image_to_string(img,
# lang=lang,
# builder=pyocr.builders.WordBoxBuilder())
#
# line_and_word_boxes = tool.image_to_string(
# img, lang=lang,
# builder=pyocr.builders.LineBoxBuilder())
#
# # Digits - Only Tesseract
# digits = tool.image_to_string(img,
# lang=lang,
# builder=pyocr.tesseract.DigitBuilder())
# return (txt, word_boxes, line_and_word_boxes, digits)
return txt
def cleanText(a):
texts = [_.replace(' ','') for _ in a.split('\n')]
if len(texts)==1:
return texts[0]
if len(texts)==0:
return ''
tl = [(len(_), _) for _ in texts]
if len(tl)==0:
return ''
l, t = max(tl)
#print 'tl:', tl
return str(t)
saved = []
for i in range(52):
saved.append({'?':3})
saved[13]['>'] = 99
saved[41]['+'] = 99
saved[51]['>'] = 99
def intelSave(a):
offs1 = -1
offs2 = -1
offs3 = -1
l = len(a)
ch = [' ']*52
p = a.find('>')
if p>=0:
if p < len(a)-1:
offs1 = 13-p
else:
offs1 = 51-p
p = a.find('>',p+1)
if p>=0:
if p < len(a)-1:
pass #some error, second < should be at end
#offs2 = 13-p
else:
offs2 = 51-p
p = a.find('+')
if p>=0:
offs3 = 41-p
offs = max(offs1, offs2, offs3)
if offs>=0:
for i in range(0,l):
try:
saved[i+offs][a[i]] += 1
ch[i+offs] = a[i]
except KeyError:
saved[i+offs][a[i]] = 1
ch[i+offs] = a[i]
res = []
rat = []
for i in range(52):
srt = sorted(saved[i].items(), key=lambda x: x[1], reverse=True)
# print 'srt:', srt
res.append(srt[0][0])
try:
r = 1.0 * srt[1][1] / srt[0][1] # ratio between counts of accepted to next possible solution
if srt[0][0]=='?':
r = 1.0
except IndexError:
r = 1.0
rat.append(r)
res = ''.join(res)
ch = ''.join(ch)
# print '-----\nres:'
# print a
# if p>=0:
# print ' '*p + '^'
# else:
# print 'XXX'
# print res
# print ch
# print 'offsets:', offs1, offs2, offs3
# print '-----'
return (res, rat)
def pruefziffer(zahl):
key = [0,9,4,6,8,2,7,1,3,5]
nd = len(str(zahl))
ueb = 0
for e in reversed(range(nd)):
z = zahl // 10**e
ueb = key[(z+ueb)%10]
return (10-ueb)%10
def main():
i=0
aa = 43
bb = 2
cc = 3
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
#print i
# Our operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
thresh = cv2.adaptiveThreshold(gray,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
cv2.THRESH_BINARY,aa,bb)
blur = cv2.GaussianBlur(thresh,(cc,cc),0)
img = blur
#blur = cv2.GaussianBlur(gray,(5,5),0)
#ret3,th3 = cv2.threshold(gray,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
#img = th3
# Display the resulting frame
cv2.imshow('frame',img)
if i%1==0:
a = getText(img)
# print 'ocr:', a[0]
# print 'ocr:', a[1]
# print 'ocr:', a[2]
a = cleanText(a)
res, ret = intelSave(a)
stat = ['_','_','_','-','_']
try:
p1 = pruefziffer(int(res[0:12]))
p2 = int(res[12])
pz = p1==p2
stat[2] = 'X'
except ValueError:
pz = False
pass
if max(ret)<0.50:
py = True
stat[1] = 'X'
else:
py = False
if res.find('?')>=0:
px = False
else:
px = True
stat[0] = 'X'
if res == '0100000100602>993899481455697700005092815+010275154>':
stat[4]='X'
print '(a:%02i b:%02i c:%02i)' % (aa,bb,cc),
# print 'stat:',''.join(stat),
# print a
# print ' >> ', ' '.join([' %s ' % _ for _ in res])
# print ' >> ', ' '.join(['%3.2f' % _ for _ in ret])
print res,
print ''.join(['^' if v>=0.5 else ' ' for v in ret]), ''.join(stat)
if px and py and pz:
break
key = cv2.waitKey(1) & 0xFF
if key == ord('q'):
break
elif key == ord('a'):
aa += 2
elif key == ord('z'):
aa -= 2
elif key == ord('s'):
bb += 1
elif key == ord('x'):
bb -= 1
elif key == ord('d'):
cc += 2
elif key == ord('c'):
cc -= 2
i +=1
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
for i in range(52):
print sorted(saved[i].items(), key=lambda x: x[1], reverse=True)
rres = res[0:42] + ' ' + res[42:]
print 'the result is (correct? %s):' % stat[4]
print rres
print 'copied tot clipboard'
import pyperclip
pyperclip.copy(rres)
if __name__ == '__main__':
main()