Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cv2
- import numpy as np
- import pyautogui
- import time
- # Load the arrow images
- left_arrow_image = cv2.imread('left_arrow.png', cv2.IMREAD_GRAYSCALE).astype(np.uint8)
- right_arrow_image = cv2.imread('right_arrow.png', cv2.IMREAD_GRAYSCALE).astype(np.uint8)
- up_arrow_image = cv2.imread('up_arrow.png', cv2.IMREAD_GRAYSCALE).astype(np.uint8)
- down_arrow_image = cv2.imread('down_arrow.png', cv2.IMREAD_GRAYSCALE).astype(np.uint8)
- while True:
- screenshot = pyautogui.screenshot()
- screenshot_np = np.array(screenshot)
- screenshot_gray = cv2.cvtColor(screenshot_np, cv2.COLOR_BGR2GRAY)
- result_left = cv2.matchTemplate(screenshot_gray, left_arrow_image, cv2.TM_CCOEFF_NORMED)
- result_right = cv2.matchTemplate(screenshot_gray, right_arrow_image, cv2.TM_CCOEFF_NORMED)
- result_up = cv2.matchTemplate(screenshot_gray, up_arrow_image, cv2.TM_CCOEFF_NORMED)
- result_down = cv2.matchTemplate(screenshot_gray, down_arrow_image, cv2.TM_CCOEFF_NORMED)
- min_val_left, max_val_left, min_loc_left, max_loc_left = cv2.minMaxLoc(result_left)
- min_val_right, max_val_right, min_loc_right, max_loc_right = cv2.minMaxLoc(result_right)
- min_val_up, max_val_up, min_loc_up, max_loc_up = cv2.minMaxLoc(result_up)
- min_val_down, max_val_down, min_loc_down, max_loc_down = cv2.minMaxLoc(result_down)
- # Adjust the threshold for arrow detection
- detection_threshold = 0.7
- if max_val_left > detection_threshold:
- print("Detected left arrow at position:", max_loc_left)
- pyautogui.keyDown('left')
- time.sleep(0.1) # Adjust the key press duration if needed
- pyautogui.keyUp('left')
- if max_val_right > detection_threshold:
- print("Detected right arrow at position:", max_loc_right)
- pyautogui.keyDown('right')
- time.sleep(0.1) # Adjust the key press duration if needed
- pyautogui.keyUp('right')
- if max_val_up > detection_threshold:
- print("Detected up arrow at position:", max_loc_up)
- pyautogui.keyDown('up')
- time.sleep(0.1) # Adjust the key press duration if needed
- pyautogui.keyUp('up')
- if max_val_down > detection_threshold:
- print("Detected down arrow at position:", max_loc_down)
- pyautogui.keyDown('down')
- time.sleep(0.1) # Adjust the key press duration if needed
- pyautogui.keyUp('down')
- # Add a condition to break out of the while loop
- if cv2.waitKey(1) == 13:
- break
- cv2.destroyAllWindows()
- input("Press Enter to close...")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement