<reponame>mankadronit/SelfDrivingCar
import numpy as np
import cv2
from keras.models import load_model
import scipy.misc
import scipy
from subprocess import call

model = load_model('Autopilot.h5')

img = cv2.imread('steering_wheel_image.jpg',0)
rows,cols = img.shape

smoothed_angle = 0

def keras_predict(model, image):
    processed = keras_process_image(image)
    steering_angle = float(model.predict(processed, batch_size=1))
    steering_angle = steering_angle * 60
    return steering_angle


def keras_process_image(img):
    image_x = 100
    image_y = 100
    img = cv2.resize(img, (image_x, image_y))
    img = np.array(img, dtype=np.float32)
    img = np.reshape(img, (-1, image_x, image_y, 1))
    return img

i = 0
while(cv2.waitKey(10) != ord('q')):
    full_image = scipy.misc.imread("./dataset/data/" + str(i) + ".jpg", mode="RGB")
    image = cv2.resize((cv2.cvtColor(full_image, cv2.COLOR_RGB2HSV))[:, :, 1], (100, 100))
    degrees = keras_predict(model, image)
    call("clear")
    print("Predicted steering angle: " + str(degrees) + " degrees")
    cv2.imshow("frame", cv2.cvtColor(full_image, cv2.COLOR_RGB2BGR))
    #make smooth angle transitions by turning the steering wheel based on the difference of the current angle
    #and the predicted angle
    smoothed_angle += 0.2 * pow(abs((degrees - smoothed_angle)), 2.0 / 3.0) * (degrees - smoothed_angle) / abs(degrees - smoothed_angle)
    M = cv2.getRotationMatrix2D((cols/2,rows/2),-smoothed_angle,1)
    dst = cv2.warpAffine(img,M,(cols,rows))
    cv2.imshow("steering wheel", dst)
    i += 1

cv2.destroyAllWindows()
import numpy as np
import cv2
from keras.models import load_model
import scipy.misc
import scipy
from subprocess import call

model = load_model('Autopilot.h5')

img = cv2.imread('steering_wheel_image.jpg',0)
rows,cols = img.shape

smoothed_angle = 0

def keras_predict(model, image):
    processed = keras_process_image(image)
    steering_angle = float(model.predict(processed, batch_size=1))
    steering_angle = steering_angle * 60
    return steering_angle


def keras_process_image(img):
    image_x = 100
    image_y = 100
    img = cv2.resize(img, (image_x, image_y))
    img = np.array(img, dtype=np.float32)
    img = np.reshape(img, (-1, image_x, image_y, 1))
    return img

i = 0
while(cv2.waitKey(10) != ord('q')):
    full_image = scipy.misc.imread("./dataset/data/" + str(i) + ".jpg", mode="RGB")
    image = cv2.resize((cv2.cvtColor(full_image, cv2.COLOR_RGB2HSV))[:, :, 1], (100, 100))
    degrees = keras_predict(model, image)
    call("clear")
    print("Predicted steering angle: " + str(degrees) + " degrees")
    cv2.imshow("frame", cv2.cvtColor(full_image, cv2.COLOR_RGB2BGR))
    #make smooth angle transitions by turning the steering wheel based on the difference of the current angle
    #and the predicted angle
    smoothed_angle += 0.2 * pow(abs((degrees - smoothed_angle)), 2.0 / 3.0) * (degrees - smoothed_angle) / abs(degrees - smoothed_angle)
    M = cv2.getRotationMatrix2D((cols/2,rows/2),-smoothed_angle,1)
    dst = cv2.warpAffine(img,M,(cols,rows))
    cv2.imshow("steering wheel", dst)
    i += 1

cv2.destroyAllWindows()
#make smooth angle transitions by turning the steering wheel based on the difference of the current angle #and the predicted angle