-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
116 lines (70 loc) · 2.35 KB
/
main.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import pyautogui
import time
import cv2
import numpy as np
from mss import mss
sct = mss()
bounding_box = {'top': 275, 'left': 275, 'width': 1325, 'height': 475}
template = cv2.imread('claw.png')
filename = sct.shot()
print(filename)
def find_cheese(img_rgb):
img_rgb = (img_rgb).astype(np.uint8)[:,:,:3]
lower_yellow = np.array([25, 150, 0])
upper_yellow = np.array([30, 255, 255])
mask = cv2.inRange(img_rgb, lower_yellow, upper_yellow)
points = cv2.findNonZero(mask)
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
# Find x,y coordinates of all non-white pixels in original image
Y, X = np.where(mask==255)
Z = np.column_stack((X,Y)).astype(np.float32)
nClusters = 2
try:
ret,label,center=cv2.kmeans(Z,nClusters,None,criteria,10,cv2.KMEANS_RANDOM_CENTERS)
except:
return [(-1, -1), (-1,-1)]
return center
def find_claw(sct_img_np):
find_claw_img = (sct_img_np[10:60, 0:1325]).astype(np.uint8)[:,:,:3]
w, h = template.shape[:-1]
res = cv2.matchTemplate(find_claw_img, template, cv2.TM_CCOEFF_NORMED)
threshold = .85
loc = np.where(res >= threshold)
for pt in zip(*loc[::-1]): # Switch columns and rows
return pt[0]
lower_grey = np.array([130, 130, 130])
upper_grey = np.array([255, 255, 255])
def find_claw_2(sct_img_np):
find_claw_img = (sct_img_np[10:30, 0:1325]).astype(np.uint8)[:,:,:3]
mask = cv2.inRange(find_claw_img, lower_grey, upper_grey)
res = cv2.bitwise_and(find_claw_img, find_claw_img, mask=mask)
points = cv2.findNonZero(mask)
try:
return points[0][0][0]
except TypeError:
return -1
def claw_loop():
error = 2
x,y = centers[0]
x = int(x)
while True:
sct_img = sct.grab(bounding_box)
sct_img_np = np.array(sct_img)
claw_loc = find_claw_2(sct_img_np)
if x > claw_loc - error and x < claw_loc + error :
pyautogui.press('space')
print("OVER")
return
while True:
print("wait")
time.sleep(5)
print("go")
pyautogui.press('space')
sct_img = sct.grab(bounding_box)
sct_img_np = np.array(sct_img)
centers = find_cheese(sct_img_np)
if(centers[0][0] == -1):
print("no cheese")
pyautogui.press('space')
else:
claw_loop()