Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.31 KB | None | 0 0
  1. import numpy as np
  2. from PIL import ImageGrab
  3. import cv2
  4. import time
  5. ## import pyautogui
  6. ## from directkeys import PressKey, W, A, S, D
  7.  
  8. import ctypes
  9.  
  10. SendInput = ctypes.windll.user32.SendInput
  11.  
  12. W = 0x11
  13.  
  14. # C struct redefinitions
  15. PUL = ctypes.POINTER(ctypes.c_ulong)
  16. class KeyBdInput(ctypes.Structure):
  17.     _fields_ = [("wVk", ctypes.c_ushort),
  18.                 ("wScan", ctypes.c_ushort),
  19.                 ("dwFlags", ctypes.c_ulong),
  20.                 ("time", ctypes.c_ulong),
  21.                 ("dwExtraInfo", PUL)]
  22.  
  23. class HardwareInput(ctypes.Structure):
  24.     _fields_ = [("uMsg", ctypes.c_ulong),
  25.                 ("wParamL", ctypes.c_short),
  26.                 ("wParamH", ctypes.c_ushort)]
  27.  
  28. class MouseInput(ctypes.Structure):
  29.     _fields_ = [("dx", ctypes.c_long),
  30.                 ("dy", ctypes.c_long),
  31.                 ("mouseData", ctypes.c_ulong),
  32.                 ("dwFlags", ctypes.c_ulong),
  33.                 ("time",ctypes.c_ulong),
  34.                 ("dwExtraInfo", PUL)]
  35.  
  36. class Input_I(ctypes.Union):
  37.     _fields_ = [("ki", KeyBdInput),
  38.                  ("mi", MouseInput),
  39.                  ("hi", HardwareInput)]
  40.  
  41. class Input(ctypes.Structure):
  42.     _fields_ = [("type", ctypes.c_ulong),
  43.                 ("ii", Input_I)]
  44.  
  45. # Actuals Functions
  46.  
  47. def PressKey(hexKeyCode):
  48.     extra = ctypes.c_ulong(0)
  49.     ii_ = Input_I()
  50.     ii_.ki = KeyBdInput( 0, hexKeyCode, 0x0008, 0, ctypes.pointer(extra) )
  51.     x = Input( ctypes.c_ulong(1), ii_ )
  52.     ctypes.windll.user32.SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))
  53.  
  54. def ReleaseKey(hexKeyCode):
  55.     extra = ctypes.c_ulong(0)
  56.     ii_ = Input_I()
  57.     ii_.ki = KeyBdInput( 0, hexKeyCode, 0x0008 | 0x0002, 0, ctypes.pointer(extra) )
  58.     x = Input( ctypes.c_ulong(1), ii_ )
  59.     ctypes.windll.user32.SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))
  60.  
  61.  
  62. template = cv2.imread('template1.png', 0)
  63. widtht, heightt = template.shape[::-1]
  64.  
  65. def process_img(image):
  66.     original_image = image
  67.     # convert to gray
  68.     processed_img = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  69.     # edge detection
  70.     processed_img =  cv2.Canny(processed_img, threshold1 = 200, threshold2=300)
  71.     return processed_img
  72.  
  73. def tap():
  74.     PressKey(W)
  75.     time.sleep(0.23)
  76.     ReleaseKey(W)
  77.     time.sleep(0.2)
  78.  
  79. def main():
  80.     print('PLEASE FOCUS ON THE GAME!')
  81.     for i in list(range(4))[::-1]:
  82.         print(i+1)
  83.         time.sleep(1)
  84.  
  85.     last_time = time.time()
  86.     while 1:
  87.         # TODO: Implement real AI
  88.         ## tap()
  89.        
  90.         screen =  np.array(ImageGrab.grab(bbox=(0,45,800,500)))
  91.         #print('Frame took {} seconds'.format(time.time()-last_time))
  92.         last_time = time.time()
  93.         new_screen = process_img(screen)
  94.         match = cv2.matchTemplate(new_screen, template, cv2.TM_CCOEFF_NORMED)
  95.         threshold = 0.8
  96.  
  97.         positiont = np.where(match >= threshold)
  98.         for point in zip(*positiont[::-1]): #draw the rectangle around the matched template
  99.             cv2.rectangle(new_screen, point, (point[0] + widtht, point[1] + heightt), (0, 204, 153), 0)
  100.             print(1)
  101.  
  102.         cv2.imshow('window', new_screen)
  103.         #cv2.imshow('window',cv2.cvtColor(screen, cv2.COLOR_BGR2RGB))
  104.        
  105.         if cv2.waitKey(25) & 0xFF == ord('q'):
  106.             cv2.destroyAllWindows()
  107.             break
  108. if __name__ == '__main__': main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement