-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutput_text_detection.py
105 lines (93 loc) · 4.34 KB
/
output_text_detection.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
import cv2
import numpy as np
from pathlib import Path
from PIL import Image
from input_text_detection import read_dataset
import time
start_time = time.time()
def overlapping_image(size1, size2):
(x1, y1, w1, h1), image1 = size1
(x2, y2, w2, h2), image2 = size2
if x1 + w1 > x2 and x2 + w2 > x1 and y1 + h1 > y2 and y2 + h2 > y1:
minx = min(x1, x2)
miny = min(y1, y2)
mask = np.zeros((max(y1 + h1, y2 + h2) - miny, max(x1 + w1, x2 + w2) - minx), dtype=np.uint8)
mask[y1 - miny:y1 - miny + h1, x1 - minx:x1 - minx + w1] = image1
mask[y2 - miny:y2 - miny + h2, x2 - minx:x2 - minx + w2] = image2
return True, mask
return False, image1
def split(word):
return list(word)
imgDict = {}
dataset = read_dataset("images/data")
def image_to_text():
# Read image form file and convert to gray image
# And using Path because read file with Vietnamese name
for f in Path('images/imageInput').rglob('*.[jp][pn]*'):
im = Image.open(f.as_posix()).convert('L')
# Convert image to numpy arr
im = np.array(im)
# im = cv2.GaussianBlur(im, (3,3), cv2.BORDER_DEFAULT)
# im = cv2.blur(im, (3,3))
thresh = cv2.threshold(im, 127, 255, cv2.THRESH_BINARY_INV)[1]
dilated = cv2.dilate(thresh.copy(), np.ones((5, 1), np.uint8), iterations=1)
contours, _ = cv2.findContours(dilated.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
dir_images = {}
list_images = []
for c in contours:
x, y, w, h = cv2.boundingRect(c)
if (h, w) != dilated.shape[:2] and h >= 2:
dir_images.setdefault(x, ((x, y, w, h), thresh[y:y + h, x:x + w]))
list_img = sorted(dir_images.items(), reverse=False)[:-1]
i = 0
while i <= len(list_img) - 2:
check, img = overlapping_image(list_img[i][1], list_img[i + 1][1])
# Compare the end point of the previous word with the first point of the following word
nx, ny, nw, nh = list_img[i + 1][1][0]
# Add image to list with image fixed and check 0 - no_space and 1 - is_space
list_images.append(
[cv2.copyMakeBorder(cv2.resize(img, (24, 43)), 30, 30, 30, 30, cv2.BORDER_CONSTANT, value=(0, 0, 0)),
1 if (nx - (list_img[i][1][0][0] + list_img[i][1][0][2])) > 7 else 0])
if check:
i += 1
if i == len(list_img) - 2:
list_images.append(
[cv2.copyMakeBorder(cv2.resize(list_img[len(list_img) - 1][1][1], (24, 43)), 30, 30, 30, 30,
cv2.BORDER_CONSTANT, value=(0, 0, 0)),
1 if (nx - (list_img[i][1][0][0] + list_img[i][1][0][2])) > 7 else 0])
i += 1
# Compare input and output image and print it to screen
result = ''
for i, (image, isSpace) in enumerate(list_images):
min_diff = []
for image_data in dataset:
""" Compare two images with number pixel different
Stack two image and count white point -> different point
The more similar two images are, the less white points they have """
res = cv2.absdiff(image, image_data[1]) # absdiff() func to stack two images
diff_count = cv2.countNonZero(res)
if len(min_diff) == 0:
min_diff = [image_data[0], diff_count]
else:
if min_diff[1] > diff_count:
min_diff = [image_data[0], diff_count]
# Check space in sentence and print sentences from image to screen
if isSpace == 1:
result += min_diff[0] + ' '
else:
result += min_diff[0]
word_list = result.split(' ')
# Find word not begin with 'l' and replace 'i' to 'l' for them
for i, word in enumerate(word_list):
if 'l' in word:
if not word.startswith('l'):
word_list[i] = word.replace('l', 'i')
if 'U' in word:
if not word.startswith('U'):
word_list[i] = word.replace('U', 'u')
# Print the result to screen
print(" ".join(word_list))
image_to_text()
#Count time run python execute
print("--- %s seconds ---" % (time.time() - start_time))
cv2.waitKey()