Answer the question
In order to leave comments, you need to log in
How to compare arrays in NumPy or do without them?
Good day, wise users of Habr.
I recently came across a video about automating fishing in MMO Albion Online / https://www.youtube.com/watch?v=92XnJ3s0vX8 / also, there is an article on Habré about it. Since I play this game, I decided to write my own script that detects red nicknames (enemies) at the edges of the screen and presses the R button, using the OpenCV and PyAutoGUI libraries, this is such a game area as a gang, but we will not go into details.
Source -
import time
import cv2
import mss
import numpy as np
import pyautogui
template = cv2.imread("red_nick.png", cv2.IMREAD_GRAYSCALE)
w, h = template.shape[::-1]
with mss.mss() as sct:
monitor = {"top": 0, "left": 0, "width": 400, "height": 250}
while "Screen capturing":
last_time = time.time()
img = np.array(sct.grab(monitor))
empty_img = np.array(sct.grab(monitor))
print("fps: {}".format(1 / (time.time() - last_time)))
gray_frame = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
res = cv2.matchTemplate(gray_frame, template, cv2.TM_CCOEFF_NORMED)
loc = np.where(res >= 0.999)
for i in zip(*loc[::-1]):
cv2.rectangle(img, i, (i[0] + w, i[1] + h), (0, 255, 255), 10)
#cv2.imshow("Frame", img)
#cv2.imshow("Frame2", empty_img)
if cv2.waitKey(1) == 27:
cv2.destroyAllWindows()
break
if (img != empty_img).any():
pyautogui.press(keys='r')
cv2.imshow("Frame", img)
cv2.waitKey()
break
if (img != empty_img).any():
pyautogui.press(keys='r')
break
empty_img = np.array(sct.grab(monitor))
empty_img = img,
for i in zip(*loc[::-1]):
cv2.rectangle(img, i, (i[0] + w, i[1] + h), (0, 255, 255), 10)
import time
import cv2
import mss
import numpy as np
import pyautogui
template = cv2.imread("red_nick.png", cv2.IMREAD_GRAYSCALE)
w, h = template.shape[::-1]
with mss.mss() as sct:
monitor = {"top": 0, "left": 0, "width": 400, "height": 250}
while "Screen capturing":
last_time = time.time()
img = np.array(sct.grab(monitor))
empty_img = np.array(sct.grab(monitor))
print("fps: {}".format(1 / (time.time() - last_time)))
gray_frame = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
res = cv2.matchTemplate(gray_frame, template, cv2.TM_CCOEFF_NORMED)
loc = np.where(res >= 0.999)
for i in zip(*loc[::-1]):
cv2.rectangle(img, i, (i[0] + w, i[1] + h), (0, 255, 255), 10)
#cv2.imshow("Frame", img)
#cv2.imshow("Frame2", empty_img)
if cv2.waitKey(1) == 27:
cv2.destroyAllWindows()
break
if (img != empty_img).any():
pyautogui.press(keys='r')
cv2.imshow("Frame", img)
cv2.waitKey()
break
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question