def drive(model): global gamepad gamepad = XInputDevice(1) gamepad.PlugIn() # last_time = time.time() # to measure the number of frames close = False # to exit execution pause = True # to pause execution print("Press T to start driving") while not close: while not pause: # apply the preprocessing image, speed, direct = img_process("Grand Theft Auto V") radar = cv2.cvtColor(image[194:230, 5:65, :], cv2.COLOR_RGB2BGR)[:, :, 1:3] image = preprocess(image) # predict steering angle for the image controls = model.predict( [np.array([image]), np.array([radar])], batch_size=1) if speed < 60: throttle = 0.4 controls = [[controls[0][0], throttle]] else: throttle = 0.0 controls = [[controls[0][0], throttle]] # set the gamepad values set_gamepad(controls) # print("Steering: {0:.2f}".format(controls[0][0])) # print('Main loop took {} seconds'.format(time.time() - last_time)) # last_time = time.time() keys = key_check() if 'T' in keys: pause = True # release gamepad keys gamepad.SetTrigger('L', 0) gamepad.SetTrigger('R', 0) gamepad.SetAxis('X', 0) print('Paused. To exit the program press Z.') time.sleep(0.5) keys = key_check() if 'T' in keys: pause = False print('Unpaused') time.sleep(1) elif 'Z' in keys: close = True print('Closing the program.') gamepad.UnPlug()
def main(): while True: screen, resized, speed, direct = img_process("Grand Theft Auto V") radar = cv2.cvtColor(resized[206:226, 25:45, :], cv2.COLOR_RGB2BGR)[:, :, 2:3] resized = preprocess(resized) left_line_color = [0, 255, 0] right_line_color = [0, 255, 0] yolo_screen, color_detected, obj_distance = yolo_detection(screen, direct) cv2.imshow("Driving-mode", yolo_screen) cv2.waitKey(1)
def drive(model): global gamepad gamepad = XInputDevice(1) gamepad.PlugIn() # last_time = time.time() # to measure the number of frames close = False # to exit execution pause = True # to pause execution throttle = 0 left_line_max = 80 right_line_max = 680 print("Press T to start driving") while not close: while not pause: # apply the preprocessing screen, resized, speed, direct = img_process("Grand Theft Auto V") radar = cv2.cvtColor(resized[206:226, 25:45, :], cv2.COLOR_RGB2BGR)[:, :, 2:3] resized = preprocess(resized) left_line_color = [0, 255, 0] right_line_color = [0, 255, 0] # predict steering angle for the image # original + radar (small) + speed controls = model.predict( [np.array([resized]), np.array([radar]), np.array([speed])], batch_size=1) # check that the car is following lane lane, stop_line = detect_lane(screen) # adjusting speed if speed < 35: throttle = 0.4 elif speed > 40: throttle = 0.0 # adjusting steering angle if lane[0] and lane[0][0] > left_line_max: controls[0][0] = 0.27 left_line_color = [0, 0, 255] elif lane[1] and lane[1][0] < right_line_max: controls[0][0] = -0.27 right_line_color = [0, 0, 255] # set the gamepad values set_gamepad([[controls[0][0], throttle]]) # print("Steering: {0:.2f}".format(controls[0][0])) # for yolo detection # screen = np.array(screen, dtype=np.uint8) # yolo_detection(tfnet, screen, speed, controls, gamepad, direct) # cv2.imshow('frame', screen) # if cv2.waitKey(1) & 0xFF == ord('q'): # break screen[280:-130, :, :] = draw_lane(screen[280:-130, :, :], lane, stop_line, left_line_color, right_line_color) cv2.imshow("Frame", screen) cv2.waitKey(1) if direct == Direct.NONE: cv2.destroyAllWindows() print("Arrived at destination.") stop() pause = True # print('Main loop took {} seconds'.format(time.time() - last_time)) # last_time = time.time() keys = key_check() if 'T' in keys: cv2.destroyAllWindows() pause = True # release gamepad keys stop() print('Paused. To exit the program press Z.') time.sleep(0.5) keys = key_check() if 'T' in keys: pause = False print('Unpaused') time.sleep(1) elif 'Z' in keys: close = True print('Closing the program.') gamepad.UnPlug()
def drive(model): global gamepad gamepad = XInputDevice(1) gamepad.PlugIn() last_time = time.time() # to measure the number of frames close = False # to exit execution pause = False # to pause execution stop = False # to stop the car throttle = 0 left_line_max = 75 right_line_max = 670 print("Press T to start driving") while not close: yolo_screen, resized, speed, direct = img_process("Grand Theft Auto V") ## cv2.imshow("Driving-mode", yolo_screen) ## cv2.waitKey(1) while not pause: # apply the preprocessing screen, resized, speed, direct = img_process("Grand Theft Auto V") radar = cv2.cvtColor(resized[206:226, 25:45, :], cv2.COLOR_RGB2BGR)[:, :, 2:3] resized = preprocess(resized) left_line_color = [0, 255, 0] right_line_color = [0, 255, 0] # predict steering angle for the image # original + radar (small) + speed controls = model.predict( [np.array([resized]), np.array([radar]), np.array([speed])], batch_size=1) # check that the car is following lane lane, stop_line = detect_lane(screen) # detect objects yolo_screen, color_detected, obj_distance = yolo_detection( screen, direct) if not stop: # adjusting speed if speed < 65: throttle = 0.8 elif speed > 70: throttle = 0.0 if 0 <= obj_distance <= 0.6: if speed < 5: throttle = 0 else: throttle = -0.7 if obj_distance <= 0.4 else -0.3 elif color_detected == "Red": if stop_line: if speed < 5: throttle = 0 elif 0 <= stop_line[0][1] <= 50: throttle = -0.5 elif 50 < stop_line[0][1] <= 120: throttle = -1 # else: # throttle = -0.5 elif speed > 5: throttle = -1 else: throttle = 0 cv2.destroyAllWindows() pause = True # adjusting steering angle if lane[0] and lane[0][0] > left_line_max: if abs(controls[0][0]) < 0.27: controls[0][0] = 0.27 left_line_color = [0, 0, 255] elif lane[1] and lane[1][0] < right_line_max: if abs(controls[0][0]) < 0.27: controls[0][0] = -0.27 right_line_color = [0, 0, 255] # set the gamepad values set_gamepad([[controls[0][0], throttle]]) # print('Main loop took {} seconds'.format(time.time() - last_time)) # last_time = time.time() screen[280:-130, :, :] = draw_lane(screen[280:-130, :, :], lane, stop_line, left_line_color, right_line_color) ## cv2.imshow("Driving-mode",yolo_screen) ## cv2.waitKey(1) if direct == 6: print("Arrived at destination.") stop = True print('Main loop took {} seconds'.format(time.time() - last_time)) last_time = time.time() keys = key_check() if 'T' in keys: cv2.destroyAllWindows() pause = True # release gamepad keys set_gamepad([[0, 0]]) print('Paused. To exit the program press Z.') time.sleep(0.5) keys = key_check() if 'T' in keys: pause = False stop = False print('Unpaused') time.sleep(1) elif 'Z' in keys: cv2.destroyAllWindows() close = True print('Closing the program.') gamepad.UnPlug()
def drive(): global gamepad gamepad = XInputDevice(1) gamepad.PlugIn() # last_time = time.time() # to measure the number of frames close = False # to exit execution pause = True # to pause execution stop = False # to stop the car throttle = 0 left_line_max = 75 right_line_max = 670 print("Press T to start driving") while not close: yolo_screen, resized, speed, direct = img_process("Grand Theft Auto V") cv2.imshow("Driving-mode", yolo_screen) cv2.waitKey(1) while not pause: # apply the preprocessing screen, resized, speed, direct = img_process("Grand Theft Auto V") radar = cv2.cvtColor(resized[206:226, 25:45, :], cv2.COLOR_RGB2BGR)[:, :, 2:3] resized = preprocess(resized) left_line_color = [0, 255, 0] right_line_color = [0, 255, 0] # predict steering angle for the image # original + radar (small) + speed # check that the car is following lane lane, stop_line = detect_lane(screen) # detect objects print(speed, "#", direct) if not stop: # adjusting speed if speed < 20: throttle = 0.3 elif speed > 25: throttle = 0.0 elif speed > 5: throttle = -1 else: throttle = 0 cv2.destroyAllWindows() pause = True # adjusting steering angle if lane[0] and lane[0][0] > left_line_max: left_line_color = [0, 0, 255] elif lane[1] and lane[1][0] < right_line_max: right_line_color = [0, 0, 255] # set the gamepad values # print('Main loop took {} seconds'.format(time.time() - last_time)) # last_time = time.time() screen[280:-130, :, :] = draw_lane(screen[280:-130, :, :], lane, stop_line, left_line_color, right_line_color) cv2.imshow("Driving-mode", yolo_screen) cv2.waitKey(1) # print('Main loop took {} seconds'.format(time.time() - last_time)) # last_time = time.time() keys = key_check() if 'T' in keys: cv2.destroyAllWindows() pause = True # release gamepad keys set_gamepad([[0, 0]]) print('Paused. To exit the program press Z.') time.sleep(0.5) keys = key_check() if 'T' in keys: pause = False stop = False print('Unpaused') time.sleep(1) elif 'Z' in keys: cv2.destroyAllWindows() close = True print('Closing the program.') gamepad.UnPlug()