-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclasses2048.py
309 lines (259 loc) · 10.7 KB
/
classes2048.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import numpy as np
#from sklearn.linear_model import SGDClassifier
from joblib import dump, load
from time import sleep
from pandas import DataFrame
from functions import getscore, getld, getflist, getfit, getGamePixels
import os
import mss
import mss.tools
from datetime import datetime
import shelve
try:
from PIL import Image
except ImportError:
import Image
class state():
def __init__(self):
self.load()
shelf = shelve.open("./.conf/props.txt", flag='r')
for key in shelf.keys():
self.props = shelf[key]
#do something with it
shelf.close()
def fit(self,folder='./data/gamestate'):
flist=getflist(folder)
self.stateSgd=getfit(flist, max_iter=600)
def save(self, fname='./.conf/state.joblib'):
dump(self.stateSgd, fname)
def load(self, fname='./.conf/state.joblib'):
self.stateSgd=load(fname)
return self.stateSgd
# def predict_on_img(self, fl):
# array=Image.open(fl).convert('L')
# return (self.sgd.predict(array.reshape(1,-1))[0] )
def statepredict(self, save=False,folder='./'):
with mss.mss() as sct:
# l1,l2 =107, 15
# top, left =335, 581
# monitor = {"top": top, "left": left, \
# "width": 4*l1+5*l2, "height": 4*l1+5*l2}
# l1,l2 =107, 16
# top, left =game.props['top']['l1']['starts']-l2, 581
s=0
for i in range(3):
s=s+self.props['top']['l'+str(i+1)]['starts']-self.props['top']['l'+str(i)]['ends']
s=s+self.props['left']['l'+str(i+1)]['starts']-self.props['left']['l'+str(i)]['ends']
dif=int(round (s/6))
monitor = {"top": self.props['top']['l0']['starts']-dif,
"left": self.props['left']['l0']['starts']-dif,
"width": self.props['left']['l3']['ends']-\
self.props['left']['l0']['starts']+2*dif,
"height": self.props['top']['l3']['ends']-\
self.props['top']['l0']['starts'] +2*dif}
sct_img=sct.grab(monitor)
if save:
img = Image.frombytes("RGB", sct_img.size, sct_img.bgra, "raw", "BGRX")
img=img.resize( (500,500) )
output=folder+str(datetime.now())+".png".format(**monitor)
img.save(output)
# mss.tools.to_png(sct_img.rgb, sct_img.size,
# )
sc=Image.frombytes("RGB", sct_img.size, sct_img.bgra, "raw", "BGRX").convert('L')
sc=np.array(sc.resize( (500,500) ) )
self.now=self.stateSgd.predict(sc.reshape(1,-1))[0]
return(self.now)
def getprops(self, save=False):
#some notes of using this
import random
from pynput.keyboard import Listener, Key
keys= [Key.right,Key.down,Key.up,Key.left]
global tempy, tempx
tempx=np.array([])
tempy=np.array([])
def on_release(key):
if key == Key.esc:
## Stop listener
return (False)
def on_press(key):
global tempy
global tempx
from functions import mv
if key == Key.enter:
mv(random.choice(keys))
sleep(1)
with mss.mss() as sct:
temp = sct.grab(sct.monitors[0])
temp=Image.frombytes("RGB", temp.size, \
temp.bgra, "raw", "BGRX").convert('L')
temp=np.array(temp)
temp2=np.array(Image.fromarray( temp.flatten().reshape(-1,1050, order='F') ) )
temp2=list(map(sum,temp2))
temp=list(map(sum,temp))
if len(tempy)==0:
tempy=np.array([temp])
tempx=np.array([temp2])
else:
tempy=np.append(tempy,[temp], axis=0)
tempx=np.append(tempx,[temp2], axis=0)
return (True)
with Listener(
on_press=on_press,
on_release=on_release) as listener:
listener.join()
qy=getGamePixels(tempy)
qx=getGamePixels(tempx)
props={}
top={}
left={}
n=0
left['l'+str(n)]={}
left['l'+str(n)]['starts']=qx[0]
top['l'+str(n)]={}
top['l'+str(n)]['starts']=qy[0]
prev=qy[0]
for i in qy[1:]:
if i!=prev+1:
top['l'+str(n)]['ends']=prev
n=n+1
top['l'+str(n)]={}
top['l'+str(n)]['starts']=i
prev=i
top['l'+str(n)]['ends']=i
props['top']=top
n=0
prev=qx[0]
for i in qx[1:]:
if i!=prev+1:
left['l'+str(n)]['ends']=prev
n=n+1
left['l'+str(n)]={}
left['l'+str(n)]['starts']=i
prev=i
left['l'+str(n)]['ends']=i
props['left']=left
if save:
shelf = shelve.open("./.conf/props.txt", flag="w")
shelf['key1']=props
shelf.close()
self.props=props
return (props)
class tbl(state):
def __init__(self,state):
self.stateSgd=state.stateSgd
super().__init__()
self.load()
def fit(self,folder='./data/numbers'):
flist=getflist()
self.sgd=getfit(flist, max_iter=1000)
def save(self, fname='./.conf/sgd.joblib'):
dump(self.sgd, fname)
def load(self, fname='./.conf/sgd.joblib'):
self.sgd=load(fname)
def predict(self, x):
self.sgd.predict(x)
def tablestate(self, log=True, sub=1):
if log:
return np.reshape(np.array( list( map(np.log2,self.tbl.replace(np.nan, sub).values.flatten()) ) ), [1, 16])
else:
return np.reshape(self.tbl.replace(np.nan, sub).values.flatten(),[1, 16])
def gettbl(self,ts=0.05, save=False, folder='./'):
# firsttime=True
if hasattr(self, 'tbl'): #ввести аргумент чекинг
# firsttime=False
self.prev=self.tbl
self.tbl=DataFrame(columns=np.arange(4), index=np.arange(4))
sleep(ts)
# l1,l2 =107, 15
# top, left=337, 581
with mss.mss() as sct:
for i in range(4):
for j in range(4):
monitor = {"top": self.props['top']['l'+str(i)]['starts'],
"left": self.props['left']['l'+str(j)]['starts'],
"width": self.props['left']['l'+str(j)]['ends'] - self.props['left']['l'+str(j)]['starts'] ,
"height": self.props['top']['l'+str(i)]['ends'] - self.props['top']['l'+str(i)]['starts']}
# monitor = {"top": top+l2+i*(l1+l2), "left": left+l2+j*(l1+l2), \
# "width": l1, "height": l1}
sct_img=sct.grab(monitor)
if save:
img = Image.frombytes("RGB", sct_img.size, sct_img.bgra, "raw", "BGRX")
img=img.resize( (107,107) )
output=folder+str(datetime.now())+".png".format(**monitor)
img.save(output)
# img = folder+str(i)+'*'+str(j)+str(datetime.now())+".png".format(**monitor)
# mss.tools.to_png(sct_img.rgb, sct_img.size, output=img)
n=Image.frombytes("RGB", sct_img.size, sct_img.bgra, "raw", "BGRX").convert('L')
n=n.resize( (107,107) )
n=np.array(n)
n=self.sgd.predict(n.reshape(1,-1) )[0]
if n=='np.nan':
self.tbl.iloc[i,j]=np.nan
else:
self.tbl.iloc[i,j]=float(n)
self.tbl=self.tbl.astype(float)
return (self.tbl)
class score():
def fit(self,folder='./data/score'):
flist=getflist()
self.sgd=getfit(flist, max_iter=500)
def save(self, fname='./.conf/score.joblib'):
dump(self.sgd, fname)
def load(self, fname='./.conf/score.joblib'):
self.sgd=load(fname)
def predict(self, score='', maxlength=15, sleep=5):
if score=='':
score=getscore(sleep=sleep)
#вроде работает, но надо поправить
color=[187, 173, 160]
colorcol=np.array([[color]*19]).astype('uint8')
#list of digits
ld=getld(score)
score=''
for j in ld:
temp=np.concatenate( (j, np.repeat( [colorcol], maxlength-len(j) , axis=1)[0]))
# temp=np.reshape(f.arflat(temp), [-1,maxlength,3], order='F')
Image.fromarray(temp).convert("RGB").save('temp.png')
# temp=imread('temp.png',0)
score=score+(self.sgd.predict(temp.reshape(1,-1))[0])
os.remove('temp.png')
self.score=score
def getl(self):
#find params for images
with mss.mss() as sct:
sleep(3)
test = sct.shot(output='fullscreen.png')
# pr=cv2.imread(test,0)
os.remove(test)
def sc(self,ts=4, save=False):
self.tbl=DataFrame(columns=np.arange(4), index=np.arange(4))
sleep(ts)
for i in range(4):
for j in range(4):
scr=sum([i,j])
pr=Image.open(scr).convert('L')
#pr=imread(scr,0)
if not save:
os.remove(scr)
pr=self.sgd.predict(pr.reshape(1,-1) )[0]
if pr=='np.nan':
self.tbl.iloc[i,j]=np.nan
else:
self.tbl.iloc[i,j]=float(pr)
self.tbl=self.tbl.astype(float)
def capture(self, fname=''):
#pright=910
pright=710
ptop=185
pwidth=100
pheight=19
with mss.mss() as sct:
# The screen part to capture
monitor = {"top": ptop, "left": pright-pwidth, "width": pwidth, "height": pheight}
if fname=='':
fname = "temp/"+str(datetime.now()).format(**monitor)
# Grab the data
sct_img = sct.grab(monitor)
# Save to the picture file
mss.tools.to_png(sct_img.rgb, sct_img.size, output=fname)
return(fname)