-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmatcher4fmbt.py
189 lines (161 loc) · 7.37 KB
/
matcher4fmbt.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
186
187
188
189
'''
matcher for computer-vision based SW testing
Copyright (c) 2012-2014, Intel Corporation.
This program is free software; you can redistribute it and/or modify it
under the terms and conditions of the GNU Lesser General Public License,
version 2.1, as published by the Free Software Foundation.
This program is distributed in the hope it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
'''
import fmbtgti
import MatcherWrapper
def _area2searcharea(screenshot, area):
top, left = fmbtgti._intCoords(area[:2], screenshot.size())
bottom, right = fmbtgti._intCoords(area[2:], screenshot.size())
return (top, left, bottom, right)
def _resultbbox2bbox(resultbbox):
return (resultbbox.x,
resultbbox.y,
resultbbox.x + resultbbox.width,
resultbbox.y + resultbbox.height)
class MatcherOcrEngine(fmbtgti.OcrEngine):
"""
OCR engine specific parameters, can be used in all
...OcrText() methods:
match (float, optional):
minimum match score in range [0.0, 1.0].
The default is 1.0 (exact match).
area ((left, top, right, bottom), optional):
search from the given area only. Left, top
right and bottom are either absolute coordinates
(integers) or floats in range [0.0, 1.0]. In the
latter case they are scaled to screenshot
dimensions. The default is (0.0, 0.0, 1.0, 1.0),
that is, search everywhere in the screenshot.
"""
def __init__(self, *args, **kwargs):
fmbtgti.OcrEngine.__init__(self, *args, **kwargs)
mconfig = MatcherWrapper.MatcherConfig(useOCR = True, useOIR = False)
self._matcher = MatcherWrapper.Matcher(mconfig)
def _addScreenshot(self, screenshot, **neverMindDefaults):
self._matcher.loadImage(screenshot.filename())
def _removeScreenshot(self, screenshot):
self._matcher.unloadImage(screenshot.filename())
def _findText(self, screenshot, text, match=1.0, area=(0.0, 0.0, 1.0, 1.0)):
top, left = fmbtgti._intCoords(area[:2], screenshot.size())
bottom, right = fmbtgti._intCoords(area[2:], screenshot.size())
threshold = int(match*100)
result = self._matcher.match(
screenshot = screenshot.filename(),
icon = text,
threshold = threshold,
method = "OCR",
searcharea = _area2searcharea(screenshot, area),
resultimage = "")
if result.result[0]/100.0 < match:
return []
else:
return [fmbtgti.GUIItem(
"OCR text",
_resultbbox2bbox(result.bbox[0]),
screenshot = screenshot.filename(),
ocrFind = text,
ocrFound = "match:%.2f/%.2f" % (result.result[0]/100.0, match))]
_supportedOirMethods = {
"template": "MATCHTEMPLATE",
"feature": "FEATURE",
"sobel": "SOBEL" }
class MatcherOirEngine(fmbtgti.OirEngine):
"""
OIR engine specific parameters, can be used in all
...OcrBitmap() methods:
match (float, optional):
minimum match score in range [0.0, 1.0].
The default is 1.0 (exact match).
area ((left, top, right, bottom), optional):
search from the given area only. Left, top
right and bottom are either absolute coordinates
(integers) or floats in range [0.0, 1.0]. In the
latter case they are scaled to screenshot
dimensions. The default is (0.0, 0.0, 1.0, 1.0),
that is, search everywhere in the screenshot.
limit (integer, optional):
maximum number of returned matches. The default is
1.
method (string, optional): available matching methods are
"template", "feature", and "sobel". The default is
"template".
scale (float, optional):
??? should we provide a range, too: [1.0, 2.0, 0.1]
(try 1.0, 1.1, ..., 2.0)
features (integer, optional):
number of features used in feature matching. The
default is 10000.
nlevels (integer, optional):
???
edgeThreshold (integer, optional):
???
???
"""
def __init__(self, *args, **engineDefaults):
engineDefaults["match"] = engineDefaults.get("match", 1.0)
engineDefaults["area"] = engineDefaults.get("area", (0.0, 0.0, 1.0, 1.0))
engineDefaults["method"] = engineDefaults.get("method", "template")
engineDefaults["scale"] = engineDefaults.get("scale", 1.0)
engineDefaults["features"] = engineDefaults.get("features", 10000)
engineDefaults["nlevels"] = engineDefaults.get("nlevels", 16)
engineDefaults["edgeThreshold"] = engineDefaults.get("edgeThreshold", 9)
super(MatcherOirEngine, self).__init__(*args, **engineDefaults)
def _findBitmap(self, screenshot, bitmap,
match=None, area=None, limit=None,
method=None, scale=None, features=None,
nlevels=None, edgeThreshold=None):
if scale != None and scale > 0:
scale_invariant = True
else:
scale_invariant = False
mconfig = MatcherWrapper.MatcherConfig(
useOCR = False,
useOIR = True,
nfeatures = features,
scaleFactor = scale,
nlevels = nlevels,
edgeThreshold = edgeThreshold,
firstLevel = 0,
WTA_K = 2,
scoreType = 0,
patchSize = 19,
min_dist_thresh = 0,
max_nndr_ratio = 1.0,
min_inliers = 4,
gpu_enabled = False,
locate = True,
scale_invariant = scale_invariant,
ransac_reprojection_thresh = 10)
matcher = MatcherWrapper.Matcher(mconfig)
if method not in _supportedOirMethods:
raise ValueError('Invalid method "%s", use one of "%s"' %
'", "'.join(_supportedOirMethods.keys()))
result = matcher.match(
screenshot = screenshot.filename(),
icon = bitmap,
threshold = int(match*100),
method = _supportedOirMethods[method],
scale_factor = scale,
searcharea = _area2searcharea(screenshot, area),
resultimage = "")
if result.result[0] <= 0 or result.result[0] < (match*100):
return []
else:
# TODO: How to get all real matches from the result vector?
# now there can be only one
return [fmbtgti.GUIItem(
"%s match %.2f" % (method, result.result[0] / 100.0),
_resultbbox2bbox(result.bbox[0]),
screenshot = screenshot.filename(),
bitmap = bitmap)]