我有此代码:
import cv2
import numpy as np
import pyautogui
import random
import time
from pymouse import PyMouse
from threading import Thread
from matplotlib import pyplot as plt
#points to image clicker calc
def r(num, rand):
return num + rand*random.random()
def RandomClicking():
m = PyMouse()
while True:
m.click(random.randint(1672, 1901), random.randint(170, 311))
time.sleep(10)
#image clicker
def click_image(image, pos, action, timestamp, offset=5):
img = cv2.imread(image)
height, width, channels = img.shape
pyautogui.moveTo(pos[0] + r(width / 2, offset), pos[1] + r(height / 2,offset), timestamp)
pyautogui.click(button=action)
#image recognition
def imagesearch(image, precision=0.8):
im = pyautogui.screenshot()
#im.save('testarea.png') usefull for debugging purposes, this will save the captured region as "testarea.png"
img_rgb = np.array(im)
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
template = cv2.imread(image, 0)
template.shape[::-1]
res = cv2.matchTemplate(img_gray, template, cv2.TM_CCOEFF_NORMED)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
if max_val < precision:
return [-1,-1]
return max_loc
#continuous image searching on screen
def imagesearch_loop(image, timesample, precision = 0.8):
pos = imagesearch(image, precision)
while pos[0] == -1:
print(image+" not found, waiting")
time.sleep(timesample)
pos = imagesearch(image, precision)
print("image found ", "x:", pos[0], "y:", pos[1])
#MultiTracking.imagesearch_count(image, precision=0.8)7
pos = imagesearch("Untitled.png", 0.8)
if pos[0] != -1:
click_image("Untitled.png", pos, "left", 0.0, offset=5)
imagesearch_loop("Untitled.png", 0.8)
return pos
#finds the occurrences
def imagesearch_count(image, precision=0.8):
img_rgb = pyautogui.screenshot()
img_rgb = np.array(img_rgb)
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
template = cv2.imread(image, 0)
w, h = template.shape[::-1]
res = cv2.matchTemplate(img_gray, template, cv2.TM_CCOEFF_NORMED)
loc = np.where(res >= precision)
count = 0
for pt in zip(*loc[::-1]):
cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0, 0, 255), 2)
count = count + 1
cv2.imwrite('result.png', img_rgb)
return count
if __name__ == '__main__':
Thread(target = RandomClicking).start()
Thread(target = imagesearch_count("Untitled.png", precision=0.9))
imagesearch_loop("Untitled.png", 0.5, 0.8)
此代码是用于浏览器游戏的机器人。该机器人工作正常,但检测到我多次给出的图像时出现问题。这是有关问题https://1drv.ms/u/s!Am7NObcyZiIghIhSlXxzqM2zTU7cOg的视频。正如您将在视频中看到的那样,它发现的不仅仅是疯狂的情况,而且它会在飞船收集前一个盒子之前单击每个图像。几秒钟后,它成功收集到一个盒子上,但其他盒子仍然存在问题。
有什么办法解决这个问题?我有一个imagesearch_count
函数,用于查找出现的事件。我正在考虑制作诸如单击优先级队列之类的内容,但不知道如何创建此类内容。
P.S。抱歉视频太慢了,但是我是从VMbox录制的。