-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOneWordSolve.py
118 lines (94 loc) · 3.34 KB
/
OneWordSolve.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
# -*-coding:utf-8 -*-
# For Capturing warnings on terminal
import logging
logging.captureWarnings(True)
import cv2
import matplotlib.pyplot as plt
from skimage import measure
from math import floor, ceil
from OneCharacterRecognize import OneCharacterRecognize
from correctWord import CorrectWord
from handwriting_tools import read_image, to_the_same_size
wordSpace = 7 #originally wordSpace = 7
def get_image_words(image):
# Delete the included area,Return to the correct area
def remove_range(cells):
# b in a
def range_include(a, b):
return b.up >= a.up and b.down <= a.down and b.left >= a.left and b.right <= a.right
def range_cmp(range_data_a, range_data_b):
return -1 if range_data_a.down - range_data_a.up < range_data_b.down - range_data_b.up else 1
cells.sort(range_cmp)
n = len(cells)
ok = [True] * n
for i in xrange(1, n):
for j in xrange(i):
if ok[j] and range_include(cells[i], cells[j]):
ok[j] = False
new_cells = [cells[i] for i in xrange(n) if ok[i]]
return new_cells
# Word sorting
def mycmp(range_data_a, range_data_b):
return -1 if range_data_a.left < range_data_b.left else 1
contours = measure.find_contours(image, 0.8)
cells = []
for contour in contours:
up, down, left, right = min(contour[:, 0]), max(contour[:, 0]), min(contour[:, 1]), max(contour[:, 1])
if down - up >= wordSpace or right - left >= wordSpace:
cells.append(RangeData(up, down, left, right))
cells = remove_range(cells)
cells.sort(mycmp)
return cells
class RangeData(object):
def __init__(self, up, down, left, right):
self.up = floor(up)
self.down = ceil(down)
self.left = floor(left)
self.right = ceil(right)
def __str__(self):
return ' '.join(str(i) for i in [self.left])
__repr__ = __str__
def get_info(self):
return self.up, self.down, self.left, self.right
# if __name__ == '__main__':
# def main():
# plt.gray()
# clf = OneCharacterRecognize()
# print 'read test file'
# image = read_image('./data/crop14.jpg')
# word = get_image_words(image)
# print len(word)
# to_predict = []
# for i, c in enumerate(word):
# nw, sw, ne, se = c.get_info()
# cur = to_the_same_size(image[nw:sw, ne:se])
# ax = plt.subplot(len(word), 1, i + 1)
# ax.imshow(cur)
# ax.set_axis_off()
# to_predict.append(cur.ravel())
# ans = clf.predict(to_predict)
# ans = ''.join(ans)
# print ans
# cw = CorrectWord()
# print cw.correct(ans.lower())
# plt.show()
# main()
def my_recognizer(image_name):
clf = OneCharacterRecognize()
image = read_image('./' + image_name + '.jpg')
word = get_image_words(image)
print len(word)
to_predict = []
for i, c in enumerate(word):
nw, sw, ne, se = c.get_info()
cur = to_the_same_size(image[nw:sw, ne:se])
to_predict.append(cur.ravel())
ans = clf.predict(to_predict)
if(ans == 0):
return 0
ans = ''.join(ans)
print ans
return ans
# cw = CorrectWord()
# print cw.correct(ans.lower())
# return cw.correct(ans.lower())