Skip to content

Instantly share code, notes, and snippets.

@4423
Created November 19, 2018 15:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 4423/16c8197587f43e53556ba56ee5ede64b to your computer and use it in GitHub Desktop.
Save 4423/16c8197587f43e53556ba56ee5ede64b to your computer and use it in GitHub Desktop.
import pyautogui as pgui
import time
from PIL import ImageGrab
import numpy as np
import cv2
# もう一度押すボタンの座標
X,Y = 300,300
target1 = cv2.imread('hiori.jpg', 0)
target2 = cv2.imread('amana.jpg', 0)
detector = cv2.xfeatures2d.SIFT_create()
_, des_hiori = detector.detectAndCompute(target1, None)
_, des_amana = detector.detectAndCompute(target2, None)
def score(matches, match_param=0.6):
return sum(1 for m,n in matches if m.distance < match_param*n.distance)
def detect(gray, score_param=3):
kp, des = detector.detectAndCompute(gray, None)
matches_hiori = cv2.BFMatcher().knnMatch(des_hiori,des, k=2)
matches_amana = cv2.BFMatcher().knnMatch(des_amana,des, k=2)
sh = score(matches_hiori)
sa = score(matches_amana)
print sh, sa
return sh > score_param and sa > score_param
while True:
screen = np.asarray(ImageGrab.grab())
gray = cv2.cvtColor(screen, cv2.COLOR_BGR2GRAY)
detected = detect(gray)
if detected:
break
time.sleep(2)
pgui.click(X, Y)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment