Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # parsing command line arguments
- import argparse
- # decoding camera images
- import base64
- # reading and writing files
- import os
- # high level file operations
- import shutil
- # for frametimestamp saving
- from datetime import datetime
- # input output
- from io import BytesIO
- from model.nvidia_cnn import model_creator
- # concurrent networking
- # web server gateway interface
- import eventlet.wsgi
- # matrix math
- import numpy as np
- # real-time server
- import socketio
- # image manipulation
- from PIL import Image
- # web framework
- from flask import Flask
- # load our saved model
- from tensorflow.contrib.keras.python.keras.models import load_model
- # helper class
- from preprocessor.preprocess import preprocess_image
- from preprocessor.advanced_preprocess import SelfDrivingDataGenerator
- # initialize our server
- sio = socketio.Server()
- # our flask (web) app
- app = Flask(__name__)
- # init our model and image array as empty
- model = None
- prev_image_array = None
- # set min/max speed for our autonomous car
- MAX_SPEED = 30
- MIN_SPEED = 5
- # and a speed limit
- speed_limit = MAX_SPEED
- # registering event handler for the server
- @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, how hard to push peddle
- 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 = SelfDrivingDataGenerator.preprocess_image(image) # apply the preprocessing
- image = np.array([image]) # the model expects 4D array
- # predict the steering angle for the image
- steering_angle = float(model.predict(image, batch_size=1))
- # 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)
- # save frame
- if args.image_folder != '':
- timestamp = datetime.utcnow().strftime('%Y_%m_%d_%H_%M_%S_%f')[:-3]
- image_filename = os.path.join(args.image_folder, timestamp)
- image.save('{}.jpg'.format(image_filename))
- 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__':
- parser = argparse.ArgumentParser(description='Remote Driving')
- parser.add_argument(
- 'model',
- type=str,
- help='Path to model h5 file. Model should be on the same path.'
- )
- parser.add_argument(
- 'image_folder',
- type=str,
- nargs='?',
- default='',
- help='Path to image folder. This is where the images from the run will be saved.'
- )
- args = parser.parse_args()
- # load model
- model = model_creator((66, 200, 3))
- model.load_weights(args.model)
- if args.image_folder != '':
- print("Creating image folder at {}".format(args.image_folder))
- if not os.path.exists(args.image_folder):
- os.makedirs(args.image_folder)
- else:
- shutil.rmtree(args.image_folder)
- os.makedirs(args.image_folder)
- print("RECORDING THIS RUN ...")
- else:
- print("NOT RECORDING THIS RUN ...")
- # 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)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement