基于python+opencv+pyautogui的图像识别点击
生活随笔
收集整理的這篇文章主要介紹了
基于python+opencv+pyautogui的图像识别点击
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
第一次發文沒啥經驗 很多都是借鑒若有侵權請告知 必更改 開源代碼只供學習交流請勿用作其他用途!!!
get_img()#這個函數是獲取rgb格式屏幕截圖,可用于截圖一次識別多個元素
imgclick()#主要的識別圖片點擊函數,只選取一個點點擊,可調節xy軸偏移值(識別圖片a位置點擊另一b位置)默認在一定范圍內隨機點擊(可自行調節范圍)圖片識別率(若發現有很多誤識別請調節識別率1為百分百匹配默認0.8)
imgcheck()#檢測圖片返回bgr數值,可用于判斷按鈕按下的顏色變化
下面是代碼區
pip install pyautogui
pip install python opencv
import random import pyautogui import time import cv2 import numpy as np from random import randrangedef py_nms(dets, thresh):"""Pure Python NMS baseline."""# x1、y1、x2、y2、以及score賦值# (x1、y1)(x2、y2)為box的左上和右下角標x1 = dets[:, 0]y1 = dets[:, 1]x2 = dets[:, 2]y2 = dets[:, 3]scores = dets[:, 4]# 每一個候選框的面積areas = (x2 - x1 + 1) * (y2 - y1 + 1)# order是按照score降序排序的order = scores.argsort()[::-1]# print("order:",order)keep = []while order.size > 0:i = order[0]keep.append(i)# 計算當前概率最大矩形框與其他矩形框的相交框的坐標,會用到numpy的broadcast機制,得到的是向量xx1 = np.maximum(x1[i], x1[order[1:]])yy1 = np.maximum(y1[i], y1[order[1:]])xx2 = np.minimum(x2[i], x2[order[1:]])yy2 = np.minimum(y2[i], y2[order[1:]])# 計算相交框的面積,注意矩形框不相交時w或h算出來會是負數,用0代替w = np.maximum(0.0, xx2 - xx1 + 1)h = np.maximum(0.0, yy2 - yy1 + 1)inter = w * h# 計算重疊度IOU:重疊面積/(面積1+面積2-重疊面積)ovr = inter / (areas[i] + areas[order[1:]] - inter)# 找到重疊度不高于閾值的矩形框索引inds = np.where(ovr <= thresh)[0]# print("inds:",inds)# 將order序列更新,由于前面得到的矩形框索引要比矩形框在原order序列中的索引小1,所以要把這個1加回來order = order[inds + 1]return keepdef template(img_gray, template_img, template_threshold):'''img_gray:待檢測的灰度圖片格式template_img:模板小圖,也是灰度化了template_threshold:模板匹配的置信度'''h, w = template_img.shape[:2]res = cv2.matchTemplate(img_gray, template_img, cv2.TM_CCOEFF_NORMED)#start_time = time.time()loc = np.where(res >= template_threshold) # 大于模板閾值的目標坐標score = res[res >= template_threshold] # 大于模板閾值的目標置信度# 將模板數據坐標進行處理成左上角、右下角的格式xmin = np.array(loc[1])ymin = np.array(loc[0])xmax = xmin + wymax = ymin + hxmin = xmin.reshape(-1, 1) # 變成n行1列維度xmax = xmax.reshape(-1, 1) # 變成n行1列維度ymax = ymax.reshape(-1, 1) # 變成n行1列維度ymin = ymin.reshape(-1, 1) # 變成n行1列維度score = score.reshape(-1, 1) # 變成n行1列維度data_hlist = []data_hlist.append(xmin)data_hlist.append(ymin)data_hlist.append(xmax)data_hlist.append(ymax)data_hlist.append(score)data_hstack = np.hstack(data_hlist) # 將xmin、ymin、xmax、yamx、scores按照列進行拼接thresh = 0.3 # NMS里面的IOU交互比閾值keep_dets = py_nms(data_hstack, thresh)#print("nms time:", time.time() - start_time) # 打印數據處理到nms運行時間dets = data_hstack[keep_dets] # 最終的nms獲得的矩形框return detsdef get_img():# 獲取屏幕截圖pyautogui.screenshot("sc.png")#以灰度加載圖片img_rgb = cv2.imread("sc.png")return img_rgbdef get_imggray(img_rgb):img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)return img_graydef imgclick(t_img,template_address,shift_x = 0,shift_y = 0,random_number = 20,template_shreshold = 0.8):'''img待檢測圖片template_address模版圖地址shift_x橫坐標偏移shift_y縱坐標偏移0random坐標隨機數范圍-random,randomtemplate_shreshold模版識別率'''# 模板相似度temp_shreshold = template_shresholdimg_template = cv2.imread(template_address, 0)img_rgb = t_imgimg_gray = get_imggray(img_rgb)r = random.uniform(-random_number, random_number)tim = random.uniform(0,0.1)result = 0try:dets = template(img_gray, img_template, temp_shreshold)for coord in dets:#print((int(coord[0])+(int(coord[2])))/2,(int(coord[1])+(int(coord[3])))/2)np.any(dets >= 0)x = (int(coord[0]) + (int(coord[2]))) / 2y = (int(coord[1]) + (int(coord[3]))) / 2# X軸偏移加隨機sx = shift_x + r# Y軸偏移加隨機sy = shift_y + rnx = x + sxny = y + sypyautogui.moveTo(nx, ny, tim)time.sleep(tim)pyautogui.mouseDown(nx, ny)time.sleep(tim)pyautogui.mouseUp(nx, ny)st = template_address.strip('.png').strip('/img')print("在", (x,y), "檢測到",st,"已點擊", ('%.1f' % nx, '%.1f' % ny))result = 1#time.sleep(0.1)#識別區畫紅框#cv2.rectangle(img_rgb, (int(coord[0]), int(coord[1])), (int(coord[2]), int(coord[3])), (0, 0, 255), 2)#隨機取單次結果break#打開測試窗口# cv2.imshow('img_rgb', img_rgb)# cv2.waitKey(0)except:passreturn resultdef imgcheck(t_img,template_address,template_shreshold = 0.8):'''img顏色檢測img待檢測圖片template_address模版圖地址template_shreshold模版識別率返回BGR值'''# 模板相似度temp_shreshold = template_shresholdimg_template = cv2.imread(template_address, 0)img_rgb = t_imgimg_gray = get_imggray(img_rgb)b = 0g = 0r = 0try:dets = template(img_gray, img_template, temp_shreshold)for coord in dets:np.any(dets >= 0)x = int((coord[0] + coord[2]) / 2)y = int((coord[1] + coord[3]) / 2)st = template_address.strip('.png').strip('/img')print("在", (x,y), "檢測到",st)b = img_rgb[y,x,0]g = img_rgb[y,x,1]r = img_rgb[y,x,2]# 隨機取單次結果break#打開測試窗口#cv2.imshow('img_rgb', img_rgb)#cv2.waitKey(0)except:passreturn b,g,rdef mouse_position():#顯示鼠標當前位置坐標while True:x,y = pyautogui.position() #獲取當前鼠標的位置posStr = str(x).rjust(4)+','+str(y).rjust(4)print(posStr)time.sleep(1)以下為使用實例
import autotouch import time def siji():while True:t_img = autotouch.get_img()autotouch.imgclick(t_img,'img/1.png',109,-37)if autotouch.imgcheck(t_img,'img/2.png')[0] == 60:autotouch.imgclick(t_img, 'img/2.png')time.sleep(1)autotouch.imgclick(t_img,'img/3.png', 1000, 0, 20, 0.9)autotouch.imgclick(t_img, 'img/4.png', 560, 40)總結
以上是生活随笔為你收集整理的基于python+opencv+pyautogui的图像识别点击的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 柑橘病虫害识别方案总结
- 下一篇: 435-无重叠区间