-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexplorers.py
More file actions
255 lines (237 loc) · 8.49 KB
/
explorers.py
File metadata and controls
255 lines (237 loc) · 8.49 KB
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
# -*- coding: UTF-8 -*-
# LabelAutofinder module
# Copyright (C) 2025 Alberto Buffolino
# Released under GPL 2
from .search import SearchDirections
from .utils import debugLog
# explorer for obj and web strategy
class ObjExplorer:
def __init__(self, objRect, config):
# obj rectangle which you look around to
self.objRect = objRect
# direction(s) to consider for label search
searchDirections = config.directions
# max external distance from an obj point (left or right)
self.maxHorizontalDistance = config.maxHorizontalDistance
# max external distance from an obj point (top or bottom)
self.maxVerticalDistance = config.maxVerticalDistance
# methods for checking on each direction
checkerMethods = (self.leftCheck, self.topCheck, self.rightCheck, self.bottomCheck)
# point distances and labels collected for each direction
self.distancesAndLabels = {}
# checker methods to be invoked (according to passed directions)
self.checkers = {}
# initialize for each passed direction
for direction, checker in zip(SearchDirections.ALL, checkerMethods):
if direction in searchDirections:
self.distancesAndLabels[direction] = []
self.checkers[direction] = checker
def getDistanceAndLabelText(self, labelObjs):
debugLog("Labels to analyze: %d"%len(labelObjs))
# check proximity for each label obj
# around obj in specified directions,
# saving distance if in the neighborhood
for labelObj in labelObjs:
labelText = labelObj.name
labelObjRect = labelObj.location.toLTRB()
for direction, checker in self.checkers.items():
distance = checker(labelObjRect)
if distance:
self.distancesAndLabels[direction].append((distance, labelText,))
if not any(self.distancesAndLabels.values()):
debugLog("No label obj found!")
return
# establish nearest label for each direction
minDistancesAndLabels = {}
for direction in self.checkers.keys():
distancesAndLabels = self.distancesAndLabels[direction]
debugLog("Distances and labels for direction %d: %s"%(direction, distancesAndLabels))
minDistanceAndLabel = min(distancesAndLabels, key=lambda i: i[0]) if distancesAndLabels else (10000, None)
minDistancesAndLabels[direction] = minDistanceAndLabel
# establish the direction with nearest label
chosenDirection = min(minDistancesAndLabels, key=minDistancesAndLabels.get)
minDistance, labelText = minDistancesAndLabels[chosenDirection]
if minDistance == 10000:
debugLog("Unable to establish label position")
return
debugLog("Min distance: %d"%minDistance)
if not labelText or labelText.isspace():
# TODO: manage image with OCR and AI
debugLog("Found label obj, but no text (maybe it's an image?)")
return
return (minDistance, labelText)
def leftCheck(self, labelObjRect):
objLeft, objTop, objRight, objBottom = self.objRect
labelLeft, labelTop, labelRight, labelBottom = labelObjRect
if (
(objTop <= labelBottom)
and
(objBottom > labelTop)
and
(
(objLeft > labelRight and objLeft-labelRight <= self.maxHorizontalDistance)
or
# overlapping edges
(objLeft <= labelRight < objRight)
)
):
distance = abs(objLeft-labelRight)
return distance
def topCheck(self, labelObjRect):
objLeft, objTop, objRight, objBottom = self.objRect
labelLeft, labelTop, labelRight, labelBottom = labelObjRect
if (
(objLeft < labelRight)
and
(objRight > labelLeft)
and
(objTop > labelBottom)
and
(objTop-labelBottom <= self.maxVerticalDistance)
):
distance = objTop-labelBottom
return distance
def rightCheck(self, labelObjRect):
objLeft, objTop, objRight, objBottom = self.objRect
labelLeft, labelTop, labelRight, labelBottom = labelObjRect
if (
(objTop <= labelBottom)
and
(objBottom > labelTop)
and
(
(objRight < labelLeft and labelLeft-objRight <= self.maxHorizontalDistance)
or
# overlapping edges
(objLeft < labelLeft <= objRight)
)
):
distance = abs(labelLeft-objRight)
return distance
def bottomCheck(self, labelObjRect):
objLeft, objTop, objRight, objBottom = self.objRect
labelLeft, labelTop, labelRight, labelBottom = labelObjRect
if (
(objLeft < labelRight)
and
(objRight > labelLeft)
and
(objBottom < labelTop)
and
(labelTop-objBottom <= self.maxVerticalDistance)
):
distance = labelTop-objBottom
return distance
# explorer for text strategy
class CharExplorer:
def __init__(self, objRect, config):
# obj rectangle which you look around
self.objRect = objRect
# direction(s) to consider for char search
searchDirections = config.directions
# max external distance from an obj point (left or right)
self.maxHorizontalDistance = config.maxHorizontalDistance
# max external distance from an obj point (top or bottom)
self.maxVerticalDistance = config.maxVerticalDistance
# methods for checking on each direction
checkerMethods = (self.leftCheck, self.topCheck, self.rightCheck, self.bottomCheck)
# char distances and offsets collected for each direction
self.distancesAndOffsets = {}
# checker methods to be invoked (according to passed directions)
self.checkers = {}
# initialize for each passed direction
for direction, checker in zip(SearchDirections.ALL, checkerMethods):
if direction in searchDirections:
self.distancesAndOffsets[direction] = {}
self.checkers[direction] = checker
def getDistanceAndCharOffsets(self, charRects):
debugLog("Rects to analyze: %d"%len(charRects))
# check proximity for each char rect
# around obj in specified directions,
# saving offset and distance if in the neighborhood
for offset, charRect in enumerate(charRects):
for direction, checker in self.checkers.items():
distance = checker(charRect)
if distance:
self.distancesAndOffsets[direction].setdefault(distance, []).append(offset)
if not any(self.distancesAndOffsets.values()):
debugLog("No chunk offset found!")
return
# establish best direction to consider
distanceAndDirection = self.getDistanceAndDirection()
if not distanceAndDirection:
debugLog("No direction found!")
return
distance, chosenDirection = distanceAndDirection
debugLog("Chosen direction: %s"%chosenDirection)
# and return min distance and the collected offsets in that direction
offsets = self.distancesAndOffsets[chosenDirection][distance]
return (distance, offsets)
def getDistanceAndDirection(self):
# find minimum distance in specified directions
# or set a no-sense, giant one instead (when no offsets)
minDistanceInDirection = {}
for direction in self.checkers.keys():
distances = self.distancesAndOffsets[direction].keys()
minDistanceInDirection[direction] = min(distances) if distances else 10000
# get direction with minimum distance
# and check whether it's valid
chosenDirection = min(minDistanceInDirection, key=minDistanceInDirection.get)
minDistance = minDistanceInDirection[chosenDirection]
if minDistance == 10000:
debugLog("Unable to establish label position")
return
debugLog("Min distance: %d"%minDistance)
# return best direction where retrieve offsets
res = (minDistance, chosenDirection)
return res
def leftCheck(self, charRect):
objLeft, objTop, objRight, objBottom = self.objRect
charLeft, charTop, charRight, charBottom = charRect
if (
(objTop<charBottom<=objBottom)
and
(objLeft > charRight)
and
(objLeft-charRight <= self.maxHorizontalDistance)
):
distance = objLeft-charRight
return distance
def topCheck(self, charRect):
objLeft, objTop, objRight, objBottom = self.objRect
charLeft, charTop, charRight, charBottom = charRect
maxVerticalDistance = self.maxVerticalDistance if self.maxVerticalDistance else charRect.height
if (
(objLeft<=charLeft<objRight)
and
(objTop > charBottom)
and
(objTop-charBottom <= maxVerticalDistance)
):
distance = objTop-charBottom
return distance
def rightCheck(self, charRect):
objLeft, objTop, objRight, objBottom = self.objRect
charLeft, charTop, charRight, charBottom = charRect
if (
(objTop<charBottom<=objBottom)
and
(charLeft > objRight)
and
(charLeft-objRight <= self.maxHorizontalDistance)
):
distance = charLeft-objRight
return distance
def bottomCheck(self, charRect):
objLeft, objTop, objRight, objBottom = self.objRect
charLeft, charTop, charRight, charBottom = charRect
maxVerticalDistance = self.maxVerticalDistance if self.maxVerticalDistance else charRect.height
if (
(objLeft<=charLeft<objRight)
and
(charTop > objBottom)
and
(charTop-objBottom <= maxVerticalDistance)
):
distance = charTop-objBottom
return distance