Skip to content

Instantly share code, notes, and snippets.

@snowyegret23
Created October 8, 2023 12:57
Show Gist options
  • Save snowyegret23/cd2d070e026bcf2f56cbdb84f3da45d8 to your computer and use it in GitHub Desktop.
Save snowyegret23/cd2d070e026bcf2f56cbdb84f3da45d8 to your computer and use it in GitHub Desktop.
1번 모니터 특정 영역에서 이미지 인식 후, 이미지에 따라 키보드 입력
import cv2
import mss
import numpy as np
import keyboard
import time
with mss.mss() as sct:
area1 = {"left": 100, "top": 1000, "width": 150, "height": 150, "mon": 1}
area2 = {"left": 200, "top": 1000, "width": 150, "height": 150, "mon": 1}
area3 = {"left": 300, "top": 1000, "width": 150, "height": 150, "mon": 1}
images = {"a": cv2.imread('a.png', cv2.IMREAD_GRAYSCALE),
"b": cv2.imread('b.png', cv2.IMREAD_GRAYSCALE),
"c": cv2.imread('c.png', cv2.IMREAD_GRAYSCALE),
"d": cv2.imread('d.png', cv2.IMREAD_GRAYSCALE)}
def detect_image():
ar_1, ar_2, ar_3 = "", "", ""
for name, img in images.items():
res = cv2.matchTemplate(cv2.cvtColor(np.array(sct.grab(area1)), cv2.COLOR_BGRA2GRAY), img, cv2.TM_CCOEFF_NORMED)
threshold = 0.8
loc = np.where(res >= threshold)
if len(loc[0]) > 0:
ar_1 = name
break
for name, img in images.items():
res = cv2.matchTemplate(cv2.cvtColor(np.array(sct.grab(area2)), cv2.COLOR_BGRA2GRAY), img, cv2.TM_CCOEFF_NORMED)
threshold = 0.8
loc = np.where(res >= threshold)
if len(loc[0]) > 0:
ar_2 = name
break
for name, img in images.items():
res = cv2.matchTemplate(cv2.cvtColor(np.array(sct.grab(area3)), cv2.COLOR_BGRA2GRAY), img, cv2.TM_CCOEFF_NORMED)
threshold = 0.8
loc = np.where(res >= threshold)
if len(loc[0]) > 0:
ar_3 = name
break
keyboard.press_and_release(ar_1)
time.sleep(0.05)
keyboard.press_and_release(ar_2)
time.sleep(0.05)
keyboard.press_and_release(ar_3)
time.sleep(0.05)
while True:
try:
detect_image()
except:
continue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment