-
Notifications
You must be signed in to change notification settings - Fork 1
/
Run_simulation.py
89 lines (70 loc) · 2.29 KB
/
Run_simulation.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
from flask import Flask
import socketio
import eventlet
import eventlet.wsgi
from io import BytesIO
import argparse
import numpy as np
from PIL import Image
import Dataset_generation
from keras.models import load_model
import base64
parser = argparse.ArgumentParser()
parser.add_argument('--ip', type=str,
help='Enter IP address for socket', default='0.0.0.0')
parser.add_argument('--min_speed', type=int,
help='Enter minimum speed of car', default=15)
parser.add_argument('--max_speed', type=int,
help='Enter maximum spped of car', default=25)
parser.add_argument('--path', type=str,
help='Enter path to saved model file',
default='./model.h5')
args = parser.parse_args()
path = args.path
ip = args.ip
Max_speed = args.max_speed
Min_speed = args.min_speed
speed_limit = Max_speed
model = load_model(path)
sio = socketio.Server()
app = Flask(__name__)
def send_control(steering_angle, throttle):
sio.emit(
'steer',
data={
'steering_angle': steering_angle.__str__(),
'throttle': throttle.__str__()
},
skip_sid=True
)
@sio.on('connect')
def connect(sid, environ):
print('connect', sid)
send_control(0, 0)
@sio.on('telemetry')
def telemetry(sid, data):
if data:
steering_angle = float(data['steering_angle'])
throttle = float(data['throttle'])
speed = float(data['speed'])
image = Image.open(BytesIO(base64.b64decode(data['image'])))
try:
image = np.asarray(image)
image = Dataset_generation.process(image)
image = np.array((image))
img = image.reshape((1,66,200,3))
steering_angle = float(model.predict(img))
global speed_limit
if speed > speed_limit:
speed_limit = Min_speed
else:
speed_limit = Max_speed
throttle = 1.0 - ((steering_angle)**2) - ((speed/speed_limit)**2)
print(steering_angle, throttle, speed)
send_control(steering_angle, throttle)
except Exception as e:
print(e)
else:
sio.emit('manual', data={}, skip_sid=True)
app = socketio.Middleware(sio, app)
eventlet.wsgi.server(eventlet.listen((ip, 4567)), app)