-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.py
53 lines (44 loc) · 1.73 KB
/
process.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
from paddleocr import PaddleOCR
from utils import is_english_compound_word, is_english_word, is_alpha_string
def process_image(img_path, printable=False):
ocr = PaddleOCR(lang="en", show_log=False, use_angle_cls=False)
result = ocr.ocr(img_path)
items = []
for idx in range(len(result)):
res = result[idx]
for line in res:
location, result = line
word,confidence = result
word = word.strip()
left_top,right_top,right_bottom,left_bottom = location
x,y = left_top
# print(word, is_alpha_string(word))
# if is_alpha_string(word) and len(word) in [3,4]:
# print(word, is_alpha_string(word), img_path)
# if is_english_compound_word(word) or is_english_word(word):
if is_alpha_string(word):
items.append((x, y, word))
# Sort items by x-coordinate then y-coordinate
items.sort(key=lambda item: (item[1], item[0]))
# Create list of lines
lines = []
current_line = []
# Threshold for y-coordinate difference
threshold = 10
for item in items:
if not current_line or abs(item[1] - current_line[-1][1]) <= threshold:
current_line.append(item)
else:
lines.append(current_line)
current_line = [item]
if current_line:
lines.append(current_line)
page_words = []
for line in lines:
line.sort(key=lambda item: item[0])
page_words.append([item[2] for item in line])
if printable:
print(', '.join([item[2] for item in line]))
return page_words
# process_image('test_images/29.png',True)
# process_image('/Users/merdan.aziz/IELTS/IELTS/images/chapter5/test1/90.png', True)