-
Notifications
You must be signed in to change notification settings - Fork 0
/
testSim.py
62 lines (49 loc) · 1.56 KB
/
testSim.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
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
from tensorflow.keras.models import load_model
from flask import Flask
import numpy as np
import tensorflow as tf
physical_devices = tf.config.experimental.list_physical_devices('GPU')
tf.config.experimental.set_memory_growth(physical_devices[0], True)
import cv2
import eventlet
import socketio
import base64
from io import BytesIO
from PIL import Image
sio = socketio.Server()
app = Flask(__name__) # '__main__'
maxSpeed = 30
def preProcess(img):
img = img[60:135, :, :]
img = cv2.cvtColor(img, cv2.COLOR_RGB2YUV)
img = cv2.GaussianBlur(img, (3, 3), 0)
img = cv2.resize(img, (200, 66))
img = img / 255
return img
@sio.on('telemetry')
def telemetry(sid, data):
speed = float(data['speed'])
image = Image.open(BytesIO(base64.b64decode(data['image'])))
image = np.asarray(image)
image = preProcess(image)
image = np.array([image])
steering = float(model.predict(image))
throttle = 1.0 - speed / maxSpeed
print(f'{steering}, {throttle}, {speed}')
sendControl(steering, throttle)
@sio.on('connect')
def connect(sid, environ):
print('Connected')
sendControl(0, 0)
def sendControl(steering, throttle):
sio.emit('steer', data={
'steering_angle': steering.__str__(),
'throttle': throttle.__str__()
})
if __name__ == '__main__':
model = load_model('model.h5')
app = socketio.Middleware(sio, app)
### LISTEN TO PORT 4567
eventlet.wsgi.server(eventlet.listen(('', 4567)), app)