-
Notifications
You must be signed in to change notification settings - Fork 0
/
autonomous_driving.py
63 lines (48 loc) · 1.47 KB
/
autonomous_driving.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
'''
This file is for testing the model in the Udacity simulator and was taken from:
https://youtu.be/mVUrErF5xq8
Author: Murtaza's Workshop - Robotics and AI
Date: July 7, 2020
'''
print('Setting UP')
# Import necessary libraries
from data_preprocessing import *
import socketio
import eventlet
import numpy as np
from flask import Flask
from tensorflow.keras.models import load_model
import base64
from io import BytesIO
from PIL import Image
import cv2
import os
# Disable debugging logs
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
sio = socketio.Server()
app = Flask(__name__) # '__main__'
maxSpeed = 30
@sio.on('telemetry')
def telemetry(sid, data):
speed = float(data['speed'])
image = Image.open(BytesIO(base64.b64decode(data['image'])))
image = np.asarray(image)
image = image_preprocessing(image)
image = np.array([image])
steering = float(model.predict(image))
throttle = 1.0 - speed / maxSpeed
print('{} {} {}'.format(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)
eventlet.wsgi.server(eventlet.listen(('', 4567)), app)