Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cv2
- import numpy as np
- from matplotlib import pyplot as plt
- import mss
- import mss
- import mss.tools
- from PIL import Image
- import time
- import sys
- import pyautogui
- import threading
- import math
- import uinput
- from pymouse import PyMouse
- def sim_score(min_val, max_val, method):
- if (method == cv2.TM_SQDIFF_NORMED):
- if(1 - min_val) <= 1:
- return(1-min_val)
- else:
- return(0)
- if (method == cv2.TM_SQDIFF):
- if(min_val <= 1):
- return(min_val)
- else:
- return(0)
- else:
- if max_val <= 1:
- return(max_val)
- else:
- return(0)
- def check_tabela():
- while(1):
- try:
- tabelalocation = pyautogui.locateOnScreen('images/retry.png', confidence=0.90)
- tabelapoint = pyautogui.center(tabelalocation)
- tabelax, tabelay = tabelapoint
- pyautogui.click(tabelax, tabelay)
- print("tabelaya tıklandı")
- except:
- print("tabela bulunamadı")
- print("test")
- time.sleep(5)
- m = PyMouse()
- template = cv2.imread('images/cene.png', 0)
- tmp1 = cv2.imread('images/cene1.png', 0)
- tmp2 = cv2.imread('images/cene2.png', 0)
- tmp3 = cv2.imread('images/cene3.png', 0)
- tmp4 = cv2.imread('images/cene4.png', 0)
- tmp5 = cv2.imread('images/1.png', 0)
- w, h = template.shape[::-1]
- w2, h2 = tmp5.shape[::-1]
- method = eval('cv2.TM_SQDIFF_NORMED')
- with mss.mss() as sct:
- monitor = {"top": 184, "left":772 , "width": 500, "height": 500}
- x = 0
- starttime = time.time()
- y= threading.Thread(target=check_tabela)
- y.start()
- while (1):
- last_time = time.time()
- img = cv2.cvtColor(np.array(sct.grab(monitor)), cv2.COLOR_BGR2GRAY)
- res0 = cv2.matchTemplate(img, template, method)
- min_val0, max_val0, min_loc0, max_loc0 = cv2.minMaxLoc(res0)
- res1 = cv2.matchTemplate(img, tmp1, method)
- min_val1, max_val1, min_loc1, max_loc1 = cv2.minMaxLoc(res1)
- res2 = cv2.matchTemplate(img, tmp2, method)
- min_val2, max_val2, min_loc2, max_loc2 = cv2.minMaxLoc(res2)
- res3 = cv2.matchTemplate(img, tmp3, method)
- min_val3, max_val3, min_loc3, max_loc3 = cv2.minMaxLoc(res3)
- res4 = cv2.matchTemplate(img, tmp4, method)
- min_val4, max_val4, min_loc4, max_loc4 = cv2.minMaxLoc(res4)
- res5 = cv2.matchTemplate(img, tmp5, cv2.TM_CCORR_NORMED)
- min_val5, max_val5, min_loc5, max_loc5 = cv2.minMaxLoc(res5)
- sınır = 0.90
- debug = 1
- sim0 = sim_score(min_val0, max_val0, method)
- sim0 = int(str(sim0)[2:9] + "0")
- sim1 = sim_score(min_val1, max_val1, method)
- sim1 = int(str(sim1)[2:9] + "1")
- sim2 = sim_score(min_val2, max_val2, method)
- sim2 = int(str(sim2)[2:9] + "2")
- sim3 = sim_score(min_val3, max_val3, method)
- sim3 = int(str(sim3)[2:9] + "3")
- sim4 = sim_score(min_val4, max_val4, method)
- sim4 = int(str(sim4)[2:9] + "4")
- sims = [sim0, sim1, sim2, sim3, sim4]
- similarity = max(sims)
- str_similarity = str(similarity)
- t_min_loc = eval("min_loc" + str_similarity[len(str_similarity)-1])
- t_max_loc = eval("max_loc" + str_similarity[len(str_similarity)-1])
- top_left = t_min_loc
- button_top_left = max_loc5
- relx = 0
- if float("0." + str(similarity)) > sınır:
- relx = (top_left[0] - button_top_left[0]) * -1
- print(float("0." + str(similarity)))
- if max_val5 > sınır:
- m.move(button_top_left[0] + int(w2/2) + 772 + relx, button_top_left[1] + int(h2/2) + 184)
- #pyautogui.moveTo(button_top_left[0] + w2/2 + 772, button_top_left[1] + h2/2 + 184)
- #win32api.SetCursorPos((button_top_left[0] + int(w2/2) + 772, button_top_left[1] + int(h2/2) + 184))
- x = x + 1
- #pyautogui.moveTo((button_top_left[0] + int(w2/2) + 772 + relx, button_top_left[1] + int(h2/2) + 184))
- #cv2.circle(img, (top_left[0] + int(w/2), top_left[1] + int(h/2)), 6, (0, 255, 255), -1)
- #cv2.imshow("OpenCV/Numpy normal", img)
- print("fps: {}".format(1 / (time.time() - last_time)))
- #time.sleep(0.001 - ((time.time() - starttime) % 0.001))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement