-
Notifications
You must be signed in to change notification settings - Fork 1
/
process.py
356 lines (308 loc) · 11.7 KB
/
process.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
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
from collections import defaultdict
from itertools import count
from multiprocessing import Pool
from random import sample
from tqdm import tqdm
from typing import Any, Optional, Iterator
import cv2
import json
import numpy as np
import os
import re
import sys
PATH = "dist"
PATH_DIR = os.path.dirname(os.path.abspath(__file__))
TAGS_FILE = os.path.join(PATH_DIR, "notebooks/tags.json")
TILE_SIZE = 16
SIMILARITY = 0.9
IMAGE_EXTENSIONS = (".png", ".jpg", ".jpeg", ".bmp", ".gif")
EXCLUDED_TAGS = (
"modernexteriors",
"moderninteriors",
"modern",
"sorter",
"complete",
"animated",
"animation",
"single",
"tileset",
"character",
"gifs",
"16x16",
"32x32",
"48x48",
"and",
"the",
"win",
"v41",
)
Id = str
Region = tuple[int, int, int, int]
Tile = tuple[Region, Region, float]
class BaseImg:
id_counter = count(0)
tag_data = json.load(open(TAGS_FILE, "r"))
def __init__(self, path: str):
self.path: str = path
self.kind: str = self.__class__.__name__.lower()
self.tags: list[str] = self.__parse_tags(path, self.kind)
self.id: Id = f"#{next(self.id_counter)}"
def __repr__(self):
return f"{self.__class__.__name__}({self.path})"
def __eq__(self, other: object) -> bool:
if not isinstance(other, BaseImg):
return NotImplemented
return self.path == other.path
def __hash__(self) -> int:
return hash(self.path)
def to_json(self) -> dict[str, Any]:
return {
"kind": self.kind,
"path": self.path,
"tags": tuple(self.tags),
}
@staticmethod
def __parse_tags(path: str, kind: str) -> list[str]:
base_name = os.path.basename(path)
unique = [kind + "s"] + BaseImg.tag_data.get(base_name, [])
clean_path = os.path.splitext(path)[0].lower()
clean_path = re.sub(r"[^a-z0-9]+", " ", clean_path).strip()
for word in clean_path.split():
if (
len(word) > 2
and not word.isdigit()
and word not in unique
and word not in EXCLUDED_TAGS
):
unique.append(word)
unique.reverse()
return unique
class BaseCV2Img(BaseImg):
def __init__(self, path: str):
super().__init__(path)
self.cv2: cv2 = cv2.imread(path, cv2.IMREAD_UNCHANGED)
self.shape: tuple[int, int] = self.cv2.shape[1], self.cv2.shape[0]
def __eq__(self, other: object) -> bool:
if not isinstance(other, BaseCV2Img):
return NotImplemented
return (self.shape == other.shape) and (self.cv2 == other.cv2).all()
def __hash__(self) -> int:
return hash((self.shape, self.cv2.tobytes()))
def to_json(self) -> dict[str, Any]:
as_json = super().to_json()
as_json["shape"] = self.shape
return as_json
class Character(BaseImg):
pass
class Animation(BaseImg):
pass
class Single(BaseCV2Img):
def __init__(self, path: str):
super().__init__(path)
self.tilesets: set[Id] = set()
def add_tileset(self, tileset: "Tileset") -> None:
self.tilesets.add(tileset.id)
def to_json(self) -> dict[str, Any]:
as_json = super().to_json()
as_json["tilesets"] = tuple(self.tilesets)
return as_json
class Tileset(BaseCV2Img):
def __init__(self, path: str):
super().__init__(path)
self.tiles: dict[Id, list[Tile]] = defaultdict(list)
def add_tile(self, single: Single, tile: Tile) -> None:
self.tiles[single.id].append(tile)
def search(
self, single: Single, threshold=SIMILARITY, size=TILE_SIZE
) -> Iterator[Tile]:
# check if the single image is in the tileset
th, tw, _ = self.cv2.shape
sh, sw, _ = single.cv2.shape
opaque_pixels_count = np.count_nonzero(single.cv2[:, :, 3] == 255)
for j in range(size - sh, th, size):
for i in range(size - sw, tw, size):
ty0, ty1 = max(0, j), min(th, j + sh)
tx0, tx1 = max(0, i), min(tw, i + sw)
t = self.cv2[ty0:ty1, tx0:tx1]
sy0, sy1 = max(0, -j), min(sh, th - j)
sx0, sx1 = max(0, -i), min(sw, tw - i)
s = single.cv2[sy0:sy1, sx0:sx1]
score = self.__compute_score(s, t, opaque_pixels_count)
if score > threshold:
t_region = tx0, ty0, tx1 - tx0, ty1 - ty0
s_region = sx0, sy0, sx1 - sx0, sy1 - sy0
yield t_region, s_region, (score - threshold) / (1 - threshold)
def to_json(self) -> dict[str, Any]:
as_json = super().to_json()
as_json["tiles"] = self.tiles
return as_json
@classmethod
def _compute_score(cls, a: cv2, b: cv2) -> float:
"""Used for debugging."""
denominator = np.count_nonzero(a[:, :, 3] == 255)
return cls.__compute_score(a, b, denominator)
@staticmethod
def __compute_score(a: cv2, b: cv2, denominator: float) -> float:
"""Return an index of how much a matches b,
where 0.0 is no match, and 1.0 is a perfect match."""
if denominator == 0:
return 0.0
comp = (a == b).min(axis=2)
mask = a[:, :, 3] == 255
return np.count_nonzero(comp & mask) / denominator
@staticmethod
def __jaccard(a: cv2, b: cv2) -> float:
"""Return the Jaccard index of two images."""
diff = BaseCV2Img.__difference(a, b)
mask = diff[:, :, 3]
mask_count = np.count_nonzero(mask)
if mask_count == 0:
return 0.0
black = (diff[:, :, :3].max(axis=2) == 0) & mask
return np.count_nonzero(black) / mask_count
class BaseLoader:
def __init__(self):
self.indices = defaultdict(set)
def load(self, path: str) -> Iterator[BaseImg]:
# iterate over all files in the asset pack
for root, _, files in os.walk(path):
for file in files:
if image := self._load_image(os.path.join(root, file)):
self.indices[image.kind].add(image)
n = len(self.indices["single"])
m = len(self.indices["tileset"])
print(f"Loaded {n} singles and {m} tilesets from {path}.")
def search(self):
# iterate over all singles and search for them in the tilesets
args = list(self._get_search_pairs())
if len(sys.argv) > 1:
args = sample(args, int(sys.argv[1]))
print(f"Searching {len(args)} pairs...")
with Pool(12) as pool:
results = pool.imap_unordered(self._search, args, 32)
results = list(tqdm(results, total=len(args)))
# update the tilesets and singles with the results
tileset_index = {image.id: image for image in self.indices["tileset"]}
single_index = {image.id: image for image in self.indices["single"]}
found = 0
for tileset_id, single_id, tiles in results:
if not tiles:
continue
tileset = tileset_index[tileset_id]
single = single_index[single_id]
single.add_tileset(tileset)
for tile in tiles:
tileset.add_tile(single, tile)
found += 1
print(f"Found {found} matches.")
def to_json(self) -> dict[str, Any]:
return {
image.id: image.to_json()
for index in self.indices.values()
for image in index
}
def _load_image(self, path: str) -> Optional[BaseImg]:
lower = path.lower()
ext = os.path.splitext(path)[1]
if ext not in IMAGE_EXTENSIONS:
# ingore unwanted files like txt, zip, ini, etc.
return None
if "palette" in lower:
# ignore palette images
return None
if "32x32" in lower or "48x48" in lower:
# these are duplicates of the 16x16 images
return None
if "animated" in lower or "animation" in lower:
if ext != ".gif":
# character sheets are ignored for now
return None
return Animation(path)
if "character" in lower:
return Character(path)
if "single" in lower:
return Single(path)
return Tileset(path)
def _get_search_pairs(self) -> Iterator[tuple[Tileset, Single]]:
for tileset in self.indices["tileset"]:
for single in self.indices["single"]:
yield tileset, single
@staticmethod
def _search(args: tuple[Tileset, Single]) -> tuple[Id, Id, list[Tile]]:
tileset, single = args
results = tileset.search(single)
return tileset.id, single.id, list(results)
class ModernExteriorsLoader(BaseLoader):
def _load_image(self, path: str) -> Optional[BaseImg]:
if "old_sorting" in path.lower():
# these are old tilesets
return None
if "complete_singles" in path.lower():
# these are duplicates of Theme Sorter
return None
return super()._load_image(path)
def _get_search_pairs(self) -> Iterator[tuple[Tileset, Single]]:
for tileset, single in super()._get_search_pairs():
if (
"theme_sorter" in tileset.path.lower()
and "singles" in single.path.lower()
):
# we can skip the search if the names don't match
single_dir = os.path.dirname(single.path)
_, *single_theme, __ = os.path.basename(single_dir).split("_")
_, *tileset_theme, __ = os.path.basename(tileset.path).split("_")
if single_theme[:-1] != tileset_theme:
continue
yield tileset, single
class ModernInteriorsLoader(BaseLoader):
def _load_image(self, path: str) -> Optional[BaseImg]:
if "old stuff" in path.lower():
# these are old tilesets
return None
if "black_shadow" in path.lower():
# these are duplicates of the normal tiles
return None
if "shadowless" in path.lower():
# these are duplicates of the normal tiles
return None
if "user_interface" in path.lower():
# TODO: these are not supported for now
return None
if "home_designs" in path.lower():
# TODO: these are not supported for now
return None
if "room_builder_subfiles" in path.lower():
# these are not singles
return None
return super()._load_image(path)
def _get_search_pairs(self) -> Iterator[tuple[Tileset, Single]]:
for tileset, single in super()._get_search_pairs():
if (
"theme_sorter" in tileset.path.lower()
and "theme_sorter_singles" in single.path.lower()
):
# we can skip the search if the names don't match
single_dir = os.path.dirname(single.path)
_, *single_theme, __ = os.path.basename(single_dir).split("_")
_, *tileset_theme, __ = os.path.basename(tileset.path).split("_")
if single_theme != tileset_theme:
continue
yield tileset, single
if __name__ == "__main__":
os.chdir(PATH)
collections = {
"modernexteriors-win": ModernExteriorsLoader,
"moderninteriors-win": ModernInteriorsLoader,
}
output = {}
for path, loader_class in collections.items():
loader = loader_class()
loader.load(path)
loader.search()
data = loader.to_json()
output.update(data)
with open(f"{path}.json", "w") as f:
json.dump(data, f)
with open("data.json", "w") as f:
json.dump(output, f)
print("Saved data.json.")