forked from udacity/CarND-Behavioral-Cloning-P3
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit fb548f0
Showing
3 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import argparse | ||
import base64 | ||
from datetime import datetime | ||
import os | ||
|
||
import numpy as np | ||
import socketio | ||
import eventlet | ||
import eventlet.wsgi | ||
from PIL import Image | ||
from flask import Flask | ||
from io import BytesIO | ||
|
||
from keras.models import model_from_json | ||
from preprocess import process_image | ||
|
||
sio = socketio.Server() | ||
app = Flask(__name__) | ||
model = None | ||
prev_image_array = None | ||
|
||
|
||
@sio.on('telemetry') | ||
def telemetry(sid, data): | ||
# The current steering angle of the car | ||
steering_angle = data["steering_angle"] | ||
# The current throttle of the car | ||
throttle = data["throttle"] | ||
# The current speed of the car | ||
speed = data["speed"] | ||
# The current image from the center camera of the car | ||
imgString = data["image"] | ||
image = Image.open(BytesIO(base64.b64decode(imgString))) | ||
image_array = np.asarray(image) | ||
img = process_image(image_array) | ||
steering_angle = float(model.predict(img[None, :, :, :], batch_size=1)) | ||
throttle = 0.1 | ||
# print(steering_angle, throttle) | ||
send_control(steering_angle, throttle) | ||
|
||
# save frame | ||
timestamp = datetime.utcnow().strftime('%Y_%m_%d_%H_%M_%S_%f')[:-3] | ||
if args.image_folder != '': | ||
image.save('{}/{}.jpg'.format(args.image_folder, timestamp)) | ||
|
||
|
||
@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 definition json. Model weights 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() | ||
with open(args.model, 'r') as jfile: | ||
# NOTE: if you saved the file by calling json.dump(model.to_json(), ...) | ||
# then you will have to call: | ||
# | ||
# model = model_from_json(json.loads(jfile.read()))\ | ||
# | ||
# instead. | ||
model = model_from_json(jfile.read()) | ||
|
||
model.compile("adam", "mse") | ||
weights_file = args.model.replace('json', 'h5') | ||
model.load_weights(weights_file) | ||
|
||
if args.image_folder != '': | ||
print("Creating image folder at {}".format(args.image_folder)) | ||
os.makedirs(args.image_folder) | ||
print("This run will be recorded ...") | ||
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from moviepy.editor import ImageSequenceClip | ||
import argparse | ||
|
||
|
||
def main(): | ||
parser = argparse.ArgumentParser(description='Create driving video.') | ||
parser.add_argument( | ||
'image_folder', | ||
type=str, | ||
default='', | ||
help='Path to image folder. The video will be created from these images.' | ||
) | ||
parser.add_argument( | ||
'--fps', | ||
type=int, | ||
default=60, | ||
help='FPS (Frames per second) setting for the video.') | ||
args = parser.parse_args() | ||
|
||
video_file = args.image_folder + '.mp4' | ||
print("Creating video {}, FPS={}".format(video_file, args.fps)) | ||
clip = ImageSequenceClip(args.image_folder, fps=args.fps) | ||
clip.write_videofile(video_file) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |