-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplates.py
175 lines (138 loc) · 4.94 KB
/
templates.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
from __future__ import annotations
import json
from pathlib import Path
import cv2
import numpy as np
from credit_store_recognizer.ocr import ocrhandle
from credit_store_recognizer.solvers.shop import CreditStore
class Logger:
def __getattr__(self, name):
return print
logger = Logger()
shop_items = [
"讯使",
"嘉维尔",
"坚雷",
"招聘许可",
"龙门币",
"技巧概要·卷2",
"初级作战记录",
"基础作战记录",
"技巧概要·卷1",
"异铁",
"装置",
"酮凝集",
"固源岩",
"糖",
"聚酸酯",
"赤金",
"代糖",
"异铁碎片",
"酯原料",
"双酮",
"破损装置",
"源岩",
"碳",
"碳素",
"家具零件",
"加急许可"
]
def load_image(image, flags: cv2.ImreadModes = cv2.IMREAD_COLOR):
if isinstance(image, (str, Path)):
return cv2.imdecode(np.fromfile(image, dtype=np.uint8), flags)
elif isinstance(image, (bytes, bytearray, memoryview)):
return cv2.imdecode(np.frombuffer(image, dtype=np.uint8), flags)
else:
return image
def save_image(image, path: str | Path) -> None:
path = Path(path)
path.parent.mkdir(parents=True, exist_ok=True)
success, buffer = cv2.imencode('.png', image)
path.write_bytes(buffer)
def linear_operation(image, min: int, max: int):
image = (image.astype(np.float32) - min) / (max - min) * 255
return np.clip(image, 0, 255).astype(np.uint8)
def scope2slice(scope):
""" ((x0, y0), (x1, y1)) -> ((y0, y1), (x0, x1)) """
if scope is None:
return slice(None), slice(None)
(x0, y0), (x1, y1) = scope
return slice(y0, y1), slice(x0, x1)
scopes = [
((25, 222), (378, 576)),
((405, 222), (757, 576)),
((784, 222), (1137, 576)),
((1164, 222), (1516, 576)),
((1544, 222), (1896, 576)),
((25, 603), (378, 957)),
((405, 603), (757, 957)),
((784, 603), (1137, 957)),
((1164, 603), (1516, 957)),
((1544, 603), (1896, 957)),
]
sold_out_image = load_image('credit_store_recognizer/resources/sold_out.png')
def get_item_image(image, index: int):
return image[scope2slice(scopes[index])]
seen = set()
def get_item_name(item_image, sold: bool):
scope = ((16, 0), (353-16, 54))
item_name_segment = item_image[scope2slice(scope)]
item_name_segment = cv2.cvtColor(item_name_segment, cv2.COLOR_BGR2GRAY)
min_value, max_value = (177, 205) if sold else (49, 255)
item_name_segment = linear_operation(item_name_segment, min_value, max_value)
item_name_segment_to_ocr = np.vstack((item_name_segment, np.zeros(item_name_segment.shape, dtype=np.uint8), np.zeros(item_name_segment.shape, dtype=np.uint8)))
item_name_segment_to_ocr = cv2.cvtColor(item_name_segment_to_ocr, cv2.COLOR_GRAY2BGR)
# cv2.imshow('item_name_segment', item_name_segment)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
ocr = ocrhandle.predict(item_name_segment_to_ocr, False)
if len(ocr) == 0:
logger.error(('OCR未返回结果'))
cv2.imshow('item_name_segment', item_name_segment)
cv2.waitKey(0)
cv2.destroyAllWindows()
return
item_name = ocr[0][1]
if item_name not in shop_items:
logger.error(('OCR结果不在列表中', item_name))
cv2.imshow('item_name_segment', item_name_segment)
cv2.waitKey(0)
cv2.destroyAllWindows()
return
if item_name not in seen:
seen.add(item_name)
save_image(item_name_segment, f'resources/{item_name}.png')
if set(shop_items) == seen:
exit()
def is_sold(item_img) -> bool:
res = cv2.matchTemplate(item_img, sold_out_image, cv2.TM_CCOEFF_NORMED)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
if max_val > 0.8:
return True
return False
# folder = Path(r'D:\BioHazard\Documents\Arknights\信用商店统计\信用商店截图')
# folder = Path(r'C:\Users\Administrator\Documents\MuMu共享文件夹\Screenshots')
# for path in folder.rglob('*.png'):
# image = load_image(path)
# if image.shape != (1080, 1920, 3):
# continue
# for index in range(10):
# item_image = get_item_image(image, index)
# sold = is_sold(item_image)
# if sold:
# continue
# get_item_name(item_image, sold)
# folder = Path(r'D:\BioHazard\Documents\Arknights\信用商店统计\信用商店截图识别结果')
# for path in folder.rglob('*.json'):
# with open(path, 'r', encoding='utf-8') as fp:
# data = json.load(fp)
# credit_store = CreditStore.from_json(data)
# if any(item.name in ('坚雷') and not item.sold for item in credit_store.items):
# print(path)
# image = load_image(r'D:\BioHazard\Documents\Arknights\信用商店统计\信用商店截图\CreditStore-20240210-154119-24-官服-eve#7543.png')
# for index in range(10):
# item_image = get_item_image(image, index)
# sold = is_sold(item_image)
# if sold:
# continue
# get_item_name(item_image, sold)