Exemple #1
0
    def setup(self, win):
        self.td_plot = win.addPlot(row=0, col=0, title="PSD input data")
        self.td_plot.setMenuEnabled(False)
        self.td_plot.setMouseEnabled(x=False, y=False)
        self.td_plot.hideButtons()
        self.td_plot.addLegend()
        self.td_curves = []
        for i, depth in enumerate(self.depths):
            name = "{:.0f} cm".format(depth * 100)
            curve = self.td_plot.plot(pen=utils.pg_pen_cycler(i), name=name)
            self.td_curves.append(curve)

        self.collapsed_plot = win.addPlot(
            row=1, col=0, title="Collapsed sqrt(PSD)")
        self.collapsed_plot.setMenuEnabled(False)
        self.collapsed_plot.setMouseEnabled(x=False, y=False)
        self.collapsed_plot.hideButtons()
        self.collapsed_plot.setXRange(0, 1)
        self.collapsed_curve = self.collapsed_plot.plot(pen=utils.pg_pen_cycler())
        self.collapsed_vline = pg.InfiniteLine(pen=utils.pg_pen_cycler())
        self.collapsed_vline.hide()
        self.collapsed_plot.addItem(self.collapsed_vline)

        bg = pg.mkColor(0xFF, 0xFF, 0xFF, 150)
        self.collapsed_text = pg.TextItem(anchor=(0, 1), color="k", fill=bg)
        self.collapsed_text.setPos(0, 0)
        self.collapsed_text.setZValue(100)
        self.collapsed_plot.addItem(self.collapsed_text)

        self.collapsed_history_plot = win.addPlot(
            row=2, col=0, title="Collapsed sqrt(PSD) history")
        self.collapsed_history_plot.setMenuEnabled(False)
        self.collapsed_history_plot.setMouseEnabled(x=False, y=False)
        self.collapsed_history_plot.hideButtons()
        self.collapsed_history_im = pg.ImageItem()
        self.collapsed_history_im.setLookupTable(utils.pg_mpl_cmap("viridis"))
        self.collapsed_history_plot.addItem(self.collapsed_history_im)

        self.dw_plot = win.addPlot(row=3, col=0, title="Depthwise PSD")
        self.dw_plot.setMenuEnabled(False)
        self.dw_plot.setMouseEnabled(x=False, y=False)
        self.dw_plot.hideButtons()
        self.dw_im = pg.ImageItem()
        self.dw_im.setLookupTable(utils.pg_mpl_cmap("viridis"))
        self.dw_plot.addItem(self.dw_im)
        self.dw_plot.setLabel("bottom", "Depth (cm)")
        self.dw_plot.getAxis("bottom").setTickSpacing(6 * self.downsampling_factor, 6)

        self.setup_is_done = True
        self.update_processing_config()
    def setup(self, win):
        num_sensors = len(self.sensor_config.sensor)

        self.ampl_plot = win.addPlot(row=0, col=0, colspan=num_sensors)
        self.ampl_plot.setMenuEnabled(False)
        self.ampl_plot.setMouseEnabled(x=False, y=False)
        self.ampl_plot.hideButtons()
        self.ampl_plot.showGrid(x=True, y=True)
        self.ampl_plot.setLabel("bottom", "Depth (m)")
        self.ampl_plot.setLabel("left", "Amplitude")
        self.ampl_plot.setXRange(*self.depths.take((0, -1)))
        self.ampl_plot.addLegend(offset=(-10, 10))

        self.phase_plot = win.addPlot(row=1, col=0, colspan=num_sensors)
        self.phase_plot.setMenuEnabled(False)
        self.phase_plot.setMouseEnabled(x=False, y=False)
        self.phase_plot.hideButtons()
        self.phase_plot.showGrid(x=True, y=True)
        self.phase_plot.setLabel("bottom", "Depth (m)")
        self.phase_plot.setLabel("left", "Phase")
        self.phase_plot.setXRange(*self.depths.take((0, -1)))
        self.phase_plot.setYRange(-np.pi, np.pi)
        self.phase_plot.getAxis("left").setTicks(utils.pg_phase_ticks)

        self.ampl_curves = []
        self.phase_curves = []
        for i, sensor_id in enumerate(self.sensor_config.sensor):
            legend = "Sensor {}".format(sensor_id)
            pen = utils.pg_pen_cycler(i)
            ampl_curve = self.ampl_plot.plot(pen=pen, name=legend)
            phase_curve = self.phase_plot.plot(pen=pen)
            self.ampl_curves.append(ampl_curve)
            self.phase_curves.append(phase_curve)

        rate = self.sensor_config.update_rate
        xlabel = "Sweeps" if rate is None else "Time (s)"
        x_scale = 1.0 if rate is None else 1.0 / rate
        y_scale = self.depth_res
        x_offset = -self.processing_config.history_length * x_scale
        y_offset = self.depths[0] - 0.5 * self.depth_res
        is_single_sensor = len(self.sensor_config.sensor) == 1

        self.history_plots = []
        self.history_ims = []
        for i, sensor_id in enumerate(self.sensor_config.sensor):
            title = None if is_single_sensor else "Sensor {}".format(sensor_id)
            plot = win.addPlot(row=2, col=i, title=title)
            plot.setMenuEnabled(False)
            plot.setMouseEnabled(x=False, y=False)
            plot.hideButtons()
            plot.setLabel("bottom", xlabel)
            plot.setLabel("left", "Depth (m)")
            im = pg.ImageItem(autoDownsample=True)
            im.setLookupTable(utils.pg_mpl_cmap("viridis"))
            im.resetTransform()
            im.translate(x_offset, y_offset)
            im.scale(x_scale, y_scale)
            plot.addItem(im)
            self.history_plots.append(plot)
            self.history_ims.append(im)
    def setup(self, win):
        self.plots = []
        self.curves = []
        for i in range(self.depths.size):
            title = "{:.0f} cm".format(100 * self.depths[i])
            plot = win.addPlot(row=0, col=i, title=title)
            plot.setMenuEnabled(False)
            plot.setMouseEnabled(x=False, y=False)
            plot.hideButtons()
            plot.showGrid(x=True, y=True)
            plot.setYRange(0, 2**16)
            plot.hideAxis("left")
            plot.hideAxis("bottom")
            plot.plot(np.arange(self.sweeps_per_frame),
                      2**15 * np.ones(self.sweeps_per_frame))
            curve = plot.plot(pen=utils.pg_pen_cycler())
            self.plots.append(plot)
            self.curves.append(curve)

        self.ft_plot = win.addPlot(row=1, col=0, colspan=self.depths.size)
        self.ft_plot.setMenuEnabled(False)
        self.ft_plot.setMouseEnabled(x=False, y=False)
        self.ft_plot.hideButtons()
        self.ft_im = pg.ImageItem(autoDownsample=True)
        self.ft_im.setLookupTable(utils.pg_mpl_cmap("viridis"))
        self.ft_plot.addItem(self.ft_im)
        self.ft_plot.setLabel("bottom", "Depth (cm)")
        self.ft_plot.getAxis("bottom").setTickSpacing(
            6 * self.downsampling_factor, 6)

        self.setup_is_done = True
        self.update_processing_config()
    def setup(self, win):
        win.setWindowTitle("Acconeer sparse example")

        # For history images:
        rate = self.sensor_config.update_rate
        xlabel = "Frames" if rate is None else "Time (s)"
        x_scale = 1.0 if rate is None else 1.0 / rate
        y_scale = self.depth_res
        x_offset = -self.processing_config.history_length * x_scale
        y_offset = self.depths[0] - 0.5 * self.depth_res

        self.data_plots = []
        self.scatters = []
        self.data_history_ims = []
        self.presence_history_ims = []

        for i in range(len(self.sensor_config.sensor)):
            data_plot = win.addPlot(title="Sparse data", row=0, col=i)
            data_plot.setMenuEnabled(False)
            data_plot.showGrid(x=True, y=True)
            data_plot.setLabel("bottom", "Depth (m)")
            data_plot.setLabel("left", "Amplitude")
            scatter = pg.ScatterPlotItem(size=10)
            data_plot.addItem(scatter)

            cmap_cols = [
                "steelblue", "lightblue", "#f0f0f0", "moccasin", "darkorange"
            ]
            cmap = LinearSegmentedColormap.from_list("mycmap", cmap_cols)
            cmap._init()
            lut = (cmap._lut * 255).view(np.ndarray)

            data_history_plot = win.addPlot(title="Data history", row=1, col=i)
            data_history_plot.setMenuEnabled(False)
            data_history_im = pg.ImageItem(autoDownsample=True)
            data_history_im.setLookupTable(lut)
            data_history_plot.addItem(data_history_im)
            data_history_plot.setLabel("bottom", xlabel)
            data_history_plot.setLabel("left", "Depth (m)")

            presence_history_plot = win.addPlot(title="Movement history",
                                                row=2,
                                                col=i)
            presence_history_plot.setMenuEnabled(False)
            presence_history_im = pg.ImageItem(autoDownsample=True)
            presence_history_im.setLookupTable(utils.pg_mpl_cmap("viridis"))
            presence_history_plot.addItem(presence_history_im)
            presence_history_plot.setLabel("bottom", xlabel)
            presence_history_plot.setLabel("left", "Depth (m)")

            for im in [presence_history_im, data_history_im]:
                im.resetTransform()
                im.translate(x_offset, y_offset)
                im.scale(x_scale, y_scale)

            self.data_plots.append(data_plot)
            self.scatters.append(scatter)
            self.data_history_ims.append(data_history_im)
            self.presence_history_ims.append(presence_history_im)
    def setup(self, win):
        num_sensors = len(self.sensor_config.sensor)

        self.ampl_plot = win.addPlot(row=0, colspan=num_sensors)
        self.ampl_plot.setMenuEnabled(False)
        self.ampl_plot.setMouseEnabled(x=False, y=False)
        self.ampl_plot.hideButtons()
        self.ampl_plot.showGrid(x=True, y=True)
        self.ampl_plot.setLabel("bottom", "Depth (m)")
        self.ampl_plot.setLabel("left", "Amplitude")
        self.ampl_plot.setXRange(*self.depths.take((0, -1)))
        self.ampl_plot.setYRange(0, 1)  # To avoid rendering bug
        self.ampl_plot.addLegend(offset=(-10, 10))

        self.ampl_curves = []
        self.bg_curves = []
        self.peak_lines = []
        for i, sensor_id in enumerate(self.sensor_config.sensor):
            legend = "Sensor {}".format(sensor_id)
            ampl_curve = self.ampl_plot.plot(pen=utils.pg_pen_cycler(i), name=legend)
            bg_curve = self.ampl_plot.plot(pen=utils.pg_pen_cycler(i, style="--"))
            color_tuple = utils.hex_to_rgb_tuple(utils.color_cycler(i))
            peak_line = pg.InfiniteLine(pen=pg.mkPen(pg.mkColor(*color_tuple, 150), width=2))
            self.ampl_plot.addItem(peak_line)
            self.ampl_curves.append(ampl_curve)
            self.bg_curves.append(bg_curve)
            self.peak_lines.append(peak_line)

        bg = pg.mkColor(0xFF, 0xFF, 0xFF, 150)
        self.peak_text = pg.TextItem(anchor=(0, 1), color="k", fill=bg)
        self.peak_text.setPos(self.depths[0], 0)
        self.peak_text.setZValue(100)
        self.ampl_plot.addItem(self.peak_text)

        rate = self.sensor_config.update_rate
        xlabel = "Sweeps" if rate is None else "Time (s)"
        x_scale = 1.0 if rate is None else 1.0 / rate
        y_scale = self.depth_res
        x_offset = -self.processing_config.history_length * x_scale
        y_offset = self.depths[0] - 0.5 * self.depth_res
        is_single_sensor = len(self.sensor_config.sensor) == 1

        self.history_plots = []
        self.history_ims = []
        for i, sensor_id in enumerate(self.sensor_config.sensor):
            title = None if is_single_sensor else "Sensor {}".format(sensor_id)
            plot = win.addPlot(row=1, col=i, title=title)
            plot.setMenuEnabled(False)
            plot.setMouseEnabled(x=False, y=False)
            plot.hideButtons()
            plot.setLabel("bottom", xlabel)
            plot.setLabel("left", "Depth (m)")
            im = pg.ImageItem(autoDownsample=True)
            im.setLookupTable(utils.pg_mpl_cmap("viridis"))
            im.resetTransform()
            im.translate(x_offset, y_offset)
            im.scale(x_scale, y_scale)
            plot.addItem(im)
            self.history_plots.append(plot)
            self.history_ims.append(im)

        self.setup_is_done = True
        self.update_processing_config()
    def update(self, data):
        feat_map = None
        if data["ml_frame_data"] is not None:
            frame_data = data["ml_frame_data"]
            feat_map = frame_data["current_frame"]["feature_map"]
            frame_size = frame_data["frame_info"]["frame_size"]
            frame_pad = frame_data["frame_info"]["frame_pad"]
            time_series = frame_data["frame_info"].get("time_series", 1)
            frame_complete = frame_data["current_frame"]["frame_complete"]
            model_dimension = data["ml_frame_data"]["model_dimension"]
        else:
            return

        sensors = []
        if self.sensor_config is not None:
            sensors = self.sensor_config.sensor

        mode = data["sensor_config"].mode

        if self.first:
            self.first = False
            self.env_plot_max_y = 0
            s_buff = frame_size + 2 * frame_pad + time_series - 1
            self.feat_plot.resetTransform()
            if model_dimension > 1:
                self.feat_plot.translate(-frame_pad, 0)
            self.feat_plot_image.setXRange(-frame_pad, s_buff - frame_pad)

            self.border_left.setValue(0)
            self.border_right.setValue(frame_size + time_series - 1)

            self.border_left.show()
            self.border_right.show()

            nr_sensors = len(sensors)
            self.hist_plot_max_y = np.zeros(nr_sensors)

            lut = utils.pg_mpl_cmap("viridis")
            if mode == Mode.SPARSE:
                cmap_cols = ["steelblue", "lightblue", "#f0f0f0", "moccasin", "darkorange"]
                cmap = LinearSegmentedColormap.from_list("mycmap", cmap_cols)
                cmap._init()
                lut = (cmap._lut * 255).view(np.ndarray)

            for i in range(4):
                if (i + 1) in sensors:
                    self.hist_plot_images[i].show()
                    self.set_axis(data, self.hist_plot_images[i])
                    self.hist_plots[i].setLookupTable(lut)
                    if (i + 1) == sensors[0]:
                        self.hist_plot_images[i].setLabel("left", "Distance (mm)")
                else:
                    self.hist_plot_images[i].hide()

        for idx, sensor in enumerate(sensors):

            if mode == Mode.SPARSE:
                data_history_adj = data["hist_env"][idx, :, :].T - 2**15
                sign = np.sign(data_history_adj)
                data_history_adj = np.abs(data_history_adj)
                data_history_adj /= data_history_adj.max()
                data_history_adj = np.power(data_history_adj, 1/2.2)  # gamma correction
                data_history_adj *= sign
                self.hist_plots[sensor-1].updateImage(data_history_adj, levels=(-1.05, 1.05))
            else:
                max_val = np.max(data["env_ampl"][idx])

                ymax_level = min(1.5 * np.max(np.max(data["hist_env"][idx, :, :])),
                                 self.hist_plot_max_y[idx])

                if max_val > self.hist_plot_max_y[idx]:
                    self.hist_plot_max_y[idx] = 1.2 * max_val

                self.hist_plots[sensor-1].updateImage(
                    data["hist_env"][idx, :, :].T,
                    levels=(0, ymax_level)
                )

        if self.show_predictions and data.get("prediction") is not None:
            self.predictions_plot_window.show()
            prediction_text = "{} ({:.2f}%)".format(
                data["prediction"]["prediction"],
                data["prediction"]["confidence"] * 100,
            )
            self.predictions_plot_window.setTitle(prediction_text)
            predictions = data["prediction"]["label_predictions"]
            pred_num = data["prediction"]["number_labels"]
            pred_history = data["prediction_hist"]
            if len(self.prediction_plots) < pred_num:
                self.generate_predciction_plots(predictions, pred_num)

            if pred_history is not None:
                for idx in range(pred_num):
                    self.prediction_plots[idx].setData(pred_history[idx, :])

        detected = True
        if feat_map is None:
            detected = False

        if detected and not len(feat_map):
            detected = False

        if self.print_info_text:
            feature_nr = frame_data["current_frame"]["frame_nr"]
            self.feature_nr_text.setText("Feature: {}".format(feature_nr))
            if not detected:
                motion_score = frame_data.pop("motion_score", None)
                text = "Waiting..."
                if motion_score is not None:
                    if isinstance(motion_score, float):
                        motion_score = "{:.1f}".format(motion_score)
                    text = "Waiting.. (Motion score: {})".format(motion_score)
                self.detected_text.setText(text)
                return
            else:
                self.detected_text.setText("Collecting..")

        if not frame_complete:
            self.border_rolling.show()
            self.border_rolling.setValue(
                frame_data["current_frame"]["sweep_counter"] - frame_pad
            )
        else:
            self.border_rolling.hide()

        if feat_map is None:
            return

        self.feat_plot_image.setYRange(0, feat_map.shape[0])
        self.feat_plot_image.setXRange(0, feat_map.shape[1])

        map_max = 1.2 * np.max(feat_map)
        ymax_level = max(map_max, self.env_plot_max_y)

        # Disable post gamma stretch for the time being
        # TODO: Add option in GUI to enable.
        g = 1/1
        feat_map = 254/(ymax_level + 1.0e-9)**g * feat_map**g

        feat_map[feat_map > 254] = 254

        self.feat_plot.updateImage(feat_map.T, levels=(0, 256))

        if map_max > self.env_plot_max_y:
            self.env_plot_max_y = map_max
    def setup(self, win):
        win.setWindowTitle("Feature plotting")
        self.feat_plot_image = win.addPlot(row=0, col=0)
        self.feat_plot = pg.ImageItem()
        self.feat_plot.setAutoDownsample(True)
        self.feat_plot_image.addItem(self.feat_plot)
        self.feat_plot_image.setLabel("left", "Features")
        self.feat_plot_image.setLabel("bottom", "Sweeps")

        lut = utils.pg_mpl_cmap("viridis")
        self.feat_plot.setLookupTable(lut)

        self.feat_plot_image.setXRange(0, 40)
        self.feat_plot_image.setYRange(0, 1)

        self.border_right = pg.InfiniteLine(
            pos=0,
            angle=90,
            pen=pg.mkPen(width=2, style=QtCore.Qt.DotLine)
        )
        self.border_left = pg.InfiniteLine(
            pos=0,
            angle=90,
            pen=pg.mkPen(width=2, style=QtCore.Qt.DotLine)
        )
        self.border_rolling = pg.InfiniteLine(pos=0, angle=90, pen=pg.mkPen(width=2))

        self.detected_text = pg.TextItem(color="r", anchor=(0, 1))
        self.feature_nr_text = pg.TextItem(color="r", anchor=(0, 2))

        self.feat_plot_image.addItem(self.detected_text)
        self.feat_plot_image.addItem(self.feature_nr_text)
        self.feat_plot_image.addItem(self.border_left)
        self.feat_plot_image.addItem(self.border_right)
        self.feat_plot_image.addItem(self.border_rolling)

        self.border_left.hide()
        self.border_right.hide()
        self.border_rolling.hide()

        r = 1
        if self.show_predictions:
            r = 2
        self.history_plot_window = win.addLayout(row=r, col=0)

        self.envelope_plots = []
        self.peak_vlines = []
        self.clutter_plots = []
        self.hist_plot_images = []
        self.hist_plots = []
        self.hist_plot_peaks = []

        lut = utils.pg_mpl_cmap("viridis")

        for s in range(4):
            legend_text = "Sensor {}".format(s+1)
            hist_title = "History {}".format(legend_text)
            self.hist_plot_images.append(
                self.history_plot_window.addPlot(row=0, col=s, title=hist_title)
            )
            self.hist_plot_images[s].setLabel("bottom", "Time (s)")
            self.hist_plots.append(pg.ImageItem())
            self.hist_plots[s].setAutoDownsample(True)
            self.hist_plots[s].setLookupTable(lut)
            self.hist_plot_images[s].addItem(self.hist_plots[s])

        if self.show_predictions:
            self.predictions_plot_window = win.addPlot(row=1, col=0, title="Prediction results")
            self.predictions_plot_window.showGrid(x=True, y=True)
            self.predictions_plot_window.addLegend(offset=(-10, 10))
            self.predictions_plot_window.setYRange(0, 1)
            self.predictions_plot_window.setLabel("left", "Probability")
            self.predictions_plot_window.setLabel("bottom", "Iteration")

            self.prediction_plots = []
Exemple #8
0
def main():
    args = utils.ExampleArgumentParser(num_sens=1).parse_args()
    utils.config_logging(args)

    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)

    config = configs.EnvelopeServiceConfig()
    config.sensor = args.sensors
    config.update_rate = 30

    session_info = client.setup_session(config)

    start = session_info["range_start_m"]
    length = session_info["range_length_m"]
    num_depths = session_info["data_length"]
    step_length = session_info["step_length_m"]
    depths = np.linspace(start, start + length, num_depths)
    num_hist = 2 * int(round(config.update_rate))
    hist_data = np.zeros([num_hist, depths.size])
    smooth_max = utils.SmoothMax(config.update_rate)

    app = QtWidgets.QApplication([])
    pg.setConfigOption("background", "w")
    pg.setConfigOption("foreground", "k")
    pg.setConfigOptions(antialias=True)
    win = pg.GraphicsLayoutWidget()
    win.setWindowTitle("Acconeer PyQtGraph example")

    env_plot = win.addPlot(title="Envelope")
    env_plot.showGrid(x=True, y=True)
    env_plot.setLabel("bottom", "Depth (m)")
    env_plot.setLabel("left", "Amplitude")
    env_curve = env_plot.plot(pen=pg.mkPen("k", width=2))

    win.nextRow()
    hist_plot = win.addPlot()
    hist_plot.setLabel("bottom", "Time (s)")
    hist_plot.setLabel("left", "Depth (m)")
    hist_image_item = pg.ImageItem()
    hist_image_item.translate(-2, start)
    hist_image_item.scale(2 / num_hist, step_length)
    hist_plot.addItem(hist_image_item)

    # Get a nice colormap from matplotlib
    hist_image_item.setLookupTable(utils.pg_mpl_cmap("viridis"))

    win.show()

    interrupt_handler = utils.ExampleInterruptHandler()
    win.closeEvent = lambda _: interrupt_handler.force_signal_interrupt()
    print("Press Ctrl-C to end session")

    client.start_session()

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

        hist_data = np.roll(hist_data, -1, axis=0)
        hist_data[-1] = data

        env_curve.setData(depths, data)
        env_plot.setYRange(0, smooth_max.update(data))
        hist_image_item.updateImage(hist_data,
                                    levels=(0, np.max(hist_data) * 1.05))

        app.processEvents()

    print("Disconnecting...")
    app.closeAllWindows()
    client.disconnect()