-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_bounding_boxes.py
185 lines (151 loc) · 6.13 KB
/
get_bounding_boxes.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
from pdfminer.layout import LAParams, LTChar, LTTextBox, LTTextLine
from pdfminer.pdfpage import PDFPage
from pdfminer.pdfinterp import PDFResourceManager
from pdfminer.pdfinterp import PDFPageInterpreter
from pdfminer.converter import PDFPageAggregator
import re
def get_bounding_boxes(path, print_output = False ):
fp = open(path, 'rb')
rsrcmgr = PDFResourceManager()
laparams = LAParams()
device = PDFPageAggregator(rsrcmgr, laparams=laparams)
interpreter = PDFPageInterpreter(rsrcmgr, device)
pages = PDFPage.get_pages(fp)
page_words = []
page_positions = []
for page in pages:
if (print_output):
print('x1\ty1\tx2\ty2\tword\n---\t---\t---\t---\t---')
interpreter.process_page(page)
layout = device.get_result()
cropbox = page.cropbox
words = []
positions = []
word = ""
height_low = 0
height_high = 10000
for lobj in layout:
if isinstance(lobj, LTTextBox):
for liobj in lobj:
if isinstance(liobj, LTTextLine):
for chobj in liobj:
if isinstance(chobj, LTChar):
if (word == "" and chobj.get_text() == ""):
continue
words.append(chobj.get_text())
positions.append(chobj.bbox)
positions, words = sortBBP(positions, words, 1, 0)
if (print_output):
for index, w in enumerate(words):
print('%d\t%d\t%d\t%d\t%s\n' % (positions[index][0], positions[index][1], positions[index][2], positions[index][3], w))
page_words.append(words)
page_positions.append(positions)
return page_words, page_positions, cropbox
def sortBBP(positions, words, i, j):
temp_positions = []
temp_words = []
positions, words = sortBB(positions, words, i)
temp_index_position = positions[0][i]
temp_part_positions = []
temp_part_words = []
for index, position in enumerate(positions):
if (abs(position[i] - temp_index_position) < 0.3 * abs(position[3] - position[1])):
temp_part_positions.append(position)
temp_part_words.append(words[index])
temp_index_position = position[i]
elif (is_list_words(temp_part_words)):
temp_part_positions, temp_part_words = sortBB(temp_part_positions, temp_part_words, j)
temp_part_positions, temp_part_words = pop_multiple_spaces(temp_part_positions, temp_part_words)
temp_part_positions, temp_part_words = concatWords(temp_part_positions, temp_part_words)
for iindex, temp_part_position in enumerate(temp_part_positions):
temp_positions.append(temp_part_position)
temp_words.append(temp_part_words[iindex])
temp_part_positions = []
temp_part_words = []
temp_part_positions.append(position)
temp_part_words.append(words[index])
temp_index_position = position[i]
elif (len(positions) > index + 1):
temp_part_positions = []
temp_part_words = []
temp_part_positions.append(position)
temp_part_words.append(words[index])
temp_index_position = position[i]
if (is_list_words(temp_part_words)):
temp_part_positions, temp_part_words = sortBB(temp_part_positions, temp_part_words, j)
temp_part_positions, temp_part_words = pop_multiple_spaces(temp_part_positions, temp_part_words)
temp_part_positions, temp_part_words = concatWords(temp_part_positions, temp_part_words)
for iindex, temp_part_position in enumerate(temp_part_positions):
temp_positions.append(temp_part_position)
temp_words.append(temp_part_words[iindex])
return temp_positions, temp_words
def sortBB(positions, words, i):
temp_positions = []
temp_words = []
indices = []
for position in positions:
indices.append(position[i])
sorted_index = sorted(range(len(indices)),key=indices.__getitem__)
for i in sorted_index:
temp_positions.append(positions[i])
temp_words.append(words[i])
return temp_positions, temp_words
def concatWords(positions, words):
temp_positions = []
temp_words = []
temp_word = ""
if (words[0] == " "):
words.pop(0)
positions.pop(0)
start_position = positions[0]
for index, word in enumerate(words):
if (word == " "):
end_position = positions[index - 1]
temp_word = correctPersian(temp_word)
temp_words.append(temp_word)
temp_positions.append([start_position[0], start_position[1], end_position[2], end_position[3]])
if (len(positions) > index + 1):
start_position = positions[index + 1]
temp_word = ""
else:
temp_word += word
if (words[-1] != " "):
end_position = positions[-1]
temp_word = correctPersian(temp_word)
temp_words.append(temp_word)
temp_positions.append([start_position[0], start_position[1], end_position[2], end_position[3]])
return temp_positions, temp_words
def pop_multiple_spaces(positions, words):
count = 0
temp_positions = []
temp_words = []
for i, word in enumerate(words):
if (word == " "):
count += 1
else:
count = 0
if (count <= 1):
temp_positions.append(positions[i])
temp_words.append(word)
return temp_positions, temp_words
def is_list_words(words):
result = False
for word in words:
if (word != " "):
result = True
return result
def correctPersian(s):
n_s = s;
for t in re.findall(r'[\u0600-\u06FF\s]+$', s):
n_s = re.sub(t, t[::-1], n_s)
return n_s
# depricated functions.
def isEnglish(s):
reg = re.compile(r'[a-zA-Z]')
return bool(reg.match(s))
def isPersian(s):
reg = re.compile(r'/^[\u0600-\u06FF\s]+$/')
return bool(reg.match(s))
def isPunctuation(s):
reg = re.compile(r'[.!?\\-]')
return bool(reg.match(s))