-
Notifications
You must be signed in to change notification settings - Fork 91
/
mdt_cv.py
208 lines (188 loc) · 5.38 KB
/
mdt_cv.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
import contextlib
import os
import sys
from ctypes import windll
from json import loads
import dhash
import win32gui
import win32ui
from PIL import Image
import cv2
import numpy as np
cid_show_gui = 0
n_flags = 3
with contextlib.suppress(Exception):
_win_v = sys.getwindowsversion()
if _win_v.major == 6 and _win_v.minor == 1:
n_flags = 1
windll.user32.SetProcessDPIAware()
BOXES = (
(
(55, 159, 126, 231), # Deck edit
(40, 178, 117, 255), # Live duel
(125, 192, 349, 414), # Card detail
(73, 172, 146, 245), # Opponent Deck
), # 1280x720
(
(58, 170, 135, 246),
(43, 190, 125, 272),
(134, 205, 373, 442),
(79, 183, 156, 260),
), # 1366x768
(
(61, 179, 142, 259),
(45, 201, 131, 287),
(141, 216, 392, 466),
(83, 194, 164, 275),
), # 1440x810
(
(68, 199, 158, 288),
(50, 223, 146, 319),
(156, 240, 437, 518),
(93, 215, 182, 305),
), # 1600x900
(
(82, 239, 189, 346),
(60, 268, 176, 383),
(187, 288, 524, 622),
(111, 258, 218, 366),
), # 1920x1080
(
(87, 255, 202, 369),
(64, 286, 187, 409),
(200, 307, 559, 663),
(119, 275, 233, 391),
), # 2048x1152
(
(109, 318, 252, 461),
(80, 357, 235, 511),
(250, 384, 698, 829),
(148, 344, 292, 488),
), # 2560x1440
(
(136, 398, 316, 576),
(100, 446, 293, 639),
(312, 480, 874, 1036),
(186, 430, 365, 610),
), # 3200x1800
(
(164, 478, 378, 692),
(120, 535, 352, 767),
(374, 576, 1048, 1244),
(224, 517, 438, 732),
), # 3840x2160
)
last_hash = [0] * 4
art_hash = {}
with open("./data/hash.json", "r", encoding="utf-8") as f:
art_hash = loads(f.read())
def screenshot():
global n_flags
if hwnd := win32gui.FindWindow(None, "masterduel"):
box = win32gui.GetClientRect(hwnd)
box_w = box[2] - box[0]
box_h = box[3] - box[1]
hwnd_dc = win32gui.GetWindowDC(hwnd)
mfc_dc = win32ui.CreateDCFromHandle(hwnd_dc)
save_dc = mfc_dc.CreateCompatibleDC()
save_bitmap = win32ui.CreateBitmap()
save_bitmap.CreateCompatibleBitmap(mfc_dc, box_w, box_h) # 10%
save_dc.SelectObject(save_bitmap)
result = windll.user32.PrintWindow(hwnd, save_dc.GetSafeHdc(), n_flags) # 58%
bmpinfo = save_bitmap.GetInfo()
bmpstr = save_bitmap.GetBitmapBits(True) # 19%
im = Image.frombuffer(
"RGB",
(bmpinfo["bmWidth"], bmpinfo["bmHeight"]),
bmpstr,
"raw",
"BGRX",
0,
1,
) # 12%
win32gui.DeleteObject(save_bitmap.GetHandle())
save_dc.DeleteDC()
mfc_dc.DeleteDC()
win32gui.ReleaseDC(hwnd, hwnd_dc)
if result != 0:
return im
return None
def get_scan():
global last_hash
full_img = screenshot()
if not full_img:
return -1
imgx, imgy = full_img.size
if imgx < 1280:
return -1
elif imgx < 1366:
resolution = 0
elif imgx < 1440:
resolution = 1
elif imgx < 1600:
resolution = 2
elif imgx < 1920:
resolution = 3
elif imgx < 2048:
resolution = 4
elif imgx < 2560:
resolution = 5
elif imgx < 3200:
resolution = 6
elif imgx < 3840:
resolution = 7
else:
resolution = 8
results = []
for i in range(4):
_sample = full_img.crop(BOXES[resolution][i])
_hash = dhash.dhash_int(_sample, 16)
if dhash.get_num_bits_different(_hash, last_hash[i]) > 10:
hash_compare = [
(dhash.get_num_bits_different(_hash, item[0]), item[1])
for item in art_hash
]
results.append(min(hash_compare, key=lambda xx: xx[0]))
last_hash[i] = _hash
if results:
result = min(results, key=lambda xx: xx[0])
if result[0] < 60:
return result[1]
return 0
def get_search_button_postion():
return position_by_template_matching("search")
def get_reset_button_postion():
return position_by_template_matching("reset")
def get_scale(): # sourcery skip: inline-immediately-returned-variable
# 窗口分辨率可能在程序运行时改变,所以不能是静态变量
hwnd = win32gui.FindWindow(None, "masterduel")
box = win32gui.GetClientRect(hwnd)
box_w = box[2] - box[0]
scale = box_w / 1920
return scale
def position_by_template_matching(template_name):
# 截屏
full_img = cv2.cvtColor(np.array(screenshot()), cv2.COLOR_RGB2BGR)
# 高, 宽
imgx, imgy, _ = full_img.shape
# 按钮模板
template_image = cv2.imread(f"./data/template/1920x1080{template_name}.png")
template_image = cv2.resize(
template_image,
(
int(get_scale() * template_image.shape[1]),
int(get_scale() * template_image.shape[0]),
),
)
# 模板匹配
result = cv2.matchTemplate(full_img, template_image, cv2.TM_CCOEFF_NORMED)
# 先横后竖
_, _, _, top_left = cv2.minMaxLoc(result)
return int(top_left[0] + template_image.shape[1] / 2), int(
top_left[1] + template_image.shape[0] / 2
)
def main():
print(get_scan())
os.system("pause")
if __name__ == "__main__":
main()