Skip to content

Instantly share code, notes, and snippets.

@z9gmoon
Created October 16, 2020 01:29
Show Gist options
  • Save z9gmoon/f5ff9534cc8ab6be3c6468e53f0f852f to your computer and use it in GitHub Desktop.
Save z9gmoon/f5ff9534cc8ab6be3c6468e53f0f852f to your computer and use it in GitHub Desktop.
#optimize time
import os
import cv2
import time
import sys
import numpy as np
from datetime import datetime
import win32api, win32con
defaultThreshold = 0.55
listScr = ['img/sample1.png','img/sample2.png','img/sample3.png','img/sample4.png','img/sample5.png','img/sample6.png','img/sample7.png','img/sample9.png']
def check(i1,i2,threshold):
res = cv2.matchTemplate(i1,i2,cv2.TM_CCOEFF_NORMED)
threshold
loc = np.where( res >= threshold)
return len(loc[0]) > 0
def findSample(imgToCheck, threshold = defaultThreshold):
if threshold > 0.95 or threshold < 0.30:
return 'null'
index = 0
listSame = []
for x in listScr:
img1 = cv2.imread(x)
result = check(img1,imgToCheck,threshold)
if result:
listSame.append(index)
index += 1
# print(listSame)
countSame = len(listSame)
if countSame == 0:
# print(0)
return findSample(imgToCheck,threshold - 0.03)
elif countSame > 1:
# print(2)
return findSample(imgToCheck,threshold + 0.035) #fix 2 or no img
else:
# print(listScr[listSame[0]])
# return listSame[0]
print(threshold)
return listScr[listSame[0]]
xCor = 535
yCor = 48
coorClick = [[71,156],[145,156],[221,156],[71,231],[145,231],[221,231],[71,307],[145,307],[221,307],[71,382],[146,385],[221,382],[71,458],[145,458],[221,458]];
click2nd = 0
after2010 = 0
index = 0
# pair = dict.fromkeys(['img/sample' + str(i) + '.png' for i in range(8)], [])
pair = dict()
def click(x,y):
x += 535
y += 48
win32api.SetCursorPos((x,y))
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,x,y,0,0)
def capture(x,y):
x = x + xCor - 16
y = y + yCor - 25
w = 40
h = 40
delay = 400 - click2nd * 50
print(delay)
# cmd = f"C:/Users/User/Desktop/ff/nircmd/nircmd.exe cmdwait 100 savescreenshot img/test_{index}_1.png {x} {y} 40 40"
# os.system(cmd)
cmd = f"C:/Users/User/Desktop/ff/nircmd/nircmd.exe cmdwait 300 savescreenshot img/test_{index}_2.png {x} {y} 32 50"
os.system(cmd)
def saveObj(index,idSrc):
if idSrc in pair:
pass
else:
pair[idSrc] = []
tempArray = pair[idSrc]
tempArray.append(index)
pair[idSrc] = tempArray
# fix append will create same value on all key
def clickPair(idSrc):
f1Index = pair[idSrc][0]
f2Index = pair[idSrc][1]
click(coorClick[f1Index][0],coorClick[f1Index][1])
time.sleep(0.27)
click(coorClick[f2Index][0],coorClick[f2Index][1])
def alreadyClickPair(idSrc):
f1Index = pair[idSrc][0]
f2Index = pair[idSrc][1]
return f2Index == 1 + f1Index
time.sleep(2)
start = time.time()
for x in coorClick:
click(x[0],x[1])
capture(x[0],x[1])
print(f"index: {index}")
img2 = cv2.imread(f"img/test_{index}_2.png")
result = findSample(img2)
print(f"img/test_{index}_2.png with {result}")
if result == 'img/sample9.png':
index+=1
time.sleep(0.5)
listScr.pop(7)
after2010 = 1
continue
saveObj(index,result)
index+=1
time.sleep(0.25 + click2nd * 0.60 + after2010 * 0.22)
# time.sleep(0.25 + click2nd * 0.55 + after2010 * 0.25)
print(f"aaaaa{click2nd}")
if len(pair[result]) > 1:
f1Index = pair[result][0]
print(f"{len(pair[result])},{f1Index}")
# print(f1Index)
if click2nd > 0:
click2nd = 1 - click2nd
if alreadyClickPair(result):
continue
clickPair(result)
else:
click(coorClick[f1Index][0],coorClick[f1Index][1])
time.sleep(1.15)
continue
click2nd = 1 - click2nd
after2010 = 0
print(time.time() - start)
print(pair)
def checkIsPair(index):
if len(pair[index]) > 1:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment