<jupyter_start><jupyter_text>Based on: https://github.com/udacity/self-driving-car-sim<jupyter_code>from flask import Flask
import eventlet.wsgi
import eventlet
import socketio
import base64
from PIL import Image
from io import BytesIO
import numpy as np
import matplotlib.pyplot as plt # to show the image
from keras.models import load_model 
sio = socketio.Server()
app = Flask(__name__)

# function to control the car
def send_control(steering_angle, throttle):
    sio.emit("steer", data={'steering_angle': str(steering_angle),
'throttle': str(throttle) }, skip_sid=True)<jupyter_output><empty_output><jupyter_text>Load the downloaded model<jupyter_code>model = load_model('mymodel.h5')
model.summary()
# function to process image
def process_image(img):
    # we are deleting unused pixels (the top and the bottom of the image and we are taking every second pixel)
    return img[10:130:2,::4,:]

# function to receive data from simulator
@sio.on('telemetry')
def telemetry(sid, data):
    if data:
        speed = float(data["speed"])
        image_str = data["image"]
        
        decode = base64.b64decode(image_str) # decode the image_str (instead of string we have bytes)
        image = Image.open(BytesIO(decode)) # from bytes to image
        image_array = np.asarray(image) # from image to numpy array
        
        # to show the image, which is 'seeing' by car
        #plt.imshow(image_array)
        #plt.show();
        
        img = process_image(image_array)
        # we expend numer of dimensions (+1)
        img_batch = np.expand_dims(img, axis=0)
        
        # steering_angle now is coming our (downloaded) model
        steering_angle = float(model.predict(img_batch))
        
        #steering_angle = -1.0        # angle <-1.0 .. 1.0>
        throttle = 0.15              # < -1.0 .. 1.0> 
        
        if speed < 13:
            throttle = 0.5
        if speed > 15:
            throttle = -0.1
        
        send_control(steering_angle, throttle)
    else:
        sio.emit('manual', data={}, skip_sid=True)

app = socketio.Middleware(sio, app)
eventlet.wsgi.server(eventlet.listen(('', 4567)), app)<jupyter_output>(6395) wsgi starting up on http://0.0.0.0:4567
wsgi exiting
(6395) wsgi exited, is_accepting=True
{'fraction_non_alphanumeric': 0.10782147591692444, 'fraction_numerical': 0.025187803800265134, 'mean_word_length': 2.9719298245614034, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 1, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '440512', 'license': 'MIT', 'n_tokens_mistral': 780, 'n_tokens_neox': 731, 'n_words': 202, 'path': 'Step3/day2.ipynb', 'repo_name': 'baczynska/DW_5challenge', 'size': '2263'}