def test_bug_no_dpi_no_aruco():
    #import matplotlib.text
    from sandbox import _calibration_dir
    _calibprojector = _calibration_dir + "my_projector_calibration.json"
    _calibsensor = _calibration_dir + "my_sensor_calibration.json"
    from sandbox.sensor import Sensor
    sensor = Sensor(calibsensor=_calibsensor, name="dummy")
    from sandbox.projector import Projector
    projector = Projector(calibprojector=_calibprojector, use_panel = False)
    # Initialize the aruco detection
    from sandbox.main_thread import MainThread
    main = MainThread(sensor=sensor, projector=projector)
    # Start the thread
    main.update()
    projector.trigger()
    def __init__(self,
                 calibprojector: str = None,
                 name: str = 'kinectv2',
                 **kwargs):
        # color map setup
        self.c_under = '#DBD053'
        self.c_over = '#DB3A34'
        # margin patches setup
        self.c_margin = '#084C61'
        self.margin_alpha = 0.5
        self.calibprojector = calibprojector
        self.sensor = Sensor(name=name,
                             invert=False,
                             clip_values=False,
                             gauss_filter=False,
                             **kwargs)
        self.projector = Projector(calibprojector=self.calibprojector,
                                   **kwargs)
        import copy
        self.cmap = copy.copy(mpl.cm.get_cmap("Greys_r"))
        self.cmap.set_over(self.c_over)
        self.cmap.set_under(self.c_under)
        self.cmap.set_bad('k')

        self._refresh_panel_frame()

        # fig = plt.figure()
        self.figure = Figure()
        self.ax_notebook_frame = plt.Axes(self.figure, [0., 0., 1., 1.])
        self.figure.add_axes(self.ax_notebook_frame)

        self.calib_notebook_frame = pn.pane.Matplotlib(self.figure,
                                                       tight=False,
                                                       height=300)
        plt.close()  # close figure to prevent inline display

        pn.state.add_periodic_callback(self.update_panel_frame, 5)
        # self.projector.panel.add_periodic_callback(self.update_panel_frame, 5)

        self.frame_raw = self.sensor.get_raw_frame()
        self.ax_notebook_frame.imshow(self.frame_raw,
                                      vmin=self.sensor.s_min,
                                      vmax=self.sensor.s_max,
                                      cmap=self.cmap,
                                      origin="lower",
                                      aspect="auto")
        self.calib_notebook_frame.param.trigger('object')
        self._create_widgets()
def test_with_gempy():
    from sandbox import _calibration_dir, _test_data
    file = np.load(_test_data['topo'] + "DEM1.npz")
    frame = file['arr_0']
    frame = frame + np.abs(frame.min())

    _calibprojector = _calibration_dir + "my_projector_calibration.json"
    _calibsensor = _calibration_dir + "my_sensor_calibration.json"
    from sandbox.sensor import Sensor
    sensor = Sensor(calibsensor=_calibsensor, name="dummy")
    from sandbox.projector import Projector
    projector = Projector(calibprojector=_calibprojector)
    # Initialize the aruco detection
    from sandbox.main_thread import MainThread
    mainT = MainThread(sensor=sensor, projector=projector)
    mainT.load_frame(frame)
    # Start the thread
    #mainT.run()
    from sandbox.modules.gempy import GemPyModule
    gpsb = GemPyModule(geo_model=None,
                       extent=sensor.extent,
                       box=sensor.physical_dimensions,
                       load_examples=True,
                       name_example=['Fault'])
    mainT.add_module(name='gempy', module=gpsb)
    mainT.update()
def test_topo_module():
    from sandbox import _calibration_dir, _test_data
    file = np.load(_test_data['topo'] + "DEM1.npz")
    frame = file['arr_0']
    frame = frame + np.abs(frame.min())

    _calibprojector = _calibration_dir + "my_projector_calibration.json"
    _calibsensor = _calibration_dir + "my_sensor_calibration.json"
    from sandbox.sensor import Sensor
    sensor = Sensor(calibsensor=_calibsensor, name="dummy")
    from sandbox.projector import Projector
    projector = Projector(calibprojector=_calibprojector)
    # Initialize the aruco detection
    from sandbox.main_thread import MainThread
    mainT = MainThread(sensor=sensor, projector=projector)
    mainT.load_frame(frame)

    # Start the thread
    #mainT.run()
    from sandbox.modules import TopoModule
    module = TopoModule(extent = extent)
    mainT.add_module(name='Topo', module=module)
    module.sea = True
    module.sea_contour = True
    mainT.update()
