import os
import pyperclip
import pyautogui
from keyboard import is_pressed
from time import sleep
import cv2
def accRecog(recogImgPath, do=pyautogui.click, method=cv2.TM_CCOEFF_NORMED,debug=0):
'''recogImgPath :需要匹配的小图 如果显示None erorr 路径改为ascii
do :pyautogui.moveTo()# 基本移动
pyautogui.click() # 左键单击
pyautogui.doubleClick() # 左键双击
pyautogui.rightClick() # 右键单击
pyautogui.middleClick() # 中键单击
method:识别算法
cv2.TM_CCOEFF 最有可能匹配最亮的像素 相关系数
cv2.TM_CCOEFF_NORMED 通用情况
cv2.TM_CCORR 最有可能匹配最亮的像素 互相关
cv2.TM_CCORR_NORMED 通用情况
cv2.TM_SQDIFF 最有可能匹配最暗的像素
cv2.TM_SQDIFF_NORMED 通用情况
debug :在识别位置画出矩形用于调试
resize showRect时显示图像的缩放大小'''
imgBigPath='imgBig.png'
pyautogui.screenshot(imgBigPath)
imgBig = cv2.imread(imgBigPath,cv2.IMREAD_GRAYSCALE)
imgSmall = cv2.imread(recogImgPath,cv2.IMREAD_GRAYSCALE)
w, h = imgSmall.shape[::-1]
def rocog(imgBig,imgSmall,method):
res = cv2.matchTemplate(imgBig,imgSmall,method)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
if method in [cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED]:top_left = min_loc
else:top_left = max_loc
y = top_left[1]
x = top_left[0]
return x,y
print(top_left,bottom_right)
if not debug:
x,y=rocog(imgBig,imgSmall,method)
x=int(x+w//2)
y=int(y+h//2)
print(x,y)
do(x,y)
else:
from matplotlib import pyplot as plt
methods = ['cv2.TM_CCOEFF', 'cv2.TM_CCOEFF_NORMED', 'cv2.TM_CCORR','cv2.TM_CCORR_NORMED', 'cv2.TM_SQDIFF', 'cv2.TM_SQDIFF_NORMED']
imgBigColor = cv2.imread(imgBigPath,cv2.IMREAD_COLOR)
imgBigColor = cv2.cvtColor(imgBigColor, cv2.COLOR_BGR2RGB)
plt.rcParams['figure.figsize']=(60,40)
for i,meth in enumerate(methods):
method = eval(meth)
x,y=rocog(imgBig,imgSmall,method)
cv2.rectangle(imgBigColor,(x,y), (x+w,y+h), (0,0,255), 2)
plt.axis('off')
plt.subplot(3, 2, i+1)
plt.imshow(imgBigColor)
plt.title(meth)
plt.xticks([])
plt.yticks([])
plt.suptitle(f"Algorithm Comparison")
plt.savefig(f"AlgorithmComparison.png")
plt.show()
os.remove(imgBigPath)
def imgDetect(ifile):
confidence=confidenceCeiling
while True:
pos=pyautogui.locateCenterOnScreen(ifile,confidence=confidence)
if pos:return pos
confidence-=0.02
if confidence<=confidenceFloor:
print(f'未检测到{ifile}')
break
def tillClick(*ifiles):
confidence=confidenceCeiling
while True:
breakout=0
for ifile in ifiles:
pos=pyautogui.locateCenterOnScreen(ifile,confidence=confidence)
if pos:
pyautogui.click(pos)
breakout=1
break
if breakout:break
confidence-=0.01
if confidence<=confidenceFloor:
print(f'无法识别{ifile}正在重试')
confidence=confidenceCeiling
def tillHotkey(ifile,*hotkeys):
confidence=confidenceCeiling
while True:
pos=pyautogui.locateCenterOnScreen(ifile,confidence=confidence)
if pos:
pyautogui.hotkey(*hotkeys)
break
confidence-=0.01
if confidence<=confidenceFloor:
print(f'无法识别{ifile}正在重试')
confidence=confidenceCeiling
def inputText(text):
pyperclip.copy(text)
pyautogui.hotkey('ctrl','v')
def enterText(text):
inputText(text)
pyautogui.press('enter')
def scroll(d):
pyautogui.scroll(-d*75)
def typeChar(char,count):
pyautogui.typewrite([char for n in range(count)])
def turnTo(url):
tillHotkey(r'edge\address.jpg','alt','d')
enterText(url)
def openE():
pyautogui.hotkey('shift','alt','s')
enterText('microsoft edge')
def chooseFile(path):
tillClick(r'openFile.jpg')
enterText(path)
def openFile(path):
pyautogui.hotkey('shift','alt','s')
enterText(path)
def getText(path):
with open(path,'r',encoding='utf-8') as f:
text=f.read()
return text
def getF(file,lv):
f=[]
def recF(file,lv):
if lv:
file=os.path.dirname(file)
lv-=1
recF(file,lv)
f.append(file)
if lv:
recF(file,lv)
return file.replace(min(f)+'\\','')
elif not lv:return file
confidenceCeiling=0.8
confidenceFloor=0.5
fontStyle='C:/Users/TRIX/AppData/Local/Microsoft/Windows/Fonts/HarmonyOS_Sans_SC_Bold.ttf'
pyautogui.FAILSAFE=True
pyautogui.PAUSE=0.3
- 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