-
Notifications
You must be signed in to change notification settings - Fork 0
/
drive.py
108 lines (77 loc) · 3.05 KB
/
drive.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
# Kindly note that the libraries required are not the latest libraries.
# The simulator wasn't updated for long hence the latest python version it supports is python 3.5.2
# and previous versions of libraries which support python 3.5.2
# This program has been written according to that
import base64
from datetime import datetime
import numpy as np
import socketio
import eventlet
import eventlet.wsgi
from PIL import Image
from flask import Flask
from io import BytesIO
from openvino.inference_engine import IECore
sio = socketio.Server()
app = Flask(__name__)
model = None
prev_image_array = None
MAX_SPEED = 25
MIN_SPEED = 10
speed_limit = MAX_SPEED
@sio.on('telemetry')
def telemetry(sid, data):
if data:
# The current steering angle of the car
steering_angle = float(data["steering_angle"])
# The current throttle of the car
throttle = float(data["throttle"])
# The current speed of the car
speed = float(data["speed"])
# The current image from the center camera of the car
image = Image.open(BytesIO(base64.b64decode(data["image"])))
try:
image = np.asarray(image) # from PIL image to numpy array
image = np.array([image]) # the model expects 4D array
# predict the steering angle for the image
steering_angle, hidden = exec_net.infer(inputs={input_blob: (image, hidden)})
# lower the throttle as the speed increases
# if the speed is above the current speed limit, we are on a downhill.
# make sure we slow down first and then go back to the original max speed.
global speed_limit
if speed > speed_limit:
speed_limit = MIN_SPEED # slow down
else:
speed_limit = MAX_SPEED
throttle = 1.0 - steering_angle**2 - (speed/speed_limit)**2
print('{} {} {}'.format(steering_angle, throttle, speed))
send_control(steering_angle, throttle)
except Exception as e:
print(e)
else:
sio.emit('manual', data={}, skip_sid=True)
@sio.on('connect')
def connect(sid, environ):
print("connect ", sid)
send_control(0, 0)
def send_control(steering_angle, throttle):
sio.emit(
"steer",
data={
'steering_angle': steering_angle.__str__(),
'throttle': throttle.__str__()
},
skip_sid=True)
if __name__ == '__main__':
ie = IECore()
# read the xml model description and bin file for the weights
net = ie.read_network(model="Driver\\Driver.xml", weights="Driver\\Driver.bin")
exec_net = ie.load_network(network=net, device_name="CPU")
input_blob = next(iter(net.input_info))
out_blob = next(iter(net.outputs))
# Hidden state of the model
hidden = np.zeros((1,5))
# wrap Flask application with engineio's middleware
app = socketio.Middleware(sio, app)
# deploy as an eventlet WSGI server
eventlet.wsgi.server(eventlet.listen(('', 4567)), app)