示例#1
0
文件: basic.py 项目: vrr8483/P21321
def main():
    # To simplify the examples, we use a generic argument parser. It
    # lets you choose between UART/SPI/socket, set which sensor(s) to
    # use, and the verbosity level of the logging.
    args = utils.ExampleArgumentParser().parse_args()

    # The client logs using the logging module with a logger named
    # acconeer.exptool.*. We call another helper function which sets up
    # the logging according to the verbosity level set in the arguments:
    # -q  or --quiet:   ERROR   (typically not used)
    # default:          WARNING
    # -v  or --verbose: INFO
    # -vv or --debug:   DEBUG
    utils.config_logging(args)

    # Pick client depending on whether socket, SPI, or UART is used
    if args.socket_addr:
        client = SocketClient(args.socket_addr)
    elif args.spi:
        client = SPIClient()
    else:
        port = args.serial_port or utils.autodetect_serial_port()
        client = UARTClient(port)

    # Create a configuration to run on the sensor. A good first choice
    # is the envelope service, so let's pick that one.
    config = configs.EnvelopeServiceConfig()

    # In all examples, we let you set the sensor(s) via the command line
    config.sensor = args.sensors

    # Set the measurement range [meter]
    config.range_interval = [0.2, 0.3]

    # Set the target measurement rate [Hz]
    config.update_rate = 10

    # Other configuration options might be available. Check out the
    # example for the corresponding service/detector to see more.

    client.connect()

    # In most cases, explicitly calling connect is not necessary as
    # setup_session below will call connect if not already connected.

    # Set up the session with the config we created. If all goes well,
    # some information/metadata for the configured session is returned.
    session_info = client.setup_session(config)
    print("Session info:\n", session_info, "\n")

    # Now would be the time to set up plotting, signal processing, etc.

    # Start the session. This call will block until the sensor has
    # confirmed that it has started.
    client.start_session()

    # Alternatively, start_session can be given the config instead. In
    # that case, the client will call setup_session(config) for you
    # before starting the session. For example:
    # session_info = client.start_session(config)
    # As this will call setup_session in the background, this will also
    # connect if not already connected.

    # In this simple example, we just want to get a couple of sweeps.
    # To get a sweep, call get_next. get_next will block until the sweep
    # is recieved. Some information/metadata is returned together with
    # the data.
    f = open("demo.txt","a");
    for i in range(3):
        data_info, data = client.get_next()
		
		
        print("Sweep {}:\n".format(i + 1), data_info, "\n", data, "\n")
        f.write(numpy.array2string(data) + "\n")
    f.close()
    # We're done, stop the session. All buffered/waiting data is thrown
    # away. This call will block until the server has confirmed that the
    # session has ended.
    client.stop_session()

    # Calling stop_session before disconnect is not necessary as
    # disconnect will call stop_session if a session is started.

    # Remember to always call disconnect to do so gracefully
    client.disconnect()
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("-s",
                        "--load-train-set",
                        dest="server",
                        help="IP of streaming server",
                        default="127.0.0.1")
    args = parser.parse_args()

    filename = "model_data_speaker_sparse.npy"

    keras_proc = kp.MachineLearning()
    model_data, message = keras_proc.load_model(filename)

    print(message, "\n")

    if not model_data["loaded"]:
        print("Failed to load model!")
        sys.exit(1)

    try:
        client = SocketClient(args.server)
    except Exception:
        print("Failed to connect to server at {}!\n".format(args.server))
        traceback.print_exc()
        sys.exit(1)

    conf_speaker = model_data["sensor_config"]
    conf_speaker.sensor = SENSOR_SPEAKER
    feature_list = model_data["feature_list"]
    feature_list[0]["sensors"] = [SENSOR_SPEAKER[0]]
    feature_list[1]["sensors"] = [SENSOR_SPEAKER[1]]
    frame_settings = model_data["frame_settings"]

    frame_settings["auto_threshold"] = 1.5
    frame_settings["dead_time"] = 30
    frame_settings["auto_offset"] = 15

    frame_settings["collection_mode"] = "auto_feature_based"

    feature_process = feature_proc.FeatureProcessing(conf_speaker)
    feature_process.set_feature_list(feature_list)
    feature_process.set_frame_settings(frame_settings)

    handles = init_demo()
    handles["feature_process"] = feature_process
    handles["keras_proc"] = keras_proc

    # get session config for speaker mode
    info_speaker = client.setup_session(conf_speaker)
    handles["dist_processors"], handles[
        "dist_tags"] = setup_distance_detectors(conf_speaker, info_speaker,
                                                SENSOR_SPEAKER)

    try:
        client.start_session()
        client.stop_session()
    except Exception:
        print("Failed to start session!")
        traceback.print_exc()
        sys.exit(1)

    demo_mode = "speaker"
    if USE_PRESENCE:
        # get session config for presence mode
        demo_mode = "presence"
        conf_presence = presence_detection.get_sensor_config()
        conf_presence.sensor = SENSOR_PRESENCE
        conf_presence.range_interval = PRESENCE_RANGE
        info_presence = client.setup_session(conf_presence)
        handles["presence_processor"] = setup_presence_detector(
            conf_presence, info_presence)

    if USE_PRESENCE:
        start_mode = "presence"
    else:
        start_mode = "speaker"
    print("Starting demo in {}-mode!".format(start_mode))

    interrupt_handler = utils.ExampleInterruptHandler()
    print("Press Ctrl-C to end demo")

    client.start_session()

    while not interrupt_handler.got_signal:
        try:
            info, sweep = client.get_next()

            if demo_mode == "presence":
                new_mode = do_presence_mode(info, sweep, handles)
            else:
                data = {
                    "sweep_data": sweep,
                    "sensor_config": conf_speaker,
                    "session_info": info_speaker,
                }
                new_mode = do_speaker_mode(info, data, handles)

            # switch between presence and speaker mode
            if new_mode != demo_mode:
                demo_mode = new_mode
                handles["led_handle"].double_flash("#000000")
                time.sleep(1)
                client.stop_session()

                if demo_mode == "presence":
                    print("Switching to presence mode!\n")
                    handles["led_handle"].double_flash("#000000")
                    time.sleep(1)
                    handles["idle_counts"] = 0
                    info_presence = client.setup_session(conf_presence)
                    handles["presence_processor"] = setup_presence_detector(
                        conf_presence, info_presence)
                    if handles["play_mode"] == "play":
                        color = "#00ff00"
                    else:
                        color = "#ff0000"
                    handles["led_handle"].set_color(color,
                                                    pos=[3, 4],
                                                    brightness=0.1)
                else:
                    print("Switching to speaker mode!\n")
                    info_speaker = client.setup_session(conf_speaker)
                    handles["led_handle"].set_color("#00ff00",
                                                    pos=[3, 4],
                                                    brightness=0.1)

                client.start_session()
        except Exception:
            traceback.print_exc()
            break

    print("Disconnecting...")
    if handles["play_mode"] == "play":
        handles["lms_handle"].queue.put("PAUSE")
    handles["lms_handle"].stop()
    handles["led_handle"].off()
    handles["led_handle"].exit()
    client.disconnect()