Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.29 KB | None | 0 0
  1. import cv2
  2. import numpy as np
  3. from matplotlib import pyplot as plt
  4. import mss
  5. import mss
  6. import mss.tools
  7. from PIL import Image
  8. import time
  9. import sys
  10. import pyautogui
  11. import threading
  12. import math
  13. import uinput
  14. from pymouse import PyMouse
  15.  
  16. def sim_score(min_val, max_val, method):
  17. if (method == cv2.TM_SQDIFF_NORMED):
  18. if(1 - min_val) <= 1:
  19. return(1-min_val)
  20. else:
  21. return(0)
  22.  
  23. if (method == cv2.TM_SQDIFF):
  24. if(min_val <= 1):
  25. return(min_val)
  26. else:
  27. return(0)
  28.  
  29. else:
  30. if max_val <= 1:
  31. return(max_val)
  32. else:
  33. return(0)
  34.  
  35. def check_tabela():
  36.  
  37. while(1):
  38. try:
  39. tabelalocation = pyautogui.locateOnScreen('images/retry.png', confidence=0.90)
  40. tabelapoint = pyautogui.center(tabelalocation)
  41. tabelax, tabelay = tabelapoint
  42. pyautogui.click(tabelax, tabelay)
  43. print("tabelaya tıklandı")
  44. except:
  45. print("tabela bulunamadı")
  46.  
  47. print("test")
  48. time.sleep(5)
  49.  
  50. m = PyMouse()
  51.  
  52. template = cv2.imread('images/cene.png', 0)
  53. tmp1 = cv2.imread('images/cene1.png', 0)
  54. tmp2 = cv2.imread('images/cene2.png', 0)
  55. tmp3 = cv2.imread('images/cene3.png', 0)
  56. tmp4 = cv2.imread('images/cene4.png', 0)
  57. tmp5 = cv2.imread('images/1.png', 0)
  58. w, h = template.shape[::-1]
  59. w2, h2 = tmp5.shape[::-1]
  60. method = eval('cv2.TM_SQDIFF_NORMED')
  61. with mss.mss() as sct:
  62. monitor = {"top": 184, "left":772 , "width": 500, "height": 500}
  63. x = 0
  64. starttime = time.time()
  65. y= threading.Thread(target=check_tabela)
  66. y.start()
  67. while (1):
  68. last_time = time.time()
  69. img = cv2.cvtColor(np.array(sct.grab(monitor)), cv2.COLOR_BGR2GRAY)
  70.  
  71. res0 = cv2.matchTemplate(img, template, method)
  72. min_val0, max_val0, min_loc0, max_loc0 = cv2.minMaxLoc(res0)
  73. res1 = cv2.matchTemplate(img, tmp1, method)
  74. min_val1, max_val1, min_loc1, max_loc1 = cv2.minMaxLoc(res1)
  75. res2 = cv2.matchTemplate(img, tmp2, method)
  76. min_val2, max_val2, min_loc2, max_loc2 = cv2.minMaxLoc(res2)
  77. res3 = cv2.matchTemplate(img, tmp3, method)
  78. min_val3, max_val3, min_loc3, max_loc3 = cv2.minMaxLoc(res3)
  79. res4 = cv2.matchTemplate(img, tmp4, method)
  80. min_val4, max_val4, min_loc4, max_loc4 = cv2.minMaxLoc(res4)
  81. res5 = cv2.matchTemplate(img, tmp5, cv2.TM_CCORR_NORMED)
  82. min_val5, max_val5, min_loc5, max_loc5 = cv2.minMaxLoc(res5)
  83. sınır = 0.90
  84. debug = 1
  85. sim0 = sim_score(min_val0, max_val0, method)
  86. sim0 = int(str(sim0)[2:9] + "0")
  87. sim1 = sim_score(min_val1, max_val1, method)
  88. sim1 = int(str(sim1)[2:9] + "1")
  89. sim2 = sim_score(min_val2, max_val2, method)
  90. sim2 = int(str(sim2)[2:9] + "2")
  91. sim3 = sim_score(min_val3, max_val3, method)
  92. sim3 = int(str(sim3)[2:9] + "3")
  93. sim4 = sim_score(min_val4, max_val4, method)
  94. sim4 = int(str(sim4)[2:9] + "4")
  95. sims = [sim0, sim1, sim2, sim3, sim4]
  96. similarity = max(sims)
  97. str_similarity = str(similarity)
  98. t_min_loc = eval("min_loc" + str_similarity[len(str_similarity)-1])
  99. t_max_loc = eval("max_loc" + str_similarity[len(str_similarity)-1])
  100. top_left = t_min_loc
  101. button_top_left = max_loc5
  102. relx = 0
  103. if float("0." + str(similarity)) > sınır:
  104. relx = (top_left[0] - button_top_left[0]) * -1
  105. print(float("0." + str(similarity)))
  106. if max_val5 > sınır:
  107. m.move(button_top_left[0] + int(w2/2) + 772 + relx, button_top_left[1] + int(h2/2) + 184)
  108. #pyautogui.moveTo(button_top_left[0] + w2/2 + 772, button_top_left[1] + h2/2 + 184)
  109. #win32api.SetCursorPos((button_top_left[0] + int(w2/2) + 772, button_top_left[1] + int(h2/2) + 184))
  110. x = x + 1
  111.  
  112. #pyautogui.moveTo((button_top_left[0] + int(w2/2) + 772 + relx, button_top_left[1] + int(h2/2) + 184))
  113. #cv2.circle(img, (top_left[0] + int(w/2), top_left[1] + int(h/2)), 6, (0, 255, 255), -1)
  114. #cv2.imshow("OpenCV/Numpy normal", img)
  115. print("fps: {}".format(1 / (time.time() - last_time)))
  116.  
  117. #time.sleep(0.001 - ((time.time() - starttime) % 0.001))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement