-
Notifications
You must be signed in to change notification settings - Fork 850
/
button_extract.py
226 lines (190 loc) · 7.79 KB
/
button_extract.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
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
import os
import imageio
import numpy as np
from tqdm.contrib.concurrent import process_map
from module.base.utils import get_bbox, get_color, image_size, load_image
from module.config.config_manual import ManualConfig as AzurLaneConfig
from module.config.server import VALID_SERVER
from module.logger import logger
MODULE_FOLDER = './module'
BUTTON_FILE = 'assets.py'
IMPORT_EXP = """
from module.base.button import Button
from module.base.template import Template
# This file was automatically generated by dev_tools/button_extract.py.
# Don't modify it manually.
"""
IMPORT_EXP = IMPORT_EXP.strip().split('\n') + ['']
class ImageExtractor:
def __init__(self, module, file):
"""
Args:
module(str):
file(str): xxx.png or xxx.gif
"""
self.module = module
self.name, self.ext = os.path.splitext(file)
self.area, self.color, self.button, self.file = {}, {}, {}, {}
for server in VALID_SERVER:
self.load(server)
def get_file(self, genre='', server='cn'):
for ext in ['.png', '.gif']:
file = f'{self.name}.{genre}{ext}' if genre else f'{self.name}{ext}'
file = os.path.join(AzurLaneConfig.ASSETS_FOLDER, server, self.module, file).replace('\\', '/')
if os.path.exists(file):
return file
ext = '.png'
file = f'{self.name}.{genre}{ext}' if genre else f'{self.name}{ext}'
file = os.path.join(AzurLaneConfig.ASSETS_FOLDER, server, self.module, file).replace('\\', '/')
return file
def extract(self, file):
if os.path.splitext(file)[1] == '.gif':
# In a gif Button, use the first image.
bbox = None
mean = None
for image in imageio.mimread(file):
image = image[:, :, :3] if len(image.shape) == 3 else image
new_bbox, new_mean = self._extract(image, file)
if bbox is None:
bbox = new_bbox
elif bbox != new_bbox:
logger.warning(f'{file} has multiple different bbox, this will cause unexpected behaviour')
if mean is None:
mean = new_mean
return bbox, mean
else:
image = load_image(file)
return self._extract(image, file)
@staticmethod
def _extract(image, file):
size = image_size(image)
if size != (1280, 720):
logger.warning(f'{file} has wrong resolution: {size}')
bbox = get_bbox(image)
mean = get_color(image=image, area=bbox)
mean = tuple(np.rint(mean).astype(int))
return bbox, mean
def load(self, server='cn'):
file = self.get_file(server=server)
if os.path.exists(file):
area, color = self.extract(file)
button = area
override = self.get_file('AREA', server=server)
if os.path.exists(override):
area, _ = self.extract(override)
override = self.get_file('COLOR', server=server)
if os.path.exists(override):
_, color = self.extract(override)
override = self.get_file('BUTTON', server=server)
if os.path.exists(override):
button, _ = self.extract(override)
self.area[server] = area
self.color[server] = color
self.button[server] = button
self.file[server] = file
else:
logger.attr(server, f'{self.name} not found, use cn server assets')
self.area[server] = self.area['cn']
self.color[server] = self.color['cn']
self.button[server] = self.button['cn']
self.file[server] = self.file['cn']
@property
def expression(self):
return '%s = Button(area=%s, color=%s, button=%s, file=%s)' % (
self.name, self.area, self.color, self.button, self.file)
class TemplateExtractor(ImageExtractor):
# def __init__(self, module, file, config):
# """
# Args:
# module(str):
# file(str): xxx.png
# config(AzurLaneConfig):
# """
# self.module = module
# self.file = file
# self.config = config
@staticmethod
def extract(file):
image = load_image(file)
bbox = get_bbox(image)
mean = get_color(image=image, area=bbox)
mean = tuple(np.rint(mean).astype(int))
return bbox, mean
@property
def expression(self):
return '%s = Template(file=%s)' % (
self.name, self.file)
# return '%s = Template(area=%s, color=%s, button=%s, file=\'%s\')' % (
# self.name, self.area, self.color, self.button,
# self.config.ASSETS_FOLDER + '/' + self.module + '/' + self.name + '.png')
# class OcrExtractor(ImageExtractor):
# @property
# def expression(self):
# return '%s = OcrArea(area=%s, color=%s, button=%s, file=\'%s\')' % (
# self.name, self.area, self.color, self.button,
# self.config.ASSETS_FOLDER + '/' + self.module + '/' + self.name + '.png')
class ModuleExtractor:
def __init__(self, name):
self.name = name
self.folder = os.path.join(AzurLaneConfig.ASSETS_FOLDER, 'cn', name)
@staticmethod
def split(file):
name, ext = os.path.splitext(file)
name, sub = os.path.splitext(name)
return name, sub, ext
def is_base_image(self, file):
_, sub, _ = self.split(file)
return sub == ''
@property
def expression(self):
exp = []
for file in os.listdir(self.folder):
if file[0].isdigit():
continue
if file.startswith('TEMPLATE_'):
exp.append(TemplateExtractor(module=self.name, file=file).expression)
continue
# if file.startswith('OCR_'):
# exp.append(OcrExtractor(module=self.name, file=file, config=self.config).expression)
# continue
if self.is_base_image(file):
exp.append(ImageExtractor(module=self.name, file=file).expression)
continue
exp.sort()
logger.info('Module: %s(%s)' % (self.name, len(exp)))
exp = IMPORT_EXP + exp
return exp
def write(self):
folder = os.path.join(MODULE_FOLDER, self.name)
if not os.path.exists(folder):
os.mkdir(folder)
with open(os.path.join(folder, BUTTON_FILE), 'w', newline='') as f:
for text in self.expression:
f.write(text + '\n')
def worker(module):
me = ModuleExtractor(name=module)
me.write()
class AssetExtractor:
"""
Extract Asset to asset.py.
All the filename of assets should be in uppercase.
Asset name starts with digit will be ignore.
E.g. 2020XXXX.png.
Asset name starts with 'TEMPLATE_' will treat as template.
E.g. TEMPLATE_AMBUSH_EVADE_SUCCESS.png
> TEMPLATE_AMBUSH_EVADE_SUCCESS = Template(file='./assets/handler/TEMPLATE_AMBUSH_EVADE_SUCCESS.png')
Asset name starts other will treat as button.
E.g. GET_MISSION.png
> Button(area=(553, 482, 727, 539), color=(93, 142, 203), button=(553, 482, 727, 539), name='GET_MISSION')
Asset name like XXX.AREA.png, XXX.COLOR.png, XXX.BUTTON.png, will overwrite the attribute of XXX.png.
E.g. BATTLE_STATUS_S.BUTTON.png overwrites the attribute 'button' of BATTLE_STATUS_S
Asset name starts with 'OCR_' will be treat as button.
E.g. OCR_EXERCISE_TIMES.png.
"""
def __init__(self):
logger.info('Assets extract')
modules = [m for m in os.listdir(AzurLaneConfig.ASSETS_FOLDER + '/cn')
if os.path.isdir(os.path.join(AzurLaneConfig.ASSETS_FOLDER + '/cn', m))]
process_map(worker, modules)
if __name__ == '__main__':
ae = AssetExtractor()