Advertisement
ZxkH

Untitled

Jun 28th, 2024
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. import cv2
  2. import numpy as np
  3. import pyautogui
  4. import time
  5.  
  6. # Load the arrow images
  7. left_arrow_image = cv2.imread('left_arrow.png', cv2.IMREAD_GRAYSCALE).astype(np.uint8)
  8. right_arrow_image = cv2.imread('right_arrow.png', cv2.IMREAD_GRAYSCALE).astype(np.uint8)
  9. up_arrow_image = cv2.imread('up_arrow.png', cv2.IMREAD_GRAYSCALE).astype(np.uint8)
  10. down_arrow_image = cv2.imread('down_arrow.png', cv2.IMREAD_GRAYSCALE).astype(np.uint8)
  11.  
  12. while True:
  13. screenshot = pyautogui.screenshot()
  14. screenshot_np = np.array(screenshot)
  15. screenshot_gray = cv2.cvtColor(screenshot_np, cv2.COLOR_BGR2GRAY)
  16.  
  17. result_left = cv2.matchTemplate(screenshot_gray, left_arrow_image, cv2.TM_CCOEFF_NORMED)
  18. result_right = cv2.matchTemplate(screenshot_gray, right_arrow_image, cv2.TM_CCOEFF_NORMED)
  19. result_up = cv2.matchTemplate(screenshot_gray, up_arrow_image, cv2.TM_CCOEFF_NORMED)
  20. result_down = cv2.matchTemplate(screenshot_gray, down_arrow_image, cv2.TM_CCOEFF_NORMED)
  21.  
  22. min_val_left, max_val_left, min_loc_left, max_loc_left = cv2.minMaxLoc(result_left)
  23. min_val_right, max_val_right, min_loc_right, max_loc_right = cv2.minMaxLoc(result_right)
  24. min_val_up, max_val_up, min_loc_up, max_loc_up = cv2.minMaxLoc(result_up)
  25. min_val_down, max_val_down, min_loc_down, max_loc_down = cv2.minMaxLoc(result_down)
  26.  
  27. # Adjust the threshold for arrow detection
  28. detection_threshold = 0.7
  29.  
  30. if max_val_left > detection_threshold:
  31. print("Detected left arrow at position:", max_loc_left)
  32. pyautogui.keyDown('left')
  33. time.sleep(0.1) # Adjust the key press duration if needed
  34. pyautogui.keyUp('left')
  35.  
  36. if max_val_right > detection_threshold:
  37. print("Detected right arrow at position:", max_loc_right)
  38. pyautogui.keyDown('right')
  39. time.sleep(0.1) # Adjust the key press duration if needed
  40. pyautogui.keyUp('right')
  41.  
  42. if max_val_up > detection_threshold:
  43. print("Detected up arrow at position:", max_loc_up)
  44. pyautogui.keyDown('up')
  45. time.sleep(0.1) # Adjust the key press duration if needed
  46. pyautogui.keyUp('up')
  47.  
  48. if max_val_down > detection_threshold:
  49. print("Detected down arrow at position:", max_loc_down)
  50. pyautogui.keyDown('down')
  51. time.sleep(0.1) # Adjust the key press duration if needed
  52. pyautogui.keyUp('down')
  53.  
  54. # Add a condition to break out of the while loop
  55. if cv2.waitKey(1) == 13:
  56. break
  57.  
  58. cv2.destroyAllWindows()
  59.  
  60. input("Press Enter to close...")
  61.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement