Esempio n. 1
0
    def test_creation(
        self, generic_temp_folder, data, cfg_extension
    ):  # TODO: toml 0.10.2 needed! toml has a bug otherwise

        generic_temp_folder = Path(generic_temp_folder)

        sample_dict = data["data"]
        to_be_raplaced_keys = data[
            "to_be_replaced"
        ]  # , _, _, to_be_raplaced_keys = data
        volatile_dict = copy.deepcopy(sample_dict)

        subtitutions_values = {}
        for k in to_be_raplaced_keys:
            random_name = k + f".{cfg_extension}"
            random_name = DirectiveAT.generate_directive_string("import", [random_name])
            subtitutions_values[random_name] = pydash.get(volatile_dict, k)

            pydash.set_(volatile_dict, k, random_name)

        output_cfg_filename = generic_temp_folder / f"out_config.{cfg_extension}"
        output_cfg_filename2 = generic_temp_folder / f"out_config2.{cfg_extension}"

        subtitutions_values[str(output_cfg_filename)] = volatile_dict

        saved_cfgs = []
        for directive_value, d in subtitutions_values.items():
            directive = DirectiveAT(value=directive_value)
            if directive.valid:
                output_filename = generic_temp_folder / directive.args[0]
            else:
                output_filename = generic_temp_folder / directive_value
            store_cfg(output_filename, d)
            saved_cfgs.append(output_filename)

        yconf = XConfig(output_cfg_filename)
        yconf.save_to(output_cfg_filename2)

        yconf_reloaded = XConfig(output_cfg_filename2)

        with pytest.raises(NotImplementedError):
            yconf.save_to(str(output_cfg_filename2) + "#IMPOSSIBLE.EXTENSION")

        assert not DeepDiff(yconf.to_dict(), sample_dict)
        assert not DeepDiff(yconf_reloaded.to_dict(), sample_dict)
        assert not DeepDiff(
            XConfig.from_dict(yconf_reloaded.to_dict()).to_dict(),
            yconf_reloaded.to_dict(),
        )
        assert len(XConfig.from_dict(yconf_reloaded.to_dict())) > len(
            sample_dict
        )  # YConf contains 2 more private keys!

        # remove last cfg file
        saved_cfgs[0].unlink()

        with pytest.raises(OSError):
            yconf = XConfig(output_cfg_filename)
Esempio n. 2
0
    def test_copy(self, sample_configurations_data):

        for config in sample_configurations_data:
            filename = config["filename"]
            xcfg = XConfig(filename=filename, no_deep_parse=True)
            print("#" * 10)
            print(filename)
            print(xcfg)
            xcfg_copy = xcfg.copy()
            xcfg_copy.deep_parse()
            xcfg_correct = XConfig(filename=filename)
            assert not DeepDiff(xcfg_correct.to_dict(), xcfg_copy.to_dict())
            assert xcfg_copy == xcfg_correct
Esempio n. 3
0
def acquire(
    calibration: Path,
    device_cfg: Path,
    output_folder: Path,
    scale_factor: int,
    disparity: bool,
    max_disparity: int,
    max_depth: int,
    max_frames: int,
    skip: int,
):
    if device_cfg is None:
        device_cfg = oakeye.data_folder / "device" / "device.yml"
    device_cfg = XConfig(device_cfg)
    device = OakDeviceFactory().create(device_cfg)
    keys = ["left", "center", "right", "depth", "disparityCL", "disparityCR"]
    acquirer = DeviceAcquirer(device)
    if calibration is not None:
        calib = XConfig(calibration)
        acquirer = RectifiedAcquirer(acquirer, calib)
    if disparity:
        acquirer = DisparityAcquirer(acquirer, disp_diff=max_disparity)
    acquirer = GuiAcquirer(
        acquirer,
        keys,
        scale_factor=scale_factor,
        ranges={
            "disparityCL": [0, max_disparity],
            "disparityCR": [0, max_disparity],
            "depth": [0, max_depth],
        },
    )
    dataset = acquirer(max_frames=max_frames, skip=skip)
    device.close()
    if output_folder is not None:
        ext_map = {
            "left": "png",
            "center": "png",
            "right": "png",
            "depth": "png",
            "disparityCL": "png",
            "disparityCR": "png",
            "device": "yml",
        }
        root_files = ["device"]
        for d in dataset:
            d["device"] = device_cfg.to_dict()
        UnderfolderWriter(output_folder,
                          root_files_keys=root_files,
                          extensions_map=ext_map)(SamplesSequence(dataset))
