Beispiel #1
0
def main():
    """获取数据,然后预测获得的数据,编辑数据,控制车行驶"""
    global model, graph
    model_loaded = glob.glob('model/*.h5')  # glob.glob()匹配指定的文件
    for single_mod in model_loaded:
        model = load_model(single_mod)
    graph = tf.get_default_graph()

    try:
        with picamera.PiCamera(resolution=(160, 120)) as camera:
            time.sleep(2)
            output = ProcessOutput()
            camera.start_recording(output, format='mjpeg')
            while not output.done:
                camera.wait_recording(1)
            camera.stop_recording()
    finally:
        zth_car_control.clean_GPIO()
def my_car_control():
    global is_capture_running, key
    key = 4
    pygame.init()
    pygame.display.set_mode((1, 1))  # 窗口
    zth_car_control.car_stop()
    sleep(0.1)
    print("Start control!")

    while is_capture_running:
        # get input from human driver
        #
        for event in pygame.event.get():
            # 判断事件是不是按键按下的事件
            if event.type == pygame.KEYDOWN:
                key_input = pygame.key.get_pressed()  # 可以同时检测多个按键
                print(key_input[pygame.K_w], key_input[pygame.K_a],
                      key_input[pygame.K_d])
                # 按下前进,保存图片以2开头
                if key_input[pygame.K_w] and not key_input[
                        pygame.K_a] and not key_input[pygame.K_d]:
                    print("Forward")
                    key = 2
                    zth_car_control.car_move_forward()
                # 按下左键,保存图片以0开头
                elif key_input[pygame.K_a]:
                    print("Left")
                    zth_car_control.car_turn_left()
                    sleep(0.1)
                    key = 0
                # 按下d右键,保存图片以1开头
                elif key_input[pygame.K_d]:
                    print("Right")
                    zth_car_control.car_turn_right()
                    sleep(0.1)
                    key = 1
                # 按下s后退键,保存图片为3开头
                elif key_input[pygame.K_s]:
                    print("Backward")
                    zth_car_control.car_move_backward()
                    key = 3
                # 按下k停止键,停止
                elif key_input[pygame.K_k]:
                    zth_car_control.car_stop()
            # 检测按键是不是抬起
            elif event.type == pygame.KEYUP:
                key_input = pygame.key.get_pressed()
                # w键抬起,轮子回正
                if key_input[pygame.K_w] and not key_input[
                        pygame.K_a] and not key_input[pygame.K_d]:
                    print("Forward")
                    key = 2
                    zth_car_control.car_turn_straight()
                    zth_car_control.car_move_forward()
                # s键抬起
                elif key_input[pygame.K_s] and not key_input[
                        pygame.K_a] and not key_input[pygame.K_d]:
                    print("Backward")
                    key = 3
                    zth_car_control.car_move_backward()
                else:
                    print("Stop")
                    zth_car_control.car_stop()
                # car_control.cleanGPIO()
    zth_car_control.clean_GPIO()
                # s键抬起
                elif key_input[pygame.K_s] and not key_input[
                        pygame.K_a] and not key_input[pygame.K_d]:
                    print("Backward")
                    key = 3
                    zth_car_control.car_move_backward()
                else:
                    print("Stop")
                    zth_car_control.car_stop()
                # car_control.cleanGPIO()
    zth_car_control.clean_GPIO()


if __name__ == '__main__':
    global train_labels, train_img, key

    print("capture thread")
    print('-' * 50)
    capture_thread = threading.Thread(target=pi_capture, args=())  # 开启线程
    capture_thread.setDaemon(True)
    capture_thread.start()

    my_car_control()

    while is_capture_running:
        pass

    print("Done!")
    zth_car_control.car_stop()
    zth_car_control.clean_GPIO()