-
Notifications
You must be signed in to change notification settings - Fork 0
/
Test_2D.py
109 lines (88 loc) · 3.12 KB
/
Test_2D.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
from vjoy import vJoy, test, ultimate_release
import numpy as np
import time
from getKeys import key_check
from grabscreen import grab_screen
from tensorflow.keras.models import load_model
import cv2
"""
The following code takes in the current screenshot of the game and feeds it to the trained neural network
The neural network returns the predicted steering and throttle value. The steering and throttle values are
passed to the game via a virtual controller
"""
prev_time = time.time()
s_gain = 1.8
t_gain = 2
vj = vJoy()
pause = False
x_range = 16393
z_range = 32786
wAxisX = 16393
wAxisY = 16393
wAxisZ = 0
wAxisXRot = 16393
wAxisYRot = 16393
wAxisZRot = 0
keys = key_check()
ultimate_release()
model = load_model("models/tf_model1.h5")
print("Autonomous mode in T minus-")
time.sleep(0.5)
for i in range(0, 3):
k = 3 - i
print(k)
time.sleep(1)
while True:
if not pause:
vj.open()
btn = 1
screen = grab_screen(region=(1170, 290, 1870, 430))
screen = cv2.resize(screen, (100, 100))
screen = cv2.cvtColor(screen, cv2.COLOR_BGR2HLS) # Converting image HLS for better detection of lanes
screen = cv2.GaussianBlur(screen, (3, 3), 0) # Blurring the image to remove noise
screen = screen/255 - 0.1 # Normalizing the image
curr_time = time.time()
FPS = 1 / (curr_time - prev_time)
FPS = round(FPS, 2)
prev_time = time.time()
control = (model.predict(screen[None, :, :, :], batch_size=1)) # Predicted steering and throttle value
print(" ")
print("\rSteering angle: {} Throttle: {} FPS: {}".format(int(control[0][0]*100), int(control[0][1]*100),
FPS), end="")
# Converting the steering and throttle values to corresponding joystick axis values
steering_angle = control[0][0]
steering_correction = (steering_angle * 16000)
steering_correction = int(steering_correction * s_gain)
throttle = control[0][1]
if throttle < 0:
throttle = throttle * 2
else:
pass
if throttle > 0:
forward = int(throttle*32000*1.6)
backward = 0
elif -0.3 < throttle <= 0:
forward = 0
backward = int(throttle*32000*t_gain)
else:
forward = 0
backward = 32000 # Full reverse throttle for instant braking at high speeds
joystickPosition = vj.generateJoystickPosition(wAxisX=16000 + steering_correction,
wAxisZ=forward, wAxisZRot=backward) # Joystick axis values
vj.update(joystickPosition)
time.sleep(0.1)
vj.sendButtons(0)
keys = key_check()
if 'T' in keys:
joystickPosition = vj.generateJoystickPosition(wAxisX=16000, wAxisY=16000)
vj.update(joystickPosition)
vj.sendButtons(0)
if pause:
pause = False
print('Play!')
time.sleep(1)
else:
print(" ")
print('Pause!')
pause = True
time.sleep(1)