-
Notifications
You must be signed in to change notification settings - Fork 0
/
aiPlays.py
71 lines (54 loc) · 1.47 KB
/
aiPlays.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
from alexnet import alexnet
from directkeys import PressKey,ReleaseKey, W, A, S, D
import numpy as np
import image, time
from PIL import ImageGrab
import cv2
from getkeys import key_check
import os
def left():
PressKey(A)
ReleaseKey(D)
print('A')
def right():
PressKey(D)
ReleaseKey(A)
print('D')
def no_keys():
ReleaseKey(A)
ReleaseKey(D)
WIDTH = 40
HEIGHT = 60
LR = 1e-3
EPOCHS = 10
MODEL_NAME = 'CNN-Play-Race-{}-{}-{}.model'.format(LR,'alexnetv3',EPOCHS)
model = alexnet(WIDTH, HEIGHT, LR)
model.load(MODEL_NAME)
def main():
paused = False
while(True):
if not paused:
screen = np.array(ImageGrab.grab(bbox=(0,40,400,600)))
screen = cv2.cvtColor(screen, cv2.COLOR_BGR2GRAY)
screen = cv2.resize(screen,(40,60))
prediction = model.predict([screen.reshape(WIDTH,HEIGHT,1)])[0]
moves = list(np.around(prediction))
if moves == [1,0,0]:
left()
elif moves == [0,1,0]:
right()
elif moves == [0,0,1]:
no_keys()
keys = key_check()
if 'P' in keys:
if paused:
paused = False
print('resuming...')
time.sleep(1)
else:
paused = True
print('paused...')
ReleaseKey(A)
ReleaseKey(D)
time.sleep(1)
main()