-
Notifications
You must be signed in to change notification settings - Fork 4
/
test_model.py
99 lines (80 loc) · 2.26 KB
/
test_model.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
# test_model.py
import numpy as np
from grabscreen import grab_screen
import cv2
import time
from directkeys import PressKey,ReleaseKey, UP, DOWN
from getkeys import key_check
import random
print("Loading Alexnet...")
from alexnet import alexnet
WIDTH = 250
HEIGHT = 60
LR = 1e-3
EPOCHS = 5
MODEL_NAME = 'pydinoai-{}-{}-{}-epochs-2k-data.model'.format(LR, 'alexnetv2',EPOCHS)
t_time = 0.09
def up():
PressKey(UP)
ReleaseKey(DOWN)
time.sleep(t_time)
ReleaseKey(DOWN)
def down():
PressKey(DOWN)
ReleaseKey(UP)
time.sleep(t_time)
ReleaseKey(UP)
def release():
ReleaseKey(DOWN)
ReleaseKey(UP)
model = alexnet(WIDTH, HEIGHT, LR)
model.load(MODEL_NAME)
print("Loaded Alexnet.")
def main():
last_time = time.time()
for i in list(range(4))[::-1]:
print(i+1)
time.sleep(1)
paused = False
while(True):
if not paused:
# (380,150,970,290) windowed mode to extract just the chrome://dino screen.
screen = grab_screen(region=(380,150,970,290))
#print('loop took {} seconds'.format(time.time()-last_time))
last_time = time.time()
screen = cv2.cvtColor(screen, cv2.COLOR_BGR2GRAY)
screen = cv2.resize(screen, (WIDTH,HEIGHT))
prediction = model.predict([screen.reshape(WIDTH,HEIGHT,1)])[0]
moves = list(np.around(prediction))
print(moves, prediction)
"""
thresh = .75
fwd_thresh = 0.70
#if prediction[0] > fwd_thresh:
# straight()
if prediction[1] > thresh:
up()
elif prediction[2] > thresh:
down()
else:
release()
"""
if moves == [0,1,0]:
print("Jump")
up()
elif moves == [0,0,1]:
print("Duck")
down()
else:
release()
keys = key_check()
# p pauses game and can get annoying.
if 'T' in keys:
if paused:
paused = False
time.sleep(1)
else:
paused = True
release()
time.sleep(1)
main()