-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_nn.py
170 lines (136 loc) · 4.48 KB
/
test_nn.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import numpy as np
# from nn_tflearn import alexnet2
# from nn_inception import inception_v3
# from models import inception_v3
from model import formula_network
from screen import grab_screen, place_emulator
import cv2
import time
from config import IMG_WIDTH, IMG_HEIGHT, SCREEN_REGION, RESIZE_RATIO, training_file_name, testing_file_name, HOST, PORT
from util import thread_on_key
from getkeys import key_check
import http.server
import threading
WIDTH = IMG_WIDTH // RESIZE_RATIO
HEIGHT = IMG_HEIGHT // RESIZE_RATIO
# file_name = config.file_name
LR = 0.0005
# EPOCHS = 1
# MODEL_NAME = "f1-bot-{}-{}-{}.model".format("alexnet2", LR, EPOCHS)
MODEL_NAME = "models\\f1-bot-formula_network-0.0005-3.model\\f1-bot-formula_network-0.0005-3.model"
model = formula_network(width=WIDTH, height=HEIGHT, lr=LR, output=4)
model.load(MODEL_NAME)
inputs = []
connected = False
class HttpServerHandler(http.server.BaseHTTPRequestHandler):
"""
A simple HTTP server capable of handling GET and POST requests
"""
inputs = []
def _set_headers(self, response=None, connection=None):
self.send_response(200)
self.send_header('Content-Type', 'text/html; charset=utf-8')
if response is not None:
self.send_header('Content-Length', len(response))
if connection is not None:
self.send_header('Connection', connection)
self.end_headers()
def do_HEAD(self):
self._set_headers()
def do_GET(self):
self.protocol_version = 'HTTP/1.1'
response = encoded_inputs()
self._set_headers(response=response)
self.wfile.write(response)
# def do_POST(self):
# # print('POST received\n')
# self.send_response(200)
# self.protocol_version = 'HTTP/1.1'
# self.end_headers()
# content_length = int(self.headers['Content-Length'])
# set_inputs(self.rfile.read(content_length).decode('utf-8').replace('payload=', '').split("x"))
# response = b'ok'
# self.wfile.write(response)
def log_message(self, format, *args):
return
def set_connected():
global connected
if not connected:
connected = True
print("Emulator connected! Ready to record.")
def encoded_inputs():
global inputs
if len(inputs) < 4:
return b"0x0x0.5x0.5"
else:
if int(round(inputs[0])) == 1:
i = "Truex"
else:
i = "Falsex"
if int(round(inputs[1])) == 1:
i += "Truex"
else:
i += "Falsex"
if inputs[2] > inputs[3]:
i += str(-inputs[2])
else:
i += str(inputs[3])
# i = str(int(round(inputs[0]))) + "x"
# i += str(int(round(inputs[1]))) + "x"
# i += str(inputs[2]) + "x"
# i += str(inputs[3])
# i += str((0 - input[2]) + input[3])
return i.encode()
def test_nn():
place_emulator()
for i in range(0, 3):
print("Taking control in", 3 - i, "...")
time.sleep(1)
paused = False
print("- - - DRIVING - - -")
while True:
timestamp = time.time()
if not paused:
# screen = grab_screen(CROPPED_REGION)
screen = cv2.resize(grab_screen(SCREEN_REGION), ((IMG_WIDTH // RESIZE_RATIO), (IMG_HEIGHT // RESIZE_RATIO)))
screen = cv2.cvtColor(screen, cv2.COLOR_BGR2GRAY)
# print("frame took {} seconds".format(time.time() - timestamp))
# screen = cv2.cvtColor(screen, cv2.COLOR_BGR2GRAY)
# cv2.imshow("test", screen)
# if cv2.waitKey(25) & 0xFF == ord("q"):
# cv2.destroyAllWindows()
prediction = model.predict([screen.reshape(WIDTH, HEIGHT, 1)])[0]
global inputs
inputs = prediction
# print("L:" + str(round(inputs[2], 8)) + " R:" + str(round(inputs[3], 8)))
print(encoded_inputs())
keys = key_check()
if "P" in keys:
if paused:
paused = False
print("Taking back control in 3...")
time.sleep(1)
print("Taking back control in 2...")
time.sleep(1)
print("Taking back control in 1...")
time.sleep(1)
print("- - - DRIVING RESUMED - - -")
else:
paused = True
print("Network paused...")
time.sleep(1)
def main():
place_emulator()
print("Starting HTTP Server")
httpd = http.server.HTTPServer((HOST, PORT), HttpServerHandler)
print("Running HTTP server at: {}:{}".format(httpd.server_address[0], httpd.server_address[1]))
thread_http = threading.Thread(target=httpd.serve_forever, name="thread_http")
thread_testing = threading.Thread(target=test_nn, name="thread_testing")
thread_http.start()
print("")
print("Waiting for you to setup connection between emulator and http server in order to send inputs from the NN")
print("Hold \'G\' on your keyboard to hand over control to the network")
thread_control = threading.Thread(target=thread_on_key, name="thread_control",
args=("G", thread_testing))
thread_control.start()
main()