forked from Kareem-Emad/arabic-ocr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
contour.py
284 lines (225 loc) · 10.5 KB
/
contour.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
import numpy as np
import argparse # noqa
import cv2
from utils import convert_to_binary_and_invert, display_image, most_frequent # noqa
from preprocess import get_baseline_y_coord, get_horizontal_projection # noqa
from preprocess import get_vertical_projection # noqa
def template_match(image, path):
template = cv2.imread(path, cv2.COLOR_BGR2GRAY)
template = convert_to_binary_and_invert(template)
if (image.shape[0] < template.shape[0] or image.shape[1] < template.shape[1]):
return [], 0
img = image.copy()
w, h = template.shape[::-1]
recv2.lines = cv2.matchTemplate(img, template, cv2.TM_CCOEFF_NORMED)
threshold = 0.65
loc = np.where(res >= threshold)
# print("template width", template.shape[1])
points = []
for pt in zip(*loc[::-1]):
if (len(points) > 0):
if (pt[0] - points[-1] < template.shape[1]):
print("here")
continue
# cv2.line(img, (pt[0], 0), (pt[0], img.shape[0]), (255, 255, 255), 1)
# cv2.line(img, (pt[0] + template.shape[1], 0), (pt[0] + template.shape[1], img.shape[0]), (255, 255, 255), 1) # noqa
# cv2.rectangle(img, pt, (pt[0] + w, pt[1] + h), (255, 255, 255), 2)
points.append(pt[0])
# display_image('res.png', img)
return points, template.shape[1]
"""
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"]
image = cv2.imread("./contour/segment_5.png", cv2.COLOR_BGR2GRAY)
display_image("source", image)
processed_image = image
edged = processed_image
vertical_projection = get_vertical_projection(image)
x, count = 0, 0
is_space = False
xcoords = []
distances = []
for i in range(edged.shape[1]):
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
contours, hierarchy = cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
contours = sorted(contours, key=lambda x: cv2.contourArea(x), reverse=True)
for cnt in contours:
if (cv2.contourArea(cnt) < 1):
# print("too small", cv2.contourArea(cnt))
break
image_blank = np.zeros(edged.shape, np.uint8)
img = cv2.drawContours(image_blank, [cnt], 0, (255, 255, 255), 1)
leftmost = tuple(cnt[cnt[:, :, 0].argmin()][0])
# print("left most", leftmost)
rightmost = tuple(cnt[cnt[:, :, 0].argmax()][0])
topmost = tuple(cnt[cnt[:, :, 1].argmin()][0])
bottommost = tuple(cnt[cnt[:, :, 1].argmax()][0])
cv2.circle(img, leftmost, 1, (255, 0, 0), -1)
cv2.circle(img, rightmost, 1, (0, 255, 0), -1)
cv2.circle(img, topmost, 1, (0, 0, 255), -1)
cv2.circle(img, bottommost, 1, (255, 255, 0), -1)
cv2.imwrite("cnt.png", img)
display_image("contour", img)
index_left = np.where((cnt == leftmost).all(axis=2))
index_right = np.where((cnt == rightmost).all(axis=2))
index_top = np.where((cnt == topmost).all(axis=2))
index_bottom = np.where((cnt == bottommost).all(axis=2))
# print("cnt shape", cnt.shape)
# right il 2a5r
# top lil left
img_cnt = np.zeros(edged.shape, np.uint8)
y_points = []
x_points = []
for i in range(0, cnt.shape[0]):
point = (cnt[i][0][0], cnt[i][0][1])
y_points.append(point[1])
x_points.append(point[0])
cv2.circle(img, point, 1, (255, 0, 0), -1)
img_cnt[point[1], point[0]] = image[point[1], point[0]]
seen_points, template_width_seen = template_match(img_cnt, "seen_start.png")
# print("seen points", seen_points)
seen_mid_points, template_width_seen = template_match(img_cnt, "seen_mid.png")
# print("seen mid points", seen_mid_points)
kaf_points, template_width_kaf = template_match(img_cnt, "kaf.png")
# print("kaf points", kaf_points)
fa2_points, template_width_fa2 = template_match(img_cnt, "fa2.png")
# print("fa2 points", fa2_points)
sad_points, template_width_sad = template_match(img_cnt, "sad.png")
# print("sad points", sad_points)
for point in seen_points:
img_cnt[:, point:point + template_width_seen] = 255
for point in kaf_points:
img_cnt[:, point:point + template_width_kaf] = 255
for point in fa2_points:
img_cnt[:, point:point + template_width_fa2] = 255
for point in sad_points:
img_cnt[:, point:point + template_width_fa2] = 255
cv2.imwrite("img_cnt.png", img_cnt)
hp = get_horizontal_projection(img_cnt)
baseline_org = get_baseline_y_coord(get_horizontal_projection(processed_image))
img_line = img_cnt.copy()
baseline = most_frequent(np.asarray(y_points))
# print("now baseline is: ", baseline)
# print("now baseline_org is:", baseline_org)
h, w = processed_image.shape
cv2.line(img_line, (0, baseline), (w, baseline), (255, 255, 255), 1)
cv2.line(img_line, (0, baseline_org), (w, baseline_org), (255, 255, 255), 1)
cv2.imwrite("baseline.png", img_line)
display_image("baseline_org", img_line)
count = 0
flag = False
length_consective = []
point_positions = []
for i in range(len(y_points)):
if not flag:
if y_points[i] - 2 == baseline or y_points[
i] == baseline or y_points[i] + 1 == baseline or y_points[i] - 1 == baseline:
count = 1
flag = True
else:
if not (y_points[i] - 2 == baseline or y_points[i] == baseline or y_points[i] + 1 == baseline
or y_points[i] - 1 == baseline):
flag = False
if count > 2:
length_consective.append(count)
point_positions.append(i)
else:
count += 1
# print("length_consective: ", length_consective)
# print("point_positions: ", point_positions)
# print(list(y_points[x] for x in point_positions))
sub_x = []
j = 0
final = img_cnt.copy()
segment_points = []
for i in point_positions:
sub_x = x_points[i - length_consective[j]:i]
print("sub_x", sub_x)
j += 1
# for k in range(len(sub_x)-1 , -1, -1):
canidatate_points = []
print(type(img_cnt[0, 0]))
for k in range(len(sub_x)):
# sub_above = img_cnt[:baseline, sub_x[k]]
sub_above = img_cnt[int(baseline / 2):baseline, sub_x[k]]
sub_below = img_cnt[baseline + 2:, sub_x[k]]
# print("sub_above: ", sub_above)
# print("sub_below:, ", sub_below)
# need to add some threshold to eliminate too close seg points
if 255 not in sub_above and 255 not in sub_below:
# cv2.line(final, (sub_x[k], 0), (sub_x[k], image.shape[0]), (255, 255, 255), 1)
# segment_points.append(sub_x[k])
# print("there is a point")
# print("sub below: ", sub_below)
canidatate_points.append(sub_x[k])
if len(canidatate_points) > 0:
# print("can", canidatate_points)
segment_points.append(canidatate_points[len(canidatate_points) // 2])
# print("seg @@@@@: ", segment_points)
if len(segment_points) < 1:
print("&&&&&&&&& no seg points")
continue
delete_point = False
segment_points.sort()
print("segment points: ", segment_points)
for i in range(1, len(segment_points)):
# cv2.imwrite("sa_" + str(i) + ".png", img_cnt[:baseline, segment_points[i-1]: segment_points[i]])
if (img_cnt[:baseline, segment_points[i - 1]:segment_points[i]] == 0).all():
delete_point = True
# print(img_cnt[:baseline, segment_points[i-1]: segment_points[i]])
# print("####seg :", i - 1, i)
# print("and ", segment_points[i - 1], segment_points[i])
segment_points[i - 1] = -1
if delete_point:
segment_points.remove(-1)
if len(segment_points) > 1:
next_last_seg_point = segment_points[1]
else:
next_last_seg_point = img_cnt.shape[1]
last_seg_point = segment_points[0]
last_seg_hp = get_horizontal_projection(img_cnt[:baseline, last_seg_point:next_last_seg_point])
# print(last_seg_hp.shape)
first_non_zero_index = (last_seg_hp != 0).argmax(axis=0)[0]
# print(first_non_zero_index)
# print(get_horizontal_projection(img_cnt[baseline - 1:baseline +2, 0:last_seg_point]))
# print(img[baseline + 3:, 0:last_seg_point])
# this is for the dall and zal at the end of a sentence
if (first_non_zero_index / last_seg_hp.shape[0]) < 0.85 and (last_seg_hp[first_non_zero_index:] != 0).all() and (img[baseline - 1:baseline + 2, 0:last_seg_point] != 0).any() and (img[0:baseline - 2, 0:last_seg_point] == 0).all() and (img[baseline + 3:, 0:last_seg_point] == 0).all():
# print("here", last_seg_hp[first_non_zero_index:])
segment_points = segment_points[1:]
# print("this is a dal at the end")
for seg_point in segment_points:
if seg_point != -1:
# print("******************")
cv2.line(image, (seg_point, 0), (seg_point, image.shape[0]), (255, 255, 255), 1)
print("segment_points", segment_points)
display_image("final", image)
cv2.imwrite("final.png", image)
"""