-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.py
93 lines (72 loc) · 2.54 KB
/
action.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
import numpy,time,sys
import cv2,time,random,os, datetime
#在背景查找目标图片,并返回查找到的结果坐标列表,target是背景,want是要找目标
def locate(target,want, show=bool(0), msg=bool(0)):
loc_pos=[]
want,treshold,c_name=want[0],want[1],want[2]
result=cv2.matchTemplate(target,want,cv2.TM_CCOEFF_NORMED)
location=numpy.where(result>=treshold)
if msg: #显示正式寻找目标名称,调试时开启
print(c_name,'searching... ')
h,w=want.shape[:-1] #want.shape[:-1]
n,ex,ey=1,0,0
for pt in zip(*location[::-1]): #其实这里经常是空的
x,y=pt[0]+int(w/2),pt[1]+int(h/2)
if (x-ex)+(y-ey)<15: #去掉邻近重复的点
continue
ex,ey=x,y
cv2.circle(target,(x,y),10,(0,0,255),3)
if msg:
print(c_name,'we find it !!! ,at',x,y)
x,y=int(x),int(y)
loc_pos.append([x,y])
if show: #在图上显示寻找的结果,调试时开启
print('Debug: show action.locate')
cv2.imshow('we get',target)
cv2.waitKey(2300)
cv2.destroyAllWindows()
if len(loc_pos)==0:
print(c_name,'not find')
else:
print("Got it, guys!")
return loc_pos
#按【文件内容,匹配精度,名称】格式批量聚聚要查找的目标图片,精度统一为0.95,名称为文件名
def load_imgs():
mubiao = {}
path = os.getcwd() + '\png2'
file_list = os.listdir(path)
for file in file_list:
name = file.split('.')[0]
file_path = path + '\\' + file
print(file_path)
a = [cv2.imread(file_path), 0.95, name]
mubiao[name] = a
print(a)
return mubiao
#蜂鸣报警器,参数n为鸣叫次数
def alarm(n):
frequency = 1500
duration = 500
if os.name=='nt':
import winsound
winsound.Beep(frequency, duration)
#裁剪图片以缩小匹配范围,screen为原图内容,upleft、downright是目标区域的左上角、右下角坐标
def cut(screen,upleft,downright):
a,b=upleft
c,d=downright
screen=screen[b:d,a:c]
return screen
#随机偏移坐标,防止游戏的外挂检测。p是原坐标,w、n是目标图像宽高,返回目标范围内的一个随机坐标
def cheat(p, w, h):
a,b = p
w, h = int(w/3), int(h/3)
c,d = random.randint(-w, w),random.randint(-h, h)
e,f = a + c, b + d
y = [e, f]
return(y)
def randomnumber():
number = random.randint(1,9)
return number
def randtimis():
times = random.randint(1,2)
return times