-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathYuHunModule.py
256 lines (224 loc) · 8.98 KB
/
YuHunModule.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
# -*- coding: utf-8 -*-
import datetime
import logging
import os
import random
import time
from tkinter import END
import cv2
import numpy
import numpy as np
import pyautogui
from PIL import ImageGrab
from matplotlib import pyplot as plt
pyautogui.FAILSAFE = False
# 初始化SIFT探测器,老版本写法:SIFT = cv2.xfeatures2d.SIFT_create()
SIFT = cv2.SIFT_create()
def ComputeScreenShot(screenShot):
"""
由于屏幕分辨率高,计算耗时,这里优化一下
:return:
"""
kp2, des2 = SIFT.detectAndCompute(screenShot, None)
return kp2, des2
def GetLocation(target, kp2, des2):
"""
获取目标图像在截图中的位置
:param target: 检测目标
:return: 返回坐标(x,y) 与opencv坐标系对应
"""
MIN_MATCH_COUNT = 10
img1 = target # cv2.cvtColor(target,cv2.COLOR_BGR2GRAY)# 查询图片
# img2 = screenShot
# img2 = cv2.cvtColor(screenShot, cv2.COLOR_BGR2GRAY) # 训练图片
# img2 = cv2.resize(img2, dsize=None, fx=0.5, fy=0.5, interpolation=cv2.INTER_NEAREST)
# 用SIFT找到关键点和描述符
kp1, des1 = SIFT.detectAndCompute(img1, None)
FLANN_INDEX_KDTREE = 0
index_params = dict(algorithm=FLANN_INDEX_KDTREE, trees=4)
search_params = dict(checks=50)
flann = cv2.FlannBasedMatcher(index_params, search_params)
matches = flann.knnMatch(des1, des2, k=2)
good = []
for m, n in matches:
if m.distance < 0.7 * n.distance:
good.append(m)
if len(good) > MIN_MATCH_COUNT:
src_pts = np.float32([kp1[m.queryIdx].pt for m in good]).reshape(-1, 1, 2)
dst_pts = np.float32([kp2[m.trainIdx].pt for m in good]).reshape(-1, 1, 2)
M, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0)
matchesMask = mask.ravel().tolist()
h, w = img1.shape
pts = np.float32([[0, 0], [0, h - 1], [w - 1, h - 1], [w - 1, 0]]).reshape(-1, 1, 2)
if M is not None:
dst = cv2.perspectiveTransform(pts, M)
arr = np.int32(dst) #
midPosArr = arr[0] + (arr[2] - arr[0]) // 2
midPos = (midPosArr[0][0], midPosArr[0][1])
# test
# screen = ImageGrab.grab()
# img2 = cv2.cvtColor(numpy.asarray(screen), cv2.COLOR_RGB2BGR)
# show=cv2.circle(img2,midPos,30,(255,255,255),thickness=5)
# cv2.imshow('s',show)
# cv2.waitKey()
# cv2.destroyAllWindows()
return midPos
else:
return None
else:
return None
def CheatPos(originPos, factor=5):
"""
对原始点击坐标进行随机偏移,防止封号
:param originPos:原始坐标
:param factor:最大随机偏移
:return:
"""
x, y = random.randint(-factor, factor), random.randint(-factor, factor)
newPos = (originPos[0] + x, originPos[1] + y)
return newPos
def Click(targetPosition):
"""
点击屏幕上的某个点
:param targetPosition: 要点击的目标点
"""
if targetPosition is None:
print('未检测到目标')
else:
pyautogui.moveTo(targetPosition, duration=0.20)
pyautogui.click()
time.sleep(random.randint(100, 300) / 1000)
# time.sleep(random.randint(100, 150) / 1000)
def LoadImgs():
"""
加载所有需要检测的目标图像
:return:
"""
obj = {}
path = os.getcwd() + '/img/yuhun'
file_list = os.listdir(path)
for file in file_list:
name = file.split('.')[0]
file_path = path + '/' + file
a = cv2.imread(file_path, 0)
obj[name] = a
return obj
def GetScreenShot():
"""
获取屏幕截图
:return:
"""
screen = ImageGrab.grab()
# screen.save('screen.jpg')
# screen = cv2.imread('screen.jpg')
screen = cv2.cvtColor(numpy.asarray(screen), cv2.COLOR_RGB2BGR)
return screen
class YuHun():
def __init__(self):
self._flag = False
self.NeedCloseGame = False
self.NeedCloseSystem = False
self.PlayerNumber = 2
def Run(self, LogUI, NeedCloseGame, NeedCloseSystem, PlayerNumber):
imgs = LoadImgs()
tips = {}
self.PlayerNumber = PlayerNumber
tips['end1'] = "胜利界面"
tips['end2'] = "奖励达摩"
tips['tiaozhan'] = "挑战按钮"
tips['reject'] = "协作拒绝按钮"
tips['invite'] = "组队邀请按钮"
while self._flag is not True:
screen = GetScreenShot()
# WindowShape = screen.shape
#result = []
resultobj ={}
# 为了优化速度,把计算屏幕截图的特征提取出来,避免重复运算
kp2, des2 = ComputeScreenShot(screen)
# 乱移动一下鼠标,防止有时候队长先进入组队页面后鼠标停留在挑战处导致无法识别
# pyautogui.moveTo((random.randint(100, 1000),random.randint(100, 1000)), duration=0.15)
for i in ['end1', 'end2', 'reject','tiaozhan','invite']:
print("图像识别判定中,寻找:"+tips[i])
obj = imgs[i]
pos = GetLocation(obj, kp2, des2)
if pos is not None:
if i == 'end1':
time.sleep(random.randint(80, 200) / 1000)
pos = CheatPos(pos, 10)
resultobj['end1']=pos
elif i == 'end2':
newPos = (pos[0] + 80, pos[1] + 80)
pos = CheatPos(newPos, 10)
resultobj['end2']=pos
elif i == 'invite':
pos = CheatPos(pos, 20)
resultobj['invite']=pos
elif i == 'tiaozhan':
resultobj['tiaozhan']=CheatPos(pos,15)
elif i == 'reject':
pos = CheatPos(pos, 3)
resultobj['reject']=pos
else:
resultobj[i] = None
# 检查结果
print('yuhun player number:', self.PlayerNumber)
if resultobj['reject'] is not None:
print("关闭邀请")
Click(resultobj['reject'])
if resultobj['end1'] is not None:
print("点击胜利界面")
Click(resultobj['end1'])
if resultobj['end2'] is not None:
print("点击胜利达摩")
Click(resultobj['end2'])
elif resultobj['end1'] is not None:
print("点击胜利界面")
Click(resultobj['end1'])
if resultobj['end2'] is not None:
print("点击胜利达摩")
Click(resultobj['end2'])
elif resultobj['end2'] is not None:
print("点击胜利达摩")
Click(resultobj['end2'])
if resultobj['end1'] is not None:
print("点击胜利界面")
Click(resultobj['end1'])
elif self.PlayerNumber is 3 and resultobj['invite'] is not None:
print("三人队有队友未加入,等待一会")
time.sleep(0.5)
if resultobj['end1'] is not None:
print("点击胜利界面")
Click(resultobj['end1'])
if resultobj['end2'] is not None:
print("点击胜利达摩")
Click(resultobj['end2'])
elif resultobj['tiaozhan'] is not None:
print("点击挑战")
Click(resultobj['tiaozhan'])
time.sleep(10)
# for i in resultobj:
# if i is not None:
# if i=='tiaozhan':
# if resultobj[i] is not None and 'end1' in resultobj and resultobj['end1'] is not None:
# print("点击胜利界面")
# Click(resultobj['end1'])
# elif resultobj[i] is not None and 'end2' in resultobj and resultobj['end2'] is not None:
# print("点击胜利达摩")
# Click(resultobj['end2'])
# elif resultobj[i] is not None and 'invite' in resultobj and self.ThreeParter is True:
# print("三人队有队友未加入,等待一会")
# time.sleep(1.5)
# if resultobj[i] is not None:
# print("点击挑战按钮")
# Click(resultobj[i])
# print("进入挑战,等待一段时间再进行图像识别")
# time.sleep(10)
# else:
# if resultobj[i] is not None:
# print("点击"+tips[i])
# Click(resultobj[i])
# time.sleep(random.randint(1500, 1800) / 1000)
def Terminate(self):
self._flag = True
if __name__ == '__main__':
pass