Last active
February 18, 2019 12:53
-
-
Save AdroitAnandAI/6762eff7771a6ac32957750134fe35af to your computer and use it in GitHub Desktop.
Car Console interface to generate training data.
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
writeFrequency = 30001 | |
while(True): | |
# to return full key code so that arrow keys also can be detected. | |
k = cv2.waitKeyEx(1) | |
# Scan Codes for arrow keys | |
# 2490368 up - for gas_pedal | |
# 2621440 down - for break | |
# 2424832 left - to turn left | |
# 2555904 right - to turn right | |
# a = to shift gear up | |
# z = to shift gear down | |
# n = to shift to neutral (from all gears) | |
if (k==2490368): | |
if (gas_pedal < 5): # max value of gas = 5 | |
gas_pedal += 0.5 | |
keepUpArrowPressed = True | |
elif (k==2621440): | |
if (brake_pedal < 5): # max value of break = 5 | |
brake_pedal += 0.5 | |
keepUpArrowPressed = False | |
elif (k==2555904): | |
degrees += 3 | |
elif (k==2424832): | |
degrees -= 3 | |
elif (k == ord('a')): | |
if (gear < 5): | |
gear += 1 | |
gearShift = 1 | |
elif (k == ord('z')): | |
if (gear > 0): | |
gear -= 1 | |
gearShift = -1 | |
elif (k == ord('g')): | |
if (gear != 0): | |
gearShift = -2 | |
else: | |
if (gas_pedal > 0.01 and not keepUpArrowPressed): # min value of gas = 0 | |
gas_pedal -= 0.005 | |
if (brake_pedal > 0.01): # min value of break = 0 | |
brake_pedal -= 0.005 | |
gearShift = 0 | |
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) | |
if (animate): | |
# To show the braking animation | |
brake_img = cv2.imread(brake_path + " " + str(int(brake_pedal*2+1)).zfill(2)+".jpg") | |
brake_img_small = cv2.resize(brake_img, (0,0), fx=0.5, fy=0.5) | |
cv2.imshow('Brake Pedal', brake_img_small) | |
# To show the gal pedal animation | |
accel_img = cv2.imread(gas_path + " " + str(int(gas_pedal*2+1)).zfill(2)+".jpg") | |
accel_img_small = cv2.resize(accel_img, (0,0), fx=0.52, fy=0.52) | |
cv2.imshow('Acceleration Pedal', accel_img_small) | |
# To show the gear animation: | |
# Frames correspond to Neutral, 1st, 2nd, 3rd, 4th and 5th gears | |
gear_frame_map = [1, 12, 22, 30, 37, 45] | |
# Frames correspond to neutral between gears | |
neutral_frame_map = [16, 26, 33, 42, 50] | |
if (gearShift != 0): | |
# To animate shift from any gear to neutral | |
if (gearShift == -2): | |
init_frame = gear_frame_map [gear] | |
end_frame = neutral_frame_map [gear-1] | |
for i in range(init_frame, end_frame, 1): | |
gear_img = cv2.imread(gear_path + " " + str(i).zfill(2)+".jpg") | |
cv2.imshow('Gear-Transmission', gear_img) | |
cv2.waitKey(1) | |
gear = 0 | |
else: # when gearShift = -1 and +1 (gear up or down) | |
init_frame = gear_frame_map [gear + gearShift*-1] | |
end_frame = gear_frame_map [gear] | |
for i in range(init_frame, end_frame, gearShift): | |
gear_img = cv2.imread(gear_path + " " + str(i).zfill(2)+".jpg") | |
cv2.imshow('Gear-Transmission', gear_img) | |
cv2.waitKey(1) | |
# 1 driving second = 30 frames (extracted). We need to sync with this frame rate to find corresponding params of each frame. | |
# Hence we will write the parameter values for acceleration, brake, gear and steering every 33 milliseconds. To average out | |
# around 33 ms, we need to tune the write frequency cut off, considering the running time of each while loop. | |
now = int(round(time.time() * 1000000)) | |
if (now - writeTime >= writeFrequency): | |
f.write(str(frame) + " " + str(degrees) + " " + str(round(gas_pedal, 2)) + " " + str(round(brake_pedal, 2)) + " " + str(gear) + '\n') | |
writeTime = int(round(time.time() * 1000000)) | |
frame += 1 | |
# To exit interface, press 'q' | |
if (k == ord('q')): | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment