This repository has been archived by the owner on May 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
ocr.py
406 lines (354 loc) · 12 KB
/
ocr.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# This Python file uses the following encoding: utf-8
import os
import sys
import time
import argparse
import csv
import re
from joblib import Parallel, delayed
import portalocker
import pyocr
import pyocr.builders
from PIL import Image, ImageOps, ImageEnhance
# 同盟タグの切り抜き範囲
ALLIANCE_CROP_RANGE = (600, 331, 720, 363)
# IDの切り抜き範囲
ID_CROP_RANGE = (692, 197, 837, 225)
# 撃破数の切り抜き範囲
# (
# (T1撃破数範囲),
# (T2撃破数範囲),
# ...
# )
KILL_CROP_RANGES = (
(864, 430, 1070, 457),
(864, 474, 1070, 501),
(864, 518, 1070, 545),
(864, 562, 1070, 589),
(864, 606, 1070, 633),
)
# 撃破ポイントの切り抜き範囲
# (
# (T1撃破数範囲),
# (T2撃破数範囲),
# ...
# )
KILL_POINT_CROP_RANGES = (
(1211, 430, 1417, 457),
(1211, 474, 1417, 501),
(1211, 518, 1417, 545),
(1211, 562, 1417, 589),
(1211, 606, 1417, 633),
)
# 撃破ポイント係数
# (T1, T2, T3, T4, T5)
KILL_POINT_COEFFICIENTS = (0.2, 2, 4, 10, 20)
# 遠隔ポイントの切り抜き範囲
RANGED_POINT_CROP_RANGE = (1117, 707, 1417, 738)
# 戦力の切り抜き範囲
POWER_CROP_RANGE = (809, 141, 1009, 173)
# 過去最大戦力の切り抜き範囲
HIGHEST_POWER_CROP_RANGE = (1103, 266, 1303, 296)
# 戦死数の切り抜き範囲
DEAD_CROP_RANGE = (1103, 450, 1303, 480)
# 資源援助数の切り抜き範囲
RSS_CROP_RANGE = (1053, 680, 1303, 710)
args = None
tool = None
dir_path: str = None
img_dir_path: str = None
log_dir_path: str = None
class Builder(pyocr.builders.TextBuilder):
def __init__(self, whitelist: str, tesseract_layout: int = 6):
super(Builder, self).__init__(tesseract_layout)
self.tesseract_configs += ["-c", "tessedit_char_whitelist=" + whitelist]
def main():
startTime = time.time()
global args, tool, dir_path, img_dir_path, log_dir_path
parser = argparse.ArgumentParser()
parser.add_argument("dir", type=str)
parser.add_argument("-j", "--jobs", type=int, default=-2)
parser.add_argument(
"-t",
"--targets",
nargs="*",
type=str,
default=[
"id",
"alliance",
"power",
"hpower",
"t1kill",
"t2kill",
"t3kill",
"t4kill",
"t5kill",
"ranged",
"dead",
"rss",
],
)
parser.add_argument("--killtest", action="store_true")
args = parser.parse_args()
tools = pyocr.get_available_tools()
if len(tools) == 0:
print("OCRツールが見つかりません。")
sys.exit(1)
tool = tools[0]
dir_path = "data/" + args.dir + "/"
img_dir_path = dir_path + "screenshots/"
log_dir_path = dir_path + "logs/ocr/"
data = []
os.makedirs(log_dir_path, exist_ok=True)
with open(img_dir_path + "names.tsv", "r", encoding="utf_8", newline="") as fh:
names = list(csv.reader(fh, delimiter="\t"))
data += Parallel(n_jobs=args.jobs)(
delayed(ocr_images)(rank, name) for rank, name in names
)
with open(dir_path + args.dir + ".tsv", "w", encoding="utf_8", newline="") as fh:
data = sorted(data, key=lambda x: int(x[0]))
data.insert(
0,
(
"Rank",
"ID",
"Name",
"Alliance",
"Power",
"Highest Power",
"T1 Kills",
"T2 Kills",
"T3 Kills",
"T4 Kills",
"T5 Kills",
"Ranged Points",
"Dead",
"RSS",
),
)
csv.writer(fh, delimiter="\t").writerows(data)
endTime = time.time()
print(f"\n処理時間:{endTime - startTime}")
def ocr_images(rank: str, name: str):
img_a = Image.open(img_dir_path + rank + "a.png")
if img_a.mode == "RGBA":
img_a = img_a.convert("RGB")
img_b = Image.open(img_dir_path + rank + "b.png")
if img_b.mode == "RGBA":
img_b = img_b.convert("RGB")
# ID
id = ""
if "id" in args.targets:
id_img = correct_image(
img_a, ID_CROP_RANGE, threshold=50, contrast=5, brightness=2
)
id = ocr_image(id_img, whitelist="0123456789)")
id = re.sub("\)$", "", id)
if id == "":
err(
f"{rank}位 - {name}: 「ID」の読み取りに失敗しました。 -> {rank}-id.png",
[(f"{rank}-id.png", id_img)],
)
# 同盟タグ
alliance = ""
if "alliance" in args.targets:
alliance_img = correct_image(
img_a, ALLIANCE_CROP_RANGE, contrast=1.3, brightness=2
)
alliance = ocr_image(
alliance_img,
whitelist=f"[]0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz`~!@#&-_=+;:'\",<>/",
)
alliance = re.match(r"^\[(.{3,4})\]", alliance)
if alliance is None:
alliance = ""
else:
alliance = alliance.group(1)
# 撃破
kills = []
for i, (
kill_crop_range,
kill_point_crop_range,
kill_point_coefficient,
) in enumerate(
zip(KILL_CROP_RANGES, KILL_POINT_CROP_RANGES, KILL_POINT_COEFFICIENTS)
):
kill = ""
if "kill" in args.targets or f"t{i+1}kill" in args.targets:
kill_img = correct_image(
img_a,
kill_crop_range,
threshold=50,
invert=False,
brightness=1.2,
contrast=1.2,
)
kill = ocr_image(kill_img)
kill = kill.replace(",", "")
kill_img_file_name = rank + "-t" + str(i + 1) + "-kill.png"
if args.killtest:
kill_p_img = correct_image(
img_a,
kill_point_crop_range,
threshold=50,
invert=False,
brightness=1.2,
contrast=1.2,
)
kill_p = ocr_image(kill_p_img)
kill_p = kill_p.replace(",", "")
kill_p_img_file_name = rank + "-t" + str(i + 1) + "-kill-point.png"
if kill == "":
err(
f"{rank}位 - {name}: 「T{i + 1}撃破数」の読み取りに失敗しました。 -> {kill_img_file_name}, {kill_p_img_file_name}",
[
(kill_img_file_name, kill_img),
(kill_p_img_file_name, kill_p_img),
],
)
elif abs(int(kill) * kill_point_coefficient - int(kill_p)) > 1:
err(
f"{rank}位 - {name}: 「T{i + 1}撃破数」を正しく読み取りできなかった可能性があります。OCRの結果は撃破数「{kill}」、撃破ポイント「{kill_p}」です。 -> {kill_img_file_name}, {kill_p_img_file_name}",
[
(kill_img_file_name, kill_img),
(kill_p_img_file_name, kill_p_img),
],
)
else:
if kill == "":
err(
f"{rank}位 - {name}: 「T{i + 1}撃破数」の読み取りに失敗しました。 -> {kill_img_file_name}",
[(kill_img_file_name, kill_img)],
)
kills.append(kill)
# 遠隔ポイント
ranged = ""
if "ranged" in args.targets:
ranged_img = correct_image(
img_a,
RANGED_POINT_CROP_RANGE,
threshold=50,
invert=False,
brightness=1.2,
contrast=2,
)
ranged = ocr_image(ranged_img)
ranged = ranged.replace(",", "")
if ranged == "":
err(
f"{rank}位 - {name}: 「遠隔ポイント」の読み取りに失敗しました。 -> {rank}-ranged.png",
[(f"{rank}-ranged.png", ranged_img)],
)
# 戦力
power = ""
if "power" in args.targets:
power_img = correct_image(
img_b, POWER_CROP_RANGE, threshold=50, brightness=2, contrast=1.2
)
power = ocr_image(power_img)
power = power.replace(",", "")
if power == "":
err(
f"{rank}位 - {name}: 「戦力」の読み取りに失敗しました。 -> {rank}-power.png",
[(f"{rank}-power.png", power_img)],
)
# 過去最大戦力
hpower = ""
if "hpower" in args.targets:
hpower_img = correct_image(
img_b, HIGHEST_POWER_CROP_RANGE, brightness=1.3, contrast=1.8
)
hpower = ocr_image(hpower_img)
hpower = hpower.replace(",", "")
if hpower == "":
err(
f"{rank}位 - {name}: 「過去最大戦力」の読み取りに失敗しました。 -> {rank}-hpower.png",
[(f"{rank}-dead.png", hpower_img)],
)
# 戦力チェック
if power != "" and hpower != "" and int(power) > int(hpower):
err(
f"{rank}位 - {name}: 「戦力」または「過去最大戦力」を正しく読み取りできなかった可能性があります。OCRの結果は戦力「{power}」、過去最大戦力「{hpower}」です。 -> {rank}-power.png, {rank}-hpower.png",
[(f"{rank}-power.png", power_img), (f"{rank}-hpower.png", hpower_img)],
)
# 戦死
dead = ""
if "dead" in args.targets:
dead_img = correct_image(img_b, DEAD_CROP_RANGE, brightness=1.3, contrast=1.8)
dead = ocr_image(dead_img)
dead = dead.replace(",", "")
if dead == "":
err(
f"{rank}位 - {name}: 「戦死数」の読み取りに失敗しました。 -> {rank}-dead.png",
[(f"{rank}-dead.png", dead_img)],
)
# 資源援助
rss = ""
if "rss" in args.targets:
rss_img = correct_image(img_b, RSS_CROP_RANGE, brightness=1.3, contrast=1.8)
rss = ocr_image(rss_img)
rss = rss.replace(",", "")
if rss == "":
err(
f"{rank}位 - {name}: 「資源援助数」の読み取りに失敗しました。 -> {rank}-rss.png",
[(f"{rank}-rss.png", rss_img)],
)
print(
f"{rank} {id} {name} {alliance} {power} {hpower} {kills[0]} {kills[1]} {kills[2]} {kills[3]} {kills[4]} {ranged} {dead} {rss}"
)
return (
rank,
id,
name,
alliance,
power,
hpower,
kills[0],
kills[1],
kills[2],
kills[3],
kills[4],
ranged,
dead,
rss,
)
def correct_image(
img: Image.Image,
crop_range: tuple,
threshold: int = 0,
threshold_max: int = -1,
invert: bool = True,
scale: float = 1,
contrast: float = 1,
brightness: float = 1,
) -> Image.Image:
tmp = img.crop(crop_range)
tmp = ImageOps.invert(tmp) if invert else tmp
tmp = tmp.convert("L")
tmp = (
tmp.resize((round(tmp.width * scale), round(tmp.height * scale)))
if scale != 1
else tmp
)
tmp = ImageEnhance.Contrast(tmp).enhance(contrast) if contrast != 1 else tmp
tmp = ImageEnhance.Brightness(tmp).enhance(brightness) if brightness != 1 else tmp
if threshold == 0:
pass
elif threshold_max == -1:
tmp = tmp.point(lambda x: 0 if x < threshold else x)
else:
tmp = tmp.point(lambda x: 0 if x < threshold else threshold_max)
return tmp
def ocr_image(img: Image, whitelist: str = "0123456789,") -> str:
result = tool.image_to_string(img, lang="eng", builder=Builder(whitelist))
return result
def err(message: str, imgs: list[tuple[str, Image.Image]] = {}):
print(message)
for file_name, img in imgs:
img.save(log_dir_path + file_name)
with portalocker.Lock(
log_dir_path + "error.log", "a", encoding="utf_8", timeout=60
) as fh:
fh.write(message + "\n")
if __name__ == "__main__":
main()