示例#5
0
def start_server(filename_projector: str = _calibration_dir +
                 "my_projector_calibration.json",
                 filename_sensor: str = _calibration_dir +
                 "my_sensor_calibration.json",
                 sensor_name='kinect_v2',
                 aruco_marker=True,
                 gempy_module=False,
                 **kwargs):
    #**projector_kwargs,
    #**sensor_kwargs,
    #**gempy_kwargs):

    from sandbox.projector import Projector
    from sandbox.sensor import Sensor
    from sandbox.markers import MarkerDetection

    if filename_projector is None:
        projector = Projector(use_panel=True, **kwargs)
    else:
        projector = Projector(calibprojector=filename_projector,
                              use_panel=True,
                              **kwargs)

    if filename_sensor is None:
        sensor = Sensor(name=sensor_name, **kwargs)
    else:
        sensor = Sensor(calibsensor=filename_sensor,
                        name=sensor_name,
                        **kwargs)

    if aruco_marker:
        aruco = MarkerDetection(sensor=sensor)
    else:
        aruco = None

    module = Sandbox(sensor=sensor,
                     projector=projector,
                     aruco=aruco,
                     gempy_module=gempy_module,
                     **kwargs)

    module.start()

    return module
def test_thread_kinectv2():
    from sandbox import _calibration_dir
    from sandbox.markers import MarkerDetection
    projector_2 = Projector(use_panel=True)
    sensor_2 = Sensor(name='kinect_v2', calibsensor=_calibration_dir+'my_sensor_calibration.json')
    aruco = MarkerDetection(sensor=sensor_2)
    smain = MainThread(sensor_2, projector_2, aruco)
    smain.sb_params['active_contours'] = True
    smain.sb_params['active_cmap'] = True
    smain.run()
def test_thread_module():
    from sandbox.modules import TopoModule, GradientModule
    proj = Projector(use_panel=True)
    sens = Sensor(name='kinect_v2', invert=True)
    smain = MainThread(sens, proj)

    topo = TopoModule(extent=sens.extent)
    grad = GradientModule(extent=sens.extent)

    smain.modules = [topo]
    smain.run()
def test_bug_no_dpi():
    from sandbox import _calibration_dir
    _calibprojector = _calibration_dir + "my_projector_calibration.json"
    _calibsensor = _calibration_dir + "my_sensor_calibration.json"
    from sandbox.sensor import Sensor
    sensor = Sensor(calibsensor=_calibsensor, name="kinect_v2")
    from sandbox.projector import Projector
    projector = Projector(calibprojector=_calibprojector)
    # Initialize the aruco detection
    from sandbox.markers import MarkerDetection
    aruco = MarkerDetection(sensor=sensor)
    from sandbox.main_thread import MainThread
    main = MainThread(sensor=sensor, projector=projector, aruco=aruco)
    # Start the thread
    main.run()
def test_widgets_calibration():
    projector = Projector(use_panel=True)
    widget = projector.calibrate_projector()
    widget.show()

    projector2 = Projector(calibprojector=calib_dir +
                           'test_projector_calibration.json',
                           use_panel=True)
    widget2 = projector2.calibrate_projector()
    widget2.show()
示例#10
0
def start_server(calibprojector: str = None,  # _calibration_dir + "my_projector_calibration.json",
                 calibsensor: str = None,  # _calibration_dir + "my_sensor_calibration.json",
                 sensor_name: str = None,
                 aruco_marker: bool = True,
                 kwargs_external_modules: dict = {},
                 kwargs_gempy_module: dict = {},
                 kwargs_projector: dict = {},
                 kwargs_sensor: dict = {},
                 kwargs_aruco: dict = {},
                 ):
    global name_sensor, p_width, p_height
    if sensor_name is None:
        sensor_name = name_sensor
    else:
        name_sensor = sensor_name

    from sandbox.projector import Projector
    if kwargs_projector.get("p_width") is not None:
        p_width = kwargs_projector.get("p_width")
    if kwargs_projector.get("p_height") is not None:
        p_height = kwargs_projector.get("p_height")
    projector = Projector(calibprojector=calibprojector, use_panel=True, **kwargs_projector)

    from sandbox.sensor import Sensor
    sensor = Sensor(calibsensor=calibsensor, name=sensor_name, **kwargs_sensor)

    if aruco_marker:
        from sandbox.markers import MarkerDetection
        aruco = MarkerDetection(sensor=sensor, **kwargs_aruco)
    else:
        aruco = None

    module = Sandbox(sensor=sensor,
                     projector=projector,
                     aruco=aruco,
                     kwargs_gempy_module=kwargs_gempy_module,
                     kwargs_external_modules=kwargs_external_modules)

    module.start()

    return module
示例#11
0
def test_delete_ax_image():
    projector = Projector(use_panel=False)
    projector.ax.plot([10, 20, 30], [20, 39, 48])
    projector.clear_axes()
示例#12
0
def test_trigger():
    projector = Projector(use_panel=True)
    projector.ax.plot([10, 20, 30], [20, 39, 48])
    projector.trigger()
示例#13
0
def test_open_panel_browser():
    projector = Projector(use_panel=False)
    projector.start_server()
示例#14
0
def test_save_load_calibration_projector():
    projector = Projector(use_panel=False)
    file = calib_dir + 'test_projector_calibration.json'
    projector.save_json(file=file)
    # now to test if it loads correctly the saved one
    projector2 = Projector(calibprojector=file, use_panel=False)
示例#15
0
def test_init_projector():
    projector = Projector(use_panel=False)
    assert projector.panel is not None
class CalibSensor:  # TODO: include automatic
    """Module to calibrate the sensor"""
    def __init__(self,
                 calibprojector: str = None,
                 name: str = 'kinectv2',
                 **kwargs):
        # color map setup
        self.c_under = '#DBD053'
        self.c_over = '#DB3A34'
        # margin patches setup
        self.c_margin = '#084C61'
        self.margin_alpha = 0.5
        self.calibprojector = calibprojector
        self.sensor = Sensor(name=name,
                             invert=False,
                             clip_values=False,
                             gauss_filter=False,
                             **kwargs)
        self.projector = Projector(calibprojector=self.calibprojector,
                                   **kwargs)
        import copy
        self.cmap = copy.copy(mpl.cm.get_cmap("Greys_r"))
        self.cmap.set_over(self.c_over)
        self.cmap.set_under(self.c_under)
        self.cmap.set_bad('k')

        self._refresh_panel_frame()

        # fig = plt.figure()
        self.figure = Figure()
        self.ax_notebook_frame = plt.Axes(self.figure, [0., 0., 1., 1.])
        self.figure.add_axes(self.ax_notebook_frame)

        self.calib_notebook_frame = pn.pane.Matplotlib(self.figure,
                                                       tight=False,
                                                       height=300)
        plt.close()  # close figure to prevent inline display

        pn.state.add_periodic_callback(self.update_panel_frame, 5)
        # self.projector.panel.add_periodic_callback(self.update_panel_frame, 5)

        self.frame_raw = self.sensor.get_raw_frame()
        self.ax_notebook_frame.imshow(self.frame_raw,
                                      vmin=self.sensor.s_min,
                                      vmax=self.sensor.s_max,
                                      cmap=self.cmap,
                                      origin="lower",
                                      aspect="auto")
        self.calib_notebook_frame.param.trigger('object')
        self._create_widgets()

    def _refresh_panel_frame(self):
        self.projector.ax.cla()
        self.fig_frame = self.projector.ax.imshow(self.sensor.get_frame(),
                                                  vmin=self.sensor.s_min,
                                                  vmax=self.sensor.s_max,
                                                  cmap=self.cmap,
                                                  origin="lower",
                                                  aspect="auto")

    def update(self):
        self.update_panel_frame(self.projector.ax)
        self.update_notebook_frame(self.ax_panel_frame)

    def update_panel_frame(self):
        frame = self.sensor.get_frame()
        self.fig_frame.set_data(frame)
        # self.projector.ax.set_xlim(0, self.sensor.s_frame_width)
        # self.projector.ax.set_ylim(0, self.sensor.s_frame_height)
        self.fig_frame.set_clim(vmin=self.sensor.s_min, vmax=self.sensor.s_max)
        self.projector.trigger()

    def update_notebook_frame(self):
        """ Adds margin patches to the current plot object.
        This is only useful when an uncropped dataframe is passed.
        """

        self.ax_notebook_frame.cla()
        self.ax_notebook_frame.imshow(self.frame_raw,
                                      vmin=self.sensor.s_min,
                                      vmax=self.sensor.s_max,
                                      cmap=self.cmap,
                                      origin="lower",
                                      aspect="auto")

        rec_t = plt.Rectangle((0, self.sensor.s_height - self.sensor.s_top),
                              self.sensor.s_width,
                              self.sensor.s_top,
                              fc=self.c_margin,
                              alpha=self.margin_alpha)
        rec_r = plt.Rectangle((self.sensor.s_width - self.sensor.s_right, 0),
                              self.sensor.s_right,
                              self.sensor.s_height,
                              fc=self.c_margin,
                              alpha=self.margin_alpha)
        rec_b = plt.Rectangle((0, 0),
                              self.sensor.s_width,
                              self.sensor.s_bottom,
                              fc=self.c_margin,
                              alpha=self.margin_alpha)
        rec_l = plt.Rectangle((0, 0),
                              self.sensor.s_left,
                              self.sensor.s_height,
                              fc=self.c_margin,
                              alpha=self.margin_alpha)
        self.ax_notebook_frame.add_patch(rec_t)
        self.ax_notebook_frame.add_patch(rec_r)
        self.ax_notebook_frame.add_patch(rec_b)
        self.ax_notebook_frame.add_patch(rec_l)
        self.calib_notebook_frame.param.trigger('object')

    def calibrate_sensor(self):
        widgets = pn.WidgetBox(
            '<b>Load a projector calibration file</b>',
            self._widget_json_filename_load_projector,
            self._widget_json_load_projector,
            '<b>Distance from edges (pixel)</b>',
            self._widget_s_top,
            self._widget_s_right,
            self._widget_s_bottom,
            self._widget_s_left,
            # self._widget_s_enable_auto_cropping,
            # self._widget_s_automatic_cropping,
            pn.layout.VSpacer(height=5),
            '<b>Distance from sensor (mm)</b>',
            self._widget_s_min,
            self._widget_s_max,
            self._widget_refresh_frame)
        box = pn.Column(
            '<b>Physical dimensions of the sandbox</b>',
            self._widget_box_width,
            self._widget_box_height,
        )
        save = pn.Column('<b>Save file</b>', self._widget_json_filename,
                         self._widget_json_save)

        rows = pn.Row(widgets, self.calib_notebook_frame)
        panel = pn.Column('## Sensor calibration', rows)
        tabs = pn.Tabs(('Calibration', panel), ("Box dimensions", box),
                       ("Save files", save))
        return tabs

    def _create_widgets(self):
        # sensor widgets and links

        self._widget_s_top = pn.widgets.IntSlider(name='Sensor top margin',
                                                  bar_color=self.c_margin,
                                                  value=self.sensor.s_top,
                                                  start=1,
                                                  end=self.sensor.s_height)
        self._widget_s_top.param.watch(self._callback_s_top,
                                       'value',
                                       onlychanged=False)

        self._widget_s_right = pn.widgets.IntSlider(name='Sensor right margin',
                                                    bar_color=self.c_margin,
                                                    value=self.sensor.s_right,
                                                    start=1,
                                                    end=self.sensor.s_width)
        self._widget_s_right.param.watch(self._callback_s_right,
                                         'value',
                                         onlychanged=False)

        self._widget_s_bottom = pn.widgets.IntSlider(
            name='Sensor bottom margin',
            bar_color=self.c_margin,
            value=self.sensor.s_bottom,
            start=1,
            end=self.sensor.s_height)
        self._widget_s_bottom.param.watch(self._callback_s_bottom,
                                          'value',
                                          onlychanged=False)

        self._widget_s_left = pn.widgets.IntSlider(name='Sensor left margin',
                                                   bar_color=self.c_margin,
                                                   value=self.sensor.s_left,
                                                   start=1,
                                                   end=self.sensor.s_width)
        self._widget_s_left.param.watch(self._callback_s_left,
                                        'value',
                                        onlychanged=False)

        self._widget_s_min = pn.widgets.IntSlider(name='Vertical minimum',
                                                  bar_color=self.c_under,
                                                  value=self.sensor.s_min,
                                                  start=0,
                                                  end=2000)
        self._widget_s_min.param.watch(self._callback_s_min,
                                       'value',
                                       onlychanged=False)

        self._widget_s_max = pn.widgets.IntSlider(name='Vertical maximum',
                                                  bar_color=self.c_over,
                                                  value=self.sensor.s_max,
                                                  start=0,
                                                  end=2000)
        self._widget_s_max.param.watch(self._callback_s_max,
                                       'value',
                                       onlychanged=False)

        # Auto cropping widgets:

        # self._widget_s_enable_auto_cropping = pn.widgets.Checkbox(name='Enable Automatic Cropping', value=False)
        # self._widget_s_enable_auto_cropping.param.watch(self._callback_enable_auto_cropping, 'value',
        #                                                onlychanged=False)

        # self._widget_s_automatic_cropping = pn.widgets.Button(name="Crop", button_type="success")
        # self._widget_s_automatic_cropping.param.watch(self._callback_automatic_cropping, 'clicks',
        #                                              onlychanged=False)

        # box widgets:

        # self._widget_s_enable_auto_calibration = CheckboxGroup(labels=["Enable Automatic Sensor Calibration"],
        #                                                                  active=[1])
        self._widget_box_width = pn.widgets.IntSlider(
            name='width of sandbox in mm',
            bar_color=self.c_margin,
            value=int(self.sensor.box_width),
            start=1,
            end=2000)
        self._widget_box_width.param.watch(self._callback_box_width,
                                           'value',
                                           onlychanged=False)

        # self._widget_s_automatic_calibration = pn.widgets.Toggle(name="Run", button_type="success")
        self._widget_box_height = pn.widgets.IntSlider(
            name='height of sandbox in mm',
            bar_color=self.c_margin,
            value=int(self.sensor.box_height),
            start=1,
            end=2000)
        self._widget_box_height.param.watch(self._callback_box_height,
                                            'value',
                                            onlychanged=False)

        # refresh button

        self._widget_refresh_frame = pn.widgets.Button(
            name='Refresh sensor frame\n(3 sec. delay)!')
        self._widget_refresh_frame.param.watch(self._callback_refresh_frame,
                                               'clicks',
                                               onlychanged=False)

        # save selection

        # Only for reading files --> Is there no location picker in panel widgets???
        # self._widget_json_location = pn.widgets.FileInput(name='JSON location')
        self._widget_json_filename = pn.widgets.TextInput(
            name='Choose a calibration filename:')
        self._widget_json_filename.param.watch(self._callback_json_filename,
                                               'value',
                                               onlychanged=False)
        self._widget_json_filename.value = _calibration_dir + 'my_sensor_calibration.json'

        self._widget_json_save = pn.widgets.Button(name='Save calibration')
        self._widget_json_save.param.watch(self._callback_json_save,
                                           'clicks',
                                           onlychanged=False)

        self._widget_json_filename_load_projector = pn.widgets.TextInput(
            name='Choose the projector calibration filename:')
        self._widget_json_filename_load_projector.param.watch(
            self._callback_json_filename_load_projector,
            'value',
            onlychanged=False)
        self._widget_json_filename_load_projector.value = _calibration_dir + 'my_projector_calibration.json'

        self._widget_json_load_projector = pn.widgets.Button(
            name='Load calibration')
        self._widget_json_load_projector.param.watch(
            self._callback_json_load_projector, 'clicks', onlychanged=False)

        return True

        # sensor callbacks
    def _callback_s_top(self, event):
        self.sensor.s_top = event.new
        # change plot and trigger panel update
        self.update_notebook_frame()

    def _callback_s_right(self, event):
        self.sensor.s_right = event.new
        # self._refresh_panel_frame() #TODO: dirty workaround
        self.update_notebook_frame()

    def _callback_s_bottom(self, event):
        self.sensor.s_bottom = event.new
        self.update_notebook_frame()

    def _callback_s_left(self, event):
        self.sensor.s_left = event.new
        # self._refresh_panel_frame()  # TODO: dirty workaround
        self.update_notebook_frame()

    def _callback_s_min(self, event):
        self.sensor.s_min = event.new
        # self._refresh_panel_frame()  # TODO: dirty workaround
        self.update_notebook_frame()

    def _callback_s_max(self, event):
        self.sensor.s_max = event.new
        # self._refresh_panel_frame()  # TODO: dirty workaround
        self.update_notebook_frame()

    def _callback_refresh_frame(self, event):
        plt.pause(3)
        # only here, get a new frame before updating the plot
        self.frame_raw = self.sensor.get_raw_frame()
        self.update_notebook_frame()

    def _callback_json_filename(self, event):
        self.sensor.json_filename = event.new

    def _callback_json_save(self, event):
        if self.sensor.json_filename is not None:
            self.sensor.save_json(file=self.sensor.json_filename)

    def _callback_json_filename_load_projector(self, event):
        self.calibprojector = event.new

    def _callback_json_load_projector(self, event):
        if self.calibprojector is not None:
            self.projector = Projector(self.calibprojector)

    def _callback_box_width(self, event):
        self.sensor.box_width = float(event.new)

    def _callback_box_height(self, event):
        self.sensor.box_height = float(event.new)

    # TODO: Make sense to enable this automatic calibration?
    """def _callback_enable_auto_calibration(self, event):
def test_run(): #TODO: runs but does not show contour lines
    projector_2 = Projector(use_panel=True)
    sensor_2 = Sensor(name='dummy')
    smain = MainThread(sensor_2, projector_2)
    smain.run()
def test_update():
    projector_2 = Projector(use_panel=True)
    sensor_2 = Sensor(name='dummy')
    smain = MainThread(sensor_2, projector_2)
    smain.update()
示例#19
0
def test_delete_points():
    projector = Projector(use_panel=False)
    line1 = projector.ax.plot([10, 20, 30], [20, 39, 48])
    projector.trigger()
    del line1
    projector.trigger()
示例#20
0
def calibrate_projector():
    from sandbox.projector import Projector
    proj = Projector(use_panel=True)
    widget = proj.calibrate_projector()
    widget.show()
    return proj
from sandbox import _test_data as test_data
from sandbox.main_thread import MainThread
from sandbox.projector import Projector
from sandbox.sensor import Sensor
import matplotlib.pyplot as plt
import numpy as np

file = np.load(test_data['topo'] + "DEM1.npz")
frame = file['arr_0']
frame = frame + np.abs(frame.min())
extent = np.asarray([0, frame.shape[1], 0, frame.shape[0], frame.min(), frame.max()])

projector = Projector(use_panel=False)
sensor = Sensor(name='dummy')

def test_init():
    smain = MainThread(sensor, projector)
    print(smain)

def test_update():
    projector_2 = Projector(use_panel=True)
    sensor_2 = Sensor(name='dummy')
    smain = MainThread(sensor_2, projector_2)
    smain.update()

def test_run(): #TODO: runs but does not show contour lines
    projector_2 = Projector(use_panel=True)
    sensor_2 = Sensor(name='dummy')
    smain = MainThread(sensor_2, projector_2)
    smain.run()
 def _callback_json_load_projector(self, event):
     if self.calibprojector is not None:
         self.projector = Projector(self.calibprojector)
示例#23
0
# sandbox_server.py
from bokeh.plotting import curdoc
from sandbox import _calibration_dir

_calibprojector = _calibration_dir + "my_projector_calibration.json"
_calibsensor = _calibration_dir + "my_sensor_calibration.json"

from sandbox.projector import Projector
from sandbox.sensor import Sensor
from sandbox.markers import MarkerDetection

projector = Projector(calibprojector=_calibprojector, use_panel=True)
sensor = Sensor(calibsensor=_calibsensor, name="kinect_v2")
aruco = MarkerDetection(sensor=sensor)

external_modules = dict(gempy_module=True,
                        gimli_module=True,
                        torch_module=True,
                        devito_module=True)

from sandbox.sandbox_api import Sandbox
module = Sandbox(sensor=sensor,
                 projector=projector,
                 aruco=aruco,
                 kwargs_external_modules=external_modules)

main_widget = module.show_widgets()

current = main_widget.get_root()
curdoc().add_root(current)