-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobjects.py
175 lines (148 loc) · 5.73 KB
/
objects.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
from collections import Counter
import numpy as np
class BoundingBox():
def __init__(self, points):
self.x0 = int(points[0])
self.y0 = int(points[1])
self.x1 = int(points[2])
self.y1 = int(points[3])
def __str__(self):
return 'x0:%d y0:%d, x1:%d y1:%d' %( self.x0, self.y0, self.x1, self.y1 )
@property
def width(self):
return self.x1 - self.x0
@property
def height(self):
return self.y1 - self.y0
def is_inside(self, other_box):
""" Determine if this object is within the provided bounding_box """
if self.x0 >= other_box.x0 and \
self.x1 <= other_box.x1 and \
self.y0 >= other_box.y0 and \
self.y1 <= other_box.y1:
return True
else:
return False
def vertical_overlap(self, other_box):
""" Return the vertical overlap of this object and the provided bounding_box """
return self._get_overlap((self.y0, self.y1),(other_box.y0, other_box.y1))
def horizontal_overlap(self, other_box):
""" Return the horizontal overlap of this object and the provided bounding_box """
return self._get_overlap((self.x0, self.x1),(other_box.x0, other_box.x1))
def _get_overlap(self, interval_one, interval_two):
""" Private function to determine overlap of two intervals """
return max(0, min(interval_one[1], interval_two[1]) - max(interval_one[0], interval_two[0]))
def derive_bounding_box(children):
temp_box = []
for child in children:
if len(temp_box) == 0 :
temp_box.append(child.box.x0)
temp_box.append(child.box.y0)
temp_box.append(child.box.x1)
temp_box.append(child.box.y1)
else:
temp_box[0] = min( child.box.x0, temp_box[0] )
temp_box[1] = min( child.box.y0, temp_box[1] )
temp_box[2] = max( child.box.x1, temp_box[2] )
temp_box[3] = max( child.box.y1, temp_box[3] )
return BoundingBox(temp_box)
class hOCRObject():
def __init__(self, element):
self.box = BoundingBox(element['title'].split()[1:])
self.text = element.string
@property
def width(self):
return self.box.width
@property
def height(self):
return self.box.height
def recalculate_bounding_box(self):
self.box = derive_bounding_box(self.children)
class Word(hOCRObject):
def __init__(self, element):
if element.has_attr('xml:lang') or element.has_attr('lang'):
self.content_type = 'GREEK'
else:
self.content_type = 'LATIN'
super().__init__(element)
# I don't strictly want to use the element attributes to set label
self.label = self.content_type
class Line(hOCRObject):
def __init__(self, element, children):
self.children = children
super().__init__(element)
# Some line dimensions don't seem to actually fit the contained words, so we'll just always do this.
self.box = derive_bounding_box(self.children)
@property
def avg_height(self):
""" Calculate the average height of the child elements """
heights = [c.height for c in self.children]
return sum(heights)/len(heights)
@property
def most_common_type(self):
""" """
return Counter([w.content_type for w in self.children]).most_common()[0][0]
class Area(hOCRObject):
def __init__(self, box, children):
self.box = box
self.children = children
self.label = ""
def is_single_line(self):
""" Returns true if area contains only one line. Test for titles et al. """
if len(self.children) == 1:
return True
else:
return False
@property
def avg_line_height(self):
heights = [s.height for s in self.children]
if heights:
return sum(heights)/len(heights)
@property
def line_std_dev(self):
""" Return the standard deviation of line heights in an area """
heights = [s.height for s in self.children]
if heights:
return np.std(heights)
class Page(hOCRObject):
def __init__(self, children):
self.children = children
if children:
self.box = derive_bounding_box(children)
self.line_spaces = self._generate_line_spaces()
else:
self.box = BoundingBox([0,0,0,0])
def _generate_line_spaces(self):
line_spaces = []
for i, l in enumerate(self.children):
next_line = i + 1
if not next_line >= len(self.children):
line_spaces.append(BoundingBox([self.box.x0, l.box.y1, self.box.x1, self.children[next_line].box.y0]))
return line_spaces
def avg_line_spacing(self):
heights = [s.height for s in self.line_spaces]
if heights:
return sum(heights)/len(heights)
def avg_line_x(self, extremity):
""" Get the median start or end point of a line
0 = start, 1 = end """
# I want to use this regardless of current page state
if isinstance(self.children[0], Area):
lines = []
for area in self.children:
lines.extend(area.children)
if isinstance(self.children[0], Line):
lines = self.children
if extremity == 'START':
x = [l.children[0].box.x0 for l in lines]
if extremity == 'END':
x = [l.children[-1].box.x1 for l in lines]
return np.median(x)
def recalculate_spaces(self):
self.line_spaces = self._generate_line_spaces()
class Text():
def __init__(self, pages):
self.pages = pages
@property
def length(self):
return len(self.pages)