-
Notifications
You must be signed in to change notification settings - Fork 0
/
imagedetect.py
63 lines (54 loc) · 2.1 KB
/
imagedetect.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from pyautogui import *
import pyautogui
import time
import keyboard
import random
import win32api
import win32con
down_path = 'd.png'
up_path = 'u.png'
left_path = 'l.png'
right_path = 'r.png'
down_region = (0,0,71,62)
left_region = (0,0,68,52)
right_region = (0,0,62,60)
up_region = (0,0,66,63)
search_region = (107,621, 512, 135)
tmp = (0, 0, 1920, 1080)
confidence_level = 0.8
def perform_keypress(action:str) -> None:
action = action.lower().strip()
if action in ['up', 'down', 'left', 'right']:
keyboard.press_and_release(action)
# if action == 'up':
# keyboard.press_and_release('up')
# elif action == 'down':
# keyboard.press_and_release('down')
# elif action == 'left':
# keyboard.press_and_release('left')
# elif action == 'right':
# keyboard.press_and_release('right')
def find_image_on_screen(region, grayscale=False, confidence=0.8):
try:
up = pyautogui.locateOnScreen(up_path, region=region, grayscale=grayscale, confidence=confidence)
down = pyautogui.locateOnScreen(down_path, region=region, grayscale=grayscale, confidence=confidence)
left = pyautogui.locateOnScreen(left_path, region=region, grayscale=grayscale, confidence=confidence)
right = pyautogui.locateOnScreen(right_path, region=region, grayscale=grayscale, confidence=confidence)
if up is not None:
print("Up arrow detected.")
perform_keypress('up')
elif down is not None:
print("Down arrow detected.")
perform_keypress('down')
elif left is not None:
print("Left arrow detected.")
perform_keypress('left')
elif right is not None:
print("Right arrow detected.")
perform_keypress('right')
else:
print("No arrow detected.")
except pyautogui.ImageNotFoundException:
print("Error: Image not found.")
while keyboard.is_pressed('q') == False:
find_image_on_screen(region=search_region, confidence=confidence_level)