-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtestingfile.py
88 lines (77 loc) · 2.25 KB
/
testingfile.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
import numpy as np
import cv2
from mss import mss
import time
from alexnet import alexnet
from directkeys import PressKey,ReleaseKey, W, A, S, D
from getkeys import key_check
WIDTH = 80
HEIGHT = 60
LR = 1e-3
EPOCHS = 8
MODEL_NAME = 'pygta5-car-{}-{}-{}-epochs.model'.format(LR, 'alexnetv2',EPOCHS)
def straight():
PressKey(W)
ReleaseKey(A)
ReleaseKey(D)
def frontleft():
PressKey(W)
PressKey(A)
ReleaseKey(D)
def frontright():
PressKey(W)
PressKey(D)
ReleaseKey(A)
def nokey():
ReleaseKey(A)
ReleaseKey(D)
ReleaseKey(W)
model = alexnet(WIDTH, HEIGHT, LR)
model.load(MODEL_NAME)
sct = mss()
monitor = {"top": 40, "left": 0, "width": 800, "height": 600}
def root():
for i in list(range(4))[::-1]:
print(i+1)
time.sleep(1)
paused = False
while 1:
if not paused:
keys = key_check()
last_time = time.time()
screen = cv2.cvtColor(np.array(sct.grab(monitor)), cv2.COLOR_BGR2GRAY)
screen = cv2.resize(screen, (80, 60))
moves = list(np.around(model.predict([screen.reshape(80, 60, 1)])[0]))
print(moves)
if moves == [1, 1, 0] or moves== [1,0,0]:
#print('wa')
frontleft()
elif moves == [0, 1, 0]:
#print('a')
straight()
elif moves == [0, 1, 1] or moves== [0,0,1]:
#print('wd')
frontright()
elif moves == [0, 0, 0]:
#print('0')
nokey()
if 'T' in keys:
if paused:
paused = False
time.sleep(1)
else:
paused = True
#print(f"fps: {1 / (time.time() - last_time)}")
if cv2.waitKey(25) & 0xFF == ord('q'):
cv2.destroyAllWindows()
break
else:
q = input('start? y/n')
if q =='y':
for i in list(range(4))[::-1]:
print('starting in :', i + 1)
time.sleep(1)
paused = False
elif q=='n':
break
root()