screen_search.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import cv2
  2. import numpy as np
  3. import pyautogui
  4. import random
  5. import time
  6. class Search:
  7. '''
  8. input :
  9. image : path to the image file (see opencv imread for supported types)
  10. precision : the higher, the lesser tolerant and fewer false positives are found default is 0.8
  11. debugg: if true its will save the captured region and write output image with boxes drawn around occurances dufault is False
  12. '''
  13. def __init__(self, image, precision=0.8, debugg=False):
  14. self.image=image
  15. self.precision=precision
  16. self.debugg=debugg
  17. '''
  18. Searchs for an image on the screen
  19. returns :
  20. the top left corner coordinates of the element if found as an array [x,y] or [-1,-1] if not
  21. '''
  22. def imagesearch(self):
  23. im = pyautogui.screenshot()
  24. if self.debugg==True:
  25. im.save('testarea.png')# usefull for debugging purposes, this will save the captured region as "testarea.png"
  26. img_rgb = np.array(im)
  27. img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
  28. template = cv2.imread(self.image, 0)
  29. template.shape[::-1]
  30. res = cv2.matchTemplate(img_gray, template, cv2.TM_CCOEFF_NORMED)
  31. min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
  32. im.close()
  33. if max_val < self.precision:
  34. return [-1,-1]
  35. return max_loc
  36. '''
  37. Searchs for an image on screen continuously until it's found.
  38. input :
  39. time : Waiting time after failing to find the image
  40. returns :
  41. the top left corner coordinates of the element if found as an array [x,y]
  42. '''
  43. def imagesearch_loop(self, timesample):
  44. pos = self.imagesearch()
  45. while pos[0] == -1:
  46. print(image+" not found, waiting")
  47. time.sleep(timesample)
  48. pos = imagesearch(image, precision)
  49. return pos
  50. '''
  51. Searchs for an image on screen continuously until it's found or max number of samples reached.
  52. input :
  53. time : Waiting time after failing to find the image
  54. maxSamples: maximum number of samples before function times out.
  55. returns :
  56. the top left corner coordinates of the element if found as an array [x,y]
  57. '''
  58. def imagesearch_numLoop(self, timesample, maxSamples):
  59. pos = self.imagesearch()
  60. count = 0
  61. while pos[0] == -1:
  62. print(image+" not found, waiting")
  63. time.sleep(timesample)
  64. pos = imagesearch(self.image, self.precision)
  65. count = count + 1
  66. if count>maxSamples:
  67. break
  68. return pos
  69. '''
  70. Searches for an image on the screen and counts the number of occurrences.
  71. returns :
  72. the number of times a given image appears on the screen.
  73. optionally an output image with all the occurances boxed with a red outline.
  74. '''
  75. def imagesearch_count(self):
  76. im = pyautogui.screenshot()
  77. img_rgb = np.array(im)
  78. img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
  79. template = cv2.imread(self.image, 0)
  80. w, h = template.shape[::-1]
  81. res = cv2.matchTemplate(img_gray, template, cv2.TM_CCOEFF_NORMED)
  82. loc = np.where(res >= self.precision)
  83. count = 0
  84. for pt in zip(*loc[::-1]): # Swap columns and rows
  85. if self.debugg==True:
  86. cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0, 0, 255), 2)# to draw boxes around found occurances
  87. count = count + 1
  88. if self.debugg==True:
  89. cv2.imwrite('result.png', img_rgb)#to write output image with boxes drawn around occurances
  90. im.close()
  91. return count