赞
踩
#利用下列函数方便实现自动化操作 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)# 读入背景图片 cv2.IMREAD_GRAYSCALE:读入灰度图片 imgSmall = cv2.imread(recogImgPath,cv2.IMREAD_GRAYSCALE)# 读入需要查找的图片 w, h = imgSmall.shape[::-1]# 得到图片的高和宽 def rocog(imgBig,imgSmall,method):#以method算法返回imgSmall在imgBigGray的左上位置x,y res = cv2.matchTemplate(imgBig,imgSmall,method)# 模板匹配操作 应用模板算法,返回一系列指标 # 注意 TM_SQDIFF 或者 TM_SQDIFF_NORMED 算法使用最小值为最优 min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)# 从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) #cv2.IMREAD_COLOR:默认参数,读入一副彩色图片,忽略alpha通道 imgBigColor = cv2.cvtColor(imgBigColor, cv2.COLOR_BGR2RGB) # 颜色转换 plt.rcParams['figure.figsize']=(60,40)#6000x4000 for i,meth in enumerate(methods):# 依次使用算法匹配 #img = img2.copy() method = eval(meth) x,y=rocog(imgBig,imgSmall,method) cv2.rectangle(imgBigColor,(x,y), (x+w,y+h), (0,0,255), 2) # 画出矩形框 rgb, 线宽 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)#识别 confidence识别精度0-1 pos=x,y if pos:return pos confidence-=0.02 if confidence<=confidenceFloor: print(f'未检测到{ifile}') break def tillClick(*ifiles):#直到识别出才点击后停止 ifile 如果引起错误 多个fileNotfound图片先把路径改为英文字符 confidence=confidenceCeiling while True: breakout=0 for ifile in ifiles: pos=pyautogui.locateCenterOnScreen(ifile,confidence=confidence)#识别 confidence识别精度0-1 pos=x,y if pos: pyautogui.click(pos) breakout=1 break if breakout:break#jump out loop confidence-=0.01 if confidence<=confidenceFloor: print(f'无法识别{ifile}正在重试') confidence=confidenceCeiling def tillHotkey(ifile,*hotkeys):#直到识别出才热键后停止 ifile 如果引起fileNotfound错误先把路径改为英文字符 confidence=confidenceCeiling while True: pos=pyautogui.locateCenterOnScreen(ifile,confidence=confidence)#识别 confidence识别精度0-1 pos=x,y 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):#滚动 -d向上 d=1=↓x1 pyautogui.scroll(-d*75) def typeChar(char,count): pyautogui.typewrite([char for n in range(count)])#按照列表中的每个键名 进行按键 sec为按键间隔 def turnTo(url):#转到该网页 tillHotkey(r'edge\address.jpg','alt','d') enterText(url) def openE():#打开egde pyautogui.hotkey('shift','alt','s')#快捷键 enterText('microsoft edge') def chooseFile(path):#文件选择对话框 tillClick(r'openFile.jpg') enterText(path) def openFile(path):#listary打开列表中第一个文件 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):#返回指定迭代目录级 0为原目录 1为basename 2为dirname(file)+basename etc 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#图片识别精度下限 过高识别不出 过低会错误识别 建议直接使用 accRecog fontStyle='C:/Users/TRIX/AppData/Local/Microsoft/Windows/Fonts/HarmonyOS_Sans_SC_Bold.ttf' pyautogui.FAILSAFE=True#鼠标移动到屏幕左上角停止程序 pyautogui.PAUSE=0.3# pyautogui指令执行间隔 间隔太小执行过快会无法正常响应
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。