forked from Kareem-Emad/arabic-ocr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
segment_words.py
278 lines (227 loc) · 9.27 KB
/
segment_words.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
import numpy as np
import argparse
import cv2
import os
import shutil
import json
import time
from utils import convert_to_binary, convert_to_binary_and_invert, display_image, get_distance_between_words
from preprocess import get_baseline_y_coord, get_horizontal_projection
from preprocess import get_vertical_projection, deskew, contour_seg
from train_recognition import batch_get_feat_vectors
from integrator import compare_and_assign, get_words_from_text, load_features_map, match_feat_to_char
def segment_lines(image, directory_name, write_to_file):
(h, w) = image.shape
image = convert_to_binary(image)
original_image = image.copy()
image = cv2.dilate(image, np.ones((3, 3), np.uint8), iterations=1)
horizontal_projection = get_horizontal_projection(image)
y, count = 0, 0
is_space = False
ycoords = []
for i in range(h):
if not is_space:
if horizontal_projection[i] == 0:
is_space = True
count = 1
y = i
else:
if horizontal_projection[i] > 0:
is_space = False
ycoords.append(y / count)
else:
y += i
count += 1
previous_height = 0
if os.path.exists(directory_name):
shutil.rmtree(directory_name)
os.makedirs(directory_name)
line_images = []
for i in range(len(ycoords)):
if i == 0:
continue
# cv2.line(image, (0, int(ycoords[i])), (w, int(ycoords[i])), (255, 255, 255), 2)
image_cropped = original_image[previous_height:int(ycoords[i]), :]
line_images.append(image_cropped)
# line = image_cropped.copy()
# baseline = get_baseline_y_coord(get_horizontal_projection(line))
# cv2.line(line, (0, baseline), (w, baseline), (255, 255, 255), 1)
# display_image("base",line)
previous_height = int(ycoords[i])
if write_to_file == 1:
cv2.imwrite(directory_name + "/" + "segment_" + str(i) + ".png", image_cropped)
# display_image("segmented lines", image_cropped)
image_cropped = original_image[previous_height:h, :]
line_images.append(image_cropped)
if write_to_file == 1:
cv2.imwrite(directory_name + "/" + "segment_" + str(i + 1) + ".png", image_cropped)
# cv2.imwrite("segmented_lines.png", image)
return line_images
def convert(o):
import numpy
if isinstance(o, numpy.int64):
return int(o)
raise TypeError
def segment_words(line_images, path, img_name, input_path, train, acc_char_map):
"""
this function keeps the list of word separatation points in word_separation list
but segments into sub words and saves the sub words segements in their designated directory
"""
# files = [f for f in os.listdir(path) if os.path.isfile(os.path.join(path, f))]
# image = cv2.imread(os.path.join(path, files[1]))
# print(os.path.join(path, files[1]))
gt_words = get_words_from_text(img_name, input_path)
if(train):
char_map = acc_char_map
else:
if(not acc_char_map or acc_char_map == {}):
char_map = load_features_map()
else:
char_map = acc_char_map
recognized_chars = ''
""""
directory_name = "./segmented_words"
if os.path.exists(directory_name):
shutil.rmtree(directory_name)
os.makedirs(directory_name)char_map
"""
curr_word_idx = 0
wrong_seg_words = 0
for image in line_images:
original_image = image.copy()
# image_with_line = image.copy()
(h, w) = image.shape
horizontal_projection = get_horizontal_projection(image)
baseline_y_coord = get_baseline_y_coord(horizontal_projection)
# cv2.line(image_with_line, (0, baseline_y_coord), (w, baseline_y_coord), (255, 255, 255), 1)
vertical_projection = get_vertical_projection(image)
# print("shape of vertical projections is: ", len(vertical_projection))
x, count = 0, 0
is_space = False
xcoords = []
distances = []
for i in range(w):
if not is_space:
if vertical_projection[i] == 0:
is_space = True
count = 1
x = i
else:
if vertical_projection[i] > 0:
is_space = False
xcoords.append(x / count)
distances.append(count)
else:
x += i
count += 1
previous_width = 0
word_separation = xcoords.copy()
# word_separation = list(filter(lambda a: a != -1, word_separation))
distance = get_distance_between_words(distances)
# print("distance is ",distance)
for i in range(len(word_separation)):
if distances[i] > distance:
pass
else:
word_separation[i] = -1
word_separation = list(filter(lambda a: a != -1, word_separation))
# print(word_separation)
previous_width = image.shape[1]
seg_points = []
for i in range(len(word_separation)):
i = len(word_separation) - i - 1
word = original_image[:, int(word_separation[i]):previous_width]
display_image("word", word)
# cv2.line(image, (int(word_separation[i]), 0), (int(word_separation[i]), image.shape[0]),(255, 255, 255), 1) # noqa
previous_width = int(word_separation[i])
seg_points = contour_seg(word, baseline_y_coord)
if (len(gt_words) > curr_word_idx and train):
feat_vectors = batch_get_feat_vectors(word, seg_points, gt_words[curr_word_idx])
else:
feat_vectors = batch_get_feat_vectors(word, seg_points, None)
if (train):
if (len(gt_words) > curr_word_idx):
aux_map = compare_and_assign(feat_vectors, gt_words[curr_word_idx], char_map)
if (aux_map != -1):
char_map = aux_map
else:
# print(f'Rejected Word #{curr_word_idx}')
wrong_seg_words += 1
else:
wrong_seg_words += 1
else:
recognized_chars += ' ' + match_feat_to_char(char_map, feat_vectors)
curr_word_idx += 1
display_image("word sep", image)
if (train):
try:
with open('./config_map.json', 'w') as f:
f.write(json.dumps(char_map, ensure_ascii=False, default=convert))
f.close()
# print(char_map)
return wrong_seg_words, curr_word_idx - 1, char_map, 0
except Exception:
# print(char_map)
return wrong_seg_words, curr_word_idx - 1, char_map, 0
else:
end_time = time.time()
try:
with open(f'./output/text/{img_name.replace("png", "txt")}', 'w') as f:
f.write(recognized_chars)
except Exception:
return 0, 0, {}, end_time
# print(f'recognized_text: {recognized_chars}')
return 0, 0, {}, end_time
def process_image(line_segmets_path, input_path, f, acc_char_map, train):
image = cv2.imread(os.path.join(input_path, f))
# display_image("source", image)
start_time = time.time()
processed_image = convert_to_binary_and_invert(image)
processed_image = deskew(processed_image)
# display_image("after deskew", processed_image)
# cv2.imwrite("binary.png", processed_image)
line_segmets_path = os.path.join(line_segmets_path, f[:-4])
lines = segment_lines(processed_image, line_segmets_path, 0)
curr_ww, curr_tw, acc_char_map, end_time = segment_words(lines, line_segmets_path, f, input_path, train, acc_char_map) # noqa
if(train):
print(f'we got {curr_ww} wrong out of {curr_tw}')
return curr_ww, curr_tw, acc_char_map, end_time - start_time
if __name__ == '__main__':
ap = argparse.ArgumentParser()
ap.add_argument("-o",
"--line-segments-path",
required=False,
help="path to line segments file",
default="./segmented_lines")
ap.add_argument("-i",
"--input-path",
required=False,
help="path to line segments file",
default="./inputs")
args = vars(ap.parse_args())
print(args)
input_path = args["input_path"]
line_segmets_path = args["line_segments_path"]
files = [f for f in os.listdir(input_path) if os.path.isfile(os.path.join(input_path, f))]
nthreads = 0
words_wrong = 0
total_words = 0
acc_char_map = load_features_map()
avg_acc = 0
train = False
durations = []
for f in files:
cww, ctw, acc_char_map, duration = process_image(line_segmets_path, input_path, f, acc_char_map, train) # noqa
durations.append(duration)
words_wrong += cww
total_words += ctw
if(total_words):
avg_acc += words_wrong / total_words
with open('output/running_time.txt', 'w') as f:
for dur in durations:
f.write(str(dur))
f.write('\n')
f.close()
avg_acc = avg_acc / len(files)
if(train):
print(f'in Total: Got {words_wrong} from {total_words} | accuracy: {1 - avg_acc}')