Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.36 KB | None | 0 0
  1. # parsing command line arguments
  2. import argparse
  3. # decoding camera images
  4. import base64
  5. # reading and writing files
  6. import os
  7. # high level file operations
  8. import shutil
  9. # for frametimestamp saving
  10. from datetime import datetime
  11. # input output
  12. from io import BytesIO
  13. from model.nvidia_cnn import model_creator
  14. # concurrent networking
  15. # web server gateway interface
  16. import eventlet.wsgi
  17. # matrix math
  18. import numpy as np
  19. # real-time server
  20. import socketio
  21. # image manipulation
  22. from PIL import Image
  23. # web framework
  24. from flask import Flask
  25. # load our saved model
  26. from tensorflow.contrib.keras.python.keras.models import load_model
  27.  
  28. # helper class
  29. from preprocessor.preprocess import preprocess_image
  30. from preprocessor.advanced_preprocess import SelfDrivingDataGenerator
  31.  
  32. # initialize our server
  33. sio = socketio.Server()
  34. # our flask (web) app
  35. app = Flask(__name__)
  36. # init our model and image array as empty
  37. model = None
  38. prev_image_array = None
  39.  
  40. # set min/max speed for our autonomous car
  41. MAX_SPEED = 30
  42. MIN_SPEED = 5
  43.  
  44. # and a speed limit
  45. speed_limit = MAX_SPEED
  46.  
  47.  
  48. # registering event handler for the server
  49. @sio.on('telemetry')
  50. def telemetry(sid, data):
  51. if data:
  52. # The current steering angle of the car
  53. steering_angle = float(data["steering_angle"])
  54. # The current throttle of the car, how hard to push peddle
  55. throttle = float(data["throttle"])
  56. # The current speed of the car
  57. speed = float(data["speed"])
  58. # The current image from the center camera of the car
  59. image = Image.open(BytesIO(base64.b64decode(data["image"])))
  60. try:
  61. image = np.asarray(image) # from PIL image to numpy array
  62. image = SelfDrivingDataGenerator.preprocess_image(image) # apply the preprocessing
  63. image = np.array([image]) # the model expects 4D array
  64.  
  65. # predict the steering angle for the image
  66. steering_angle = float(model.predict(image, batch_size=1))
  67. # lower the throttle as the speed increases
  68. # if the speed is above the current speed limit, we are on a downhill.
  69. # make sure we slow down first and then go back to the original max speed.
  70. global speed_limit
  71. if speed > speed_limit:
  72. speed_limit = MIN_SPEED # slow down
  73. else:
  74. speed_limit = MAX_SPEED
  75. throttle = 1.0 - steering_angle ** 2 - (speed / speed_limit) ** 2
  76.  
  77. print('{} {} {}'.format(steering_angle, throttle, speed))
  78. send_control(steering_angle, throttle)
  79. except Exception as e:
  80. print(e)
  81.  
  82. # save frame
  83. if args.image_folder != '':
  84. timestamp = datetime.utcnow().strftime('%Y_%m_%d_%H_%M_%S_%f')[:-3]
  85. image_filename = os.path.join(args.image_folder, timestamp)
  86. image.save('{}.jpg'.format(image_filename))
  87. else:
  88.  
  89. sio.emit('manual', data={}, skip_sid=True)
  90.  
  91.  
  92. @sio.on('connect')
  93. def connect(sid, environ):
  94. print("connect ", sid)
  95. send_control(0, 0)
  96.  
  97.  
  98. def send_control(steering_angle, throttle):
  99. sio.emit(
  100. "steer",
  101. data={
  102. 'steering_angle': steering_angle.__str__(),
  103. 'throttle': throttle.__str__()
  104. },
  105. skip_sid=True)
  106.  
  107.  
  108. if __name__ == '__main__':
  109. parser = argparse.ArgumentParser(description='Remote Driving')
  110. parser.add_argument(
  111. 'model',
  112. type=str,
  113. help='Path to model h5 file. Model should be on the same path.'
  114. )
  115. parser.add_argument(
  116. 'image_folder',
  117. type=str,
  118. nargs='?',
  119. default='',
  120. help='Path to image folder. This is where the images from the run will be saved.'
  121. )
  122. args = parser.parse_args()
  123.  
  124. # load model
  125. model = model_creator((66, 200, 3))
  126. model.load_weights(args.model)
  127.  
  128. if args.image_folder != '':
  129. print("Creating image folder at {}".format(args.image_folder))
  130. if not os.path.exists(args.image_folder):
  131. os.makedirs(args.image_folder)
  132. else:
  133. shutil.rmtree(args.image_folder)
  134. os.makedirs(args.image_folder)
  135. print("RECORDING THIS RUN ...")
  136. else:
  137. print("NOT RECORDING THIS RUN ...")
  138.  
  139. # wrap Flask application with engineio's middleware
  140. app = socketio.Middleware(sio, app)
  141.  
  142. # deploy as an eventlet WSGI server
  143. eventlet.wsgi.server(eventlet.listen(('', 4567)), app)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement