-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbutton.py
More file actions
248 lines (192 loc) · 6.85 KB
/
button.py
File metadata and controls
248 lines (192 loc) · 6.85 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
import module.config.server as server
from module.base.decorator import cached_property, del_cached_property
from module.base.resource import Resource
from module.base.utils import *
from module.exception import ScriptError
class Button(Resource):
def __init__(self, file, area, search, color, button):
"""
Args:
file: Filepath to an assets
area: Area to crop template
search: Area to search from, 20px larger than `area` by default
color: Average color of assets
button: Area to click if assets appears on the image
"""
self.file: str = file
self.area: t.Tuple[int, int, int, int] = area
self.search: t.Tuple[int, int, int, int] = search
self.color: t.Tuple[int, int, int] = color
self._button: t.Tuple[int, int, int, int] = button
self.resource_add(self.file)
self._button_offset: t.Tuple[int, int] = (0, 0)
@property
def button(self):
return area_offset(self._button, self._button_offset)
def clear_offset(self):
self._button_offset = (0, 0)
@cached_property
def image(self):
return load_image(self.file, self.area)
def resource_release(self):
del_cached_property(self, 'image')
self.clear_offset()
def __str__(self):
return self.file
__repr__ = __str__
def __eq__(self, other):
return str(self) == str(other)
def __hash__(self):
return hash(self.file)
def __bool__(self):
return True
def match_color(self, image, threshold=10) -> bool:
"""
Check if the button appears on the image, using average color
Args:
image (np.ndarray): Screenshot.
threshold (int): Default to 10.
Returns:
bool: True if button appears on screenshot.
"""
color = get_color(image, self.area)
return color_similar(
color1=color,
color2=self.color,
threshold=threshold
)
def match_template(self, image, similarity=0.85) -> bool:
"""
Detects assets by template matching.
To Some buttons, its location may not be static, `_button_offset` will be set.
Args:
image: Screenshot.
similarity (float): 0-1.
Returns:
bool.
"""
image = crop(image, self.search, copy=False)
res = cv2.matchTemplate(self.image, image, cv2.TM_CCOEFF_NORMED)
_, sim, _, point = cv2.minMaxLoc(res)
self._button_offset = np.array(point) + self.search[:2] - self.area[:2]
return sim > similarity
def match_template_color(self, image, similarity=0.85, threshold=30) -> bool:
"""
Template match first, color match then
Args:
image: Screenshot.
similarity (float): 0-1.
threshold (int): Default to 10.
Returns:
"""
matched = self.match_template(image, similarity=similarity)
if not matched:
return False
area = area_offset(self.area, offset=self._button_offset)
color = get_color(image, area)
return color_similar(
color1=color,
color2=self.color,
threshold=threshold
)
class ButtonWrapper(Resource):
def __init__(self, name='MULTI_ASSETS', **kwargs):
self.name = name
self.data_buttons = kwargs
self._matched_button: t.Optional[Button] = None
self.resource_add(self.name)
def resource_release(self):
del_cached_property(self, 'assets')
self._matched_button = None
def __str__(self):
return self.name
__repr__ = __str__
def __eq__(self, other):
return str(self) == str(other)
def __hash__(self):
return hash(self.name)
def __bool__(self):
return True
@cached_property
def buttons(self) -> t.List[Button]:
for trial in [server.server, 'share', 'cn']:
assets = self.data_buttons.get(trial, None)
if assets is not None:
if isinstance(assets, Button):
return [assets]
elif isinstance(assets, list):
return assets
raise ScriptError(f'ButtonWrapper({self}) on server {server.server} has no fallback button')
def match_color(self, image, threshold=10) -> bool:
for assets in self.buttons:
if assets.match_color(image, threshold=threshold):
self._matched_button = assets
return True
return False
def match_template(self, image, similarity=0.85) -> bool:
for assets in self.buttons:
if assets.match_template(image, similarity=similarity):
self._matched_button = assets
return True
return False
def match_template_color(self, image, similarity=0.85, threshold=30) -> bool:
for assets in self.buttons:
if assets.match_template_color(image, similarity=similarity, threshold=threshold):
self._matched_button = assets
return True
return False
@property
def matched_button(self) -> Button:
if self._matched_button is None:
return self.buttons[0]
else:
return self._matched_button
@property
def area(self) -> tuple[int, int, int, int]:
return self.matched_button.area
@property
def search(self) -> tuple[int, int, int, int]:
return self.matched_button.search
@property
def color(self) -> tuple[int, int, int]:
return self.matched_button.color
@property
def button(self) -> tuple[int, int, int, int]:
return self.matched_button.button
@property
def button_offset(self) -> tuple[int, int]:
return self.matched_button._button_offset
@property
def width(self) -> int:
return area_size(self.area)[0]
@property
def height(self) -> int:
return area_size(self.area)[1]
class ClickButton:
def __init__(self, button, name='CLICK_BUTTON'):
self.area = button
self.button = button
self.name = name
def __str__(self):
return self.name
__repr__ = __str__
def __eq__(self, other):
return str(self) == str(other)
def __hash__(self):
return hash(self.name)
def __bool__(self):
return True
def match_template(image, template, similarity=0.85):
"""
Args:
image (np.ndarray): Screenshot
template (np.ndarray):
area (tuple): Crop area of image.
offset (int, tuple): Detection area offset.
similarity (float): 0-1. Similarity. Lower than this value will return float(0).
Returns:
bool:
"""
res = cv2.matchTemplate(image, template, cv2.TM_CCOEFF_NORMED)
_, sim, _, point = cv2.minMaxLoc(res)
return sim > similarity