-
Notifications
You must be signed in to change notification settings - Fork 0
/
literali_assistant.py
169 lines (144 loc) · 4.04 KB
/
literali_assistant.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
import copy
import cv2
import os
import urllib
import webcam_utils
import char_net
characters = char_net.characters
def read_dict():
dict_cache_path = './dict_cache.lst'
dict_url = 'https://github.com/brown-uk/dict_uk/blob/master/data/dict/base.lst?raw=true'
if not os.path.exists(dict_cache_path):
urllib.request.urlretrieve(dict_url, dict_cache_path)
file = open(dict_cache_path, encoding="utf8")
dict = []
for line in file.readlines():
try:
characters.index(line[0])
word = line.split(' ')[0]
for c in word:
characters.index(c)
if len(word) < 3:
continue
dict.append(word)
except:
pass
return dict
def gen_combo_words(words, intersection):
suffix_dict = {}
for w in words:
if len(w) <= intersection:
continue
suffix_dict[w[-intersection:]] = w
res = []
for w in words:
if len(w) <= intersection:
continue
if w[:intersection] in suffix_dict:
res.append(suffix_dict[w[:intersection]] + w[intersection:])
return res
dict = read_dict()
combo_words = []
for intersection in range(2, 5):
combo_words += gen_combo_words(dict, intersection)
dict += combo_words
dict = set(dict)
def with_mask(dict):
res = []
for word in dict:
mask = 0
for i, c in enumerate(characters):
try:
word.index(c)
mask |= 1<<i
except:
pass
res.append((word, mask))
return res
dict_with_mask = with_mask(dict)
def score_len_with_redness(word, char_to_redness):
max_redness = 0
for rs in char_to_redness.values():
for r in rs:
max_redness = max(max_redness, r)
if max_redness >= 3 and len(word) > 7:
return 0
if max_redness >= 2 and len(word) > 10:
return 0
res = 0
ctr = copy.deepcopy(char_to_redness)
for c in word:
redness = ctr[c].pop()
if redness < 1:
res += 1
elif redness < 1.4:
res += 2
elif redness < 2:
res += 8
elif redness < 3:
res += 32
else:
res += 128
return res
def score_len(word, char_to_redness):
return len(word)
def score_len8(word, char_to_redness):
if len(word) < 8:
return 0
return 100 - len(word)
def find_match(chars, char_to_redness, score = score_len_with_redness):
s = -1
r = ""
d = {}
mask = 0
for c in characters:
d[c] = 0
for c in chars:
if c == '?':
continue
d[c] += 1
mask |= 1 << characters.index(c)
for word, word_mask in dict_with_mask:
if (mask & word_mask) != word_mask:
continue
ok = True
for c in word:
d[c] -= 1
if d[c] < 0:
ok = False
for c in word:
d[c] += 1
if not ok:
continue
ns = score(word, char_to_redness)
if ns > s:
s = ns
r = word
return r
print('Dict size:', len(dict))
net = char_net.CharNet.create_from_file('./char_net0.pt')
def process(extracted_squares, extracted_squares_redness):
result = []
for square in extracted_squares:
character = net.guess_character(square)
if not character is None:
result.append(character)
else:
result.append('?')
char_to_redness = {}
for c in result:
char_to_redness[c] = []
for c, redness in zip(result, extracted_squares_redness):
char_to_redness[c].append(redness)
char_to_redness[c].sort()
match = find_match(result, char_to_redness)
highlight_contour_ids = []
if len(match) > 0:
print(match)
for c in match:
# TODO: find the one with highest redness
id = result.index(c)
highlight_contour_ids.append(id)
result[id] = '*'
return highlight_contour_ids
webcam_utils.main_loop(process)