-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplaygame.py
84 lines (73 loc) · 2.65 KB
/
playgame.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
import numpy as np
from PIL import ImageGrab
import cv2
import time
from directkeys import PressKey,ReleaseKey, turnRight, turnLeft, straight, reverse, reverseLeft, reverseRight, W, A, D
from alexnet import alexnet
from getkeys import key_check
WIDTH = 80
HEIGHT = 60
LR = 0.001
EPOCHS = 15
MODEL_NAME = 'nfspb_epoch_lr{}_{}_{}_reverse.model'.format('0001', 'alexnet', EPOCHS) #model name
model = alexnet(WIDTH, HEIGHT, LR) #init the model architechture
model.load(MODEL_NAME) #load the pretrained model
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:
# 800x600 windowed mode
screen = np.array(ImageGrab.grab(bbox=(0, 40, 800, 640))) #grab the game screen
print('loop took {} seconds'.format(time.time() - last_time))
last_time = time.time()
screen = cv2.cvtColor(screen, cv2.COLOR_BGR2GRAY) #convert to greyscale
screen = cv2.resize(screen, (80, 60)) #resize as per model accepted dimensions
cv2.imshow('window', screen)
moves = list(np.around(model.predict([screen.reshape(80, 60, 1)])[0])) #predict the moves based on the above resized image(array/tensor) -> returns list
if moves == [1, 0, 0, 0]:
turnLeft()
time.sleep(0.09)
#straight()
print('Predict to turn left')
elif moves == [0, 1, 0, 0]:
straight()
print('Predict to turn straight')
elif moves == [0, 0, 1, 0]:
turnRight()
time.sleep(0.09)
#straight()
print('Predict to turn Right')
elif moves == [0, 0, 0, 1]:
reverse()
time.sleep(0.5)
print('Reverse')
elif moves == [1, 0, 0, 1]:
reverseLeft()
print('Reverse Left')
elif moves == [0, 0, 1, 1]:
reverseRight()
print("Reverse Right")
else:
pass
print('No moves')
keys = key_check()
# to pause the keypress simulation, to gain control over the car.
if 'J' in keys:
if paused:
paused = False
time.sleep(1)
else:
paused = True
ReleaseKey(A)
ReleaseKey(W)
ReleaseKey(D)
time.sleep(1)
if cv2.waitKey(25) & 0xFF == ord('q'):
cv2.destroyAllWindows()
break
if __name__ == '__main__':
main()