Esempio n. 4
0
def compile(
    configuration_file: str,
    output_file: str,
    check: bool,
    options: Sequence[Tuple[str, str]],
):

    from choixe.configurations import XConfig
    import rich

    try:
        cfg = XConfig(filename=configuration_file)
    except Exception as e:
        rich.print(f"[red]Invalid configuration file: {e}[/red]")
        import sys

        sys.exit(1)

    placeholders = cfg.available_placeholders()
    for key, value in options:
        if key in placeholders:
            placeholder = placeholders[key]
            cfg.deep_set(key, placeholder.cast(value))

    if check:
        cfg.check_available_placeholders(close_app=True)

    cfg.save_to(output_file)
Esempio n. 5
0
    def create_from_file(cls, filename: str) -> any:
        """Creates an object starting from its serialized representation in a file loadable
        as XConfig (yml, json, etc.)

        :param filename: config filename
        :type filename: str
        :return: hydrated object
        :rtype: any
        """
        return cls.create(XConfig(filename))
Esempio n. 6
0
    def test_prompt(self, monkeypatch, tmpdir_factory):

        d = {
            "a_int": "@int(a_int)",
            "another_int": "@int(a_int)",
            "a_float": "@float(a_float)",
            "a_bool": "@bool(a_bool)",
            "a_str1": "@str(a_str1)",
            "a_str2": "@str(a_str2,hello,hi)",
            "a_str3": "@str(a_str3,one,two,three,default=one)",
        }

        fills = {
            "a_int": 2,
            "another_int": 666,
            "a_float": 2.22,
            "a_bool": True,
            "a_str1": "hello",
            "a_str2": "hi",
            "a_str3": "three",
        }

        def custom_prompt(questions):
            return fills

        # old_prompt = XInquirer._system_prompt
        monkeypatch.setattr(XInquirer,
                            "_system_prompt",
                            custom_prompt,
                            raising=True)

        # from dict
        xconfig = XConfig.from_dict(d)
        xconfig.set_schema(Schema({}))
        new_xconfig = XInquirer.prompt(xconfig, close_app=False)

        assert len(new_xconfig.available_placeholders()) == 0
        assert xconfig._filename == new_xconfig._filename
        assert xconfig.get_schema() == new_xconfig.get_schema()

        # from file
        filename = tmpdir_factory.mktemp("configuration") / "cfg.yml"
        xconfig.save_to(filename)
        xconfig = XConfig(filename=filename)
        new_xconfig = XInquirer.prompt(xconfig, close_app=False)

        assert len(new_xconfig.available_placeholders()) == 0
        assert xconfig._filename == new_xconfig._filename
        assert xconfig.get_schema() == new_xconfig.get_schema()
Esempio n. 7
0
    def test_chunks_view(self, sample_configurations_data):

        for config in sample_configurations_data:
            filename = config["filename"]
            xcfg = XConfig(filename=filename, no_deep_parse=True)

            assert len(xcfg.chunks_as_lists()) > 0
            assert len(xcfg.chunks_as_lists()) == len(xcfg.chunks_as_tuples())
            assert len(xcfg.chunks()) == len(xcfg.chunks_as_tuples())

            ls = xcfg.chunks_as_lists()
            ts = xcfg.chunks_as_tuples()

            for idx in range(len(ls)):
                l, _ = ls[idx]
                t, _ = ts[idx]
                for kidx in range(len(l)):
                    assert l[kidx] == t[kidx]
Esempio n. 8
0
def check(configuration_file, close):

    from choixe.configurations import XConfig
    import rich

    try:
        cfg = XConfig(filename=configuration_file)
        rich.print(cfg.to_dict())
    except Exception as e:
        rich.print(f"[red]Invalid configuration file: {e}[/red]")
        import sys

        sys.exit(1)

    rich.print("[green]Configuration is good![/green]")

    # d = cfg.to_dict(discard_private_qualifiers=False)
    cfg.check_available_placeholders(close_app=close)
Esempio n. 9
0
    def test_creation_root_replace(self, generic_temp_folder, data, cfg_extension):
        """Test with double @@ -> replace value with content of content"""
        generic_temp_folder = Path(generic_temp_folder)

        sample_dict = data["data"]
        to_be_raplaced_keys = data[
            "to_be_replaced"
        ]  # , _, _, to_be_raplaced_keys = data
        volatile_dict = copy.deepcopy(sample_dict)

        subtitutions_values = {}
        for k in to_be_raplaced_keys:
            random_name = k + f".{cfg_extension}"
            random_name = DirectiveAT.generate_directive_string(
                "import_root", [random_name]
            )
            subtitutions_values[random_name] = pydash.get(volatile_dict, k)

            pydash.set_(volatile_dict, k, random_name)

        output_cfg_filename = generic_temp_folder / f"out_config.{cfg_extension}"

        subtitutions_values[str(output_cfg_filename)] = volatile_dict

        saved_cfgs = []
        for directive_value, d in subtitutions_values.items():
            directive = DirectiveAT(value=directive_value)
            if directive.valid:
                output_filename = generic_temp_folder / directive.args[0]
            else:
                output_filename = generic_temp_folder / directive_value
            store_cfg(output_filename, d)
            saved_cfgs.append(output_filename)

        yconf = XConfig(output_cfg_filename)

        assert DeepDiff(yconf.to_dict(), sample_dict)
Esempio n. 10
0
from choixe.configurations import XConfig
from rich import print

cfg = XConfig(filename="cfg.yml")
print(cfg.to_dict())
Esempio n. 11
0

# Create Object
bundle = MyBundle(
    name="custom_bundle",
    images=[
        MyImage(name="image", image_size=[800, 600]),
        MyImage(name="mask", image_size=[256, 256]),
        MyImage(name="crop", image_size=[32, 32]),
    ],
)


# Serialization
bundle_serialized = bundle.serialize()
rich.print("Serialized object:", bundle_serialized)

# IO Write
cfg = XConfig.from_dict(bundle_serialized)
cfg_filename = Path(tempfile.mkdtemp()) / "cfg.yml"
cfg.save_to(cfg_filename)
rich.print("Stored config in:", cfg_filename)

# IO Read
cfg = XConfig(filename=cfg_filename)
hydrated_bundle: MyBundle = Spook.create(cfg)
rich.print("Hydrated object:", hydrated_bundle.serialize())

for image in hydrated_bundle.images:
    image.my_method()
Esempio n. 12
0
def generate_labels(
    data_folder,
    scale_factor,
    distance_threshold,
    ransac_n,
    num_iterations,
    offset,
    offset_up,
    eps,
    min_points,
    min_area,
):

    VISUALIZER_SIZE = [800, 800]
    try:
        from screeninfo import get_monitors

        max_height = int(get_monitors()[0].height)
        max_width = int(get_monitors()[0].width * 0.48)
        VISUALIZER_SIZE = [max_width, max_height]
    except:
        pass

    # Check for filenames
    image_filename = data_folder / "image.png"
    disparity_filename = data_folder / "disparity.png"
    calibration_filename = data_folder / "calibration.yml"

    for _filename in [
            image_filename, disparity_filename, calibration_filename
    ]:
        assert _filename.exists(), f"Filename: {str(_filename)} not found!"

    # Load camera calibration data
    calibration = XConfig(calibration_filename)
    camera_matrix = calibration["camera_matrix"]["center"]
    baseline = calibration["baseline"]["center_right"]
    focal = camera_matrix[0][0]

    # Load image/disparity
    image = np.array(imageio.imread(str(image_filename)))
    if len(image.shape) == 2:
        image = cv2.cvtColor(image, cv2.COLOR_GRAY2RGB)
    disparity = np.array(imageio.imread(str(disparity_filename)))

    # Compute depth from disparity
    depth = (baseline * focal / disparity * 1000).astype(np.float32)

    # Segmentation Pipeline
    segmenter = Segmenter(image, depth, camera_matrix, f=scale_factor)
    segmentation_output = segmenter.segment(
        distance_threshold,
        ransac_n,
        num_iterations,
        offset,
        offset_up,
        eps,
        min_points,
        min_area,
    )

    # Visualization loop control
    visualization_running = True

    def stop_visualization(event=None):
        nonlocal visualization_running
        visualization_running = False

    # Visualize 3D PointCloud with plane segmentation
    visualizer_3d = create_3d_visualizer(
        point_cloud=segmentation_output["full_point_cloud"],
        inliers=segmentation_output["plane_inliers"],
        visualizer_size=VISUALIZER_SIZE,
    )
    visualizer_3d.register_key_callback(81, stop_visualization)

    # Visualize 2D Final Output
    colored_seg_mask = segmentation_output["colored_final_mask"]
    plane_seg_mask = segmentation_output["colored_plane_mask"]
    crops = segmentation_output["crops"]
    _, _, bounding_boxes = zip(*crops)
    out = create_2d_stacked_image(image, colored_seg_mask, plane_seg_mask,
                                  bounding_boxes)
    out = cv2.cvtColor(out, cv2.COLOR_RGB2BGR)
    cv2.namedWindow("output", cv2.WINDOW_NORMAL)
    cv2.resizeWindow("output", VISUALIZER_SIZE[0], VISUALIZER_SIZE[1])
    cv2.moveWindow("output", int(VISUALIZER_SIZE[0] * 1.1), 0)

    # Visualization loop
    while visualization_running:
        visualizer_3d.poll_events()
        visualizer_3d.update_renderer()
        cv2.imshow("output", out)
        c = cv2.waitKey(10)
        if ord("q") == c:
            stop_visualization()

    # teardown visualizers
    visualizer_3d.destroy_window()
    cv2.destroyWindow("output")
Esempio n. 13
0
def calibrate(
    input_folder: Path,
    output_folder: Path,
    board_cfg: Path,
    device_cfg: Path,
    rectification: Path,
    scale_factor: int,
    save: bool,
):
    acquire_corners = False
    if input_folder is None:
        acquire_corners = True
    if board_cfg is None:
        board_cfg = oakeye.data_folder / "board" / "chessboard.yml"
    if device_cfg is None:
        device_cfg = oakeye.data_folder / "device" / "device.yml"

    board: Board = Spook.create(XConfig(board_cfg))

    if acquire_corners:
        device_cfg = XConfig(device_cfg)
        device = OakDeviceFactory().create(device_cfg)
        keys = ["left", "center", "right"]
        acquirer = DeviceAcquirer(device)
        if rectification is not None:
            calib = XConfig(filename=rectification)
            acquirer = RectifiedAcquirer(acquirer, calib)
        acquirer = CornerAcquirer(acquirer,
                                  keys,
                                  board,
                                  scale_factor=scale_factor)
        dataset = acquirer()
        device.close()
        if save:
            ext_map = {
                "left": "png",
                "center": "png",
                "right": "png",
                "depth": "png",
                "board": "yml",
                "device": "yml",
            }
            root_files = ["board", "device"]
            for d in dataset:
                d["board"] = board.serialize()
                d["device"] = device_cfg.to_dict()
            UnderfolderWriter(output_folder,
                              root_files_keys=root_files,
                              extensions_map=ext_map)(SamplesSequence(dataset))
    else:
        dataset = UnderfolderReader(input_folder)

    if len(dataset) < 1:
        raise ClickException(f"Input dataset must contain at least one sample")

    left = []
    center = []
    right = []
    for sample in dataset:
        left.append(sample["left"])
        center.append(sample["center"])
        right.append(sample["right"])
    calib = board.trinocular_calibration(left, center, right)

    output_folder.mkdir(parents=True, exist_ok=True)
    XConfig(plain_dict=calib).save_to(output_folder / "calibration.yml")
Esempio n. 14
0
from choixe.configurations import XConfig
import rich

conf = XConfig(filename="cfg.yml")
conf_to_replace = XConfig(filename="cfg_deep.yml")

rich.print(conf.to_dict())
rich.print(conf_to_replace.to_dict())

# Deep Update
conf.deep_update(conf_to_replace)
rich.print(conf.to_dict())