Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 2D presets #302

Merged
merged 1 commit into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 59 additions & 9 deletions modules/zivid/presets.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@

from zivid.settings import _to_settings
from zivid.camera_info import CameraInfo, _to_internal_camera_info
from zivid.settings2d import _to_settings2d


class Preset:
"""3D settings preset.
"""Settings preset.

Presets are pre-defined settings that are tuned for different camera models to perform optimally
under different conditions and use cases.
Expand All @@ -27,10 +28,21 @@ def __init__(self, impl):
Raises:
TypeError: If argument does not match the expected internal class.
"""
if not isinstance(impl, _zivid.presets.Preset):
if not any(
[
isinstance(impl, _zivid.presets.Preset),
isinstance(impl, _zivid.presets.Preset2D),
]
):
raise TypeError(
"Unsupported type for argument impl. Got {}, expected {}".format(
type(impl), _zivid.presets.Preset
type(impl),
" or ".join(
(
_zivid.presets.Preset.__name__,
_zivid.presets.Preset2D.__name__,
)
),
)
)
self.__impl = impl
Expand All @@ -53,9 +65,16 @@ def settings(self):
recommended to save them to a YML file and load as needed.

Returns:
The settings of the preset.
The settings of the preset. The settings will be an instance of Settings if the preset is a 3D preset,
or an instance of Settings2D if the preset is a 2D preset.
"""
return _to_settings(self.__impl.settings())

settings = self.__impl.settings()

if isinstance(settings, _zivid.Settings2D):
return _to_settings2d(settings)

return _to_settings(settings)

def __str__(self):
return str(self.__impl)
Expand All @@ -64,8 +83,8 @@ def __str__(self):
class Category:
"""Preset category.

This class cannot be initialized directly by the end-user. Use the categories function to
obtain a list of categories available for a camera.
This class cannot be initialized directly by the end-user. Use the categories or categories2d functions to obtain a
list of categories available for a camera.
"""

def __init__(self, impl):
Expand All @@ -79,10 +98,21 @@ def __init__(self, impl):
Raises:
TypeError: If argument does not match the expected internal class.
"""
if not isinstance(impl, _zivid.presets.Category):
if not any(
[
isinstance(impl, _zivid.presets.Category),
isinstance(impl, _zivid.presets.Category2D),
]
):
raise TypeError(
"Unsupported type for argument impl. Got {}, expected {}".format(
type(impl), _zivid.presets.Category
type(impl),
" or ".join(
(
_zivid.presets.Category.__name__,
_zivid.presets.Category2D.__name__,
)
),
)
)
self.__impl = impl
Expand Down Expand Up @@ -135,3 +165,23 @@ def categories(model):
_to_internal_camera_info(CameraInfo(model=model)).model
)
]


def categories2d(model):
"""Get available 2D preset categories for the specified camera model.

See `categories` for more information.

Args:
model: The model for the camera whose 2D preset categories should be returned. This value can
be obtained from CameraInfo.model.

Returns:
The available 2D categories for the specified camera model.
"""
return [
Category(c)
for c in _zivid.presets.categories2d(
_to_internal_camera_info(CameraInfo(model=model)).model
)
]
13 changes: 13 additions & 0 deletions src/Presets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,34 @@ namespace ZividPython
pyClass.def("name", &Zivid::Presets::Preset::name).def("settings", &Zivid::Presets::Preset::settings);
}

void wrapClass(pybind11::class_<Zivid::Presets::Preset2D> pyClass)
{
pyClass.def("name", &Zivid::Presets::Preset2D::name).def("settings", &Zivid::Presets::Preset2D::settings);
}

void wrapClass(pybind11::class_<Zivid::Presets::Category> pyClass)
{
pyClass.def("name", &Zivid::Presets::Category::name).def("presets", &Zivid::Presets::Category::presets);
}

void wrapClass(pybind11::class_<Zivid::Presets::Category2D> pyClass)
{
pyClass.def("name", &Zivid::Presets::Category2D::name).def("presets", &Zivid::Presets::Category2D::presets);
}

namespace Presets
{
void wrapAsSubmodule(pybind11::module &dest)
{
using namespace Zivid::Presets;

ZIVID_PYTHON_WRAP_CLASS(dest, Preset);
ZIVID_PYTHON_WRAP_CLASS(dest, Preset2D);
ZIVID_PYTHON_WRAP_CLASS(dest, Category);
ZIVID_PYTHON_WRAP_CLASS(dest, Category2D);

dest.def("categories", &categories);
dest.def("categories2d", &categories2D);
}
} // namespace Presets

Expand Down
29 changes: 28 additions & 1 deletion test/test_presets.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest


def test_presets(application): # pylint: disable=unused-argument
def test_3d_presets(application): # pylint: disable=unused-argument
import zivid

for model in zivid.CameraInfo.Model.valid_values():
Expand All @@ -26,3 +26,30 @@ def test_presets(application): # pylint: disable=unused-argument
assert isinstance(preset, zivid.presets.Preset)
assert preset.name != ""
assert isinstance(preset.settings, zivid.Settings)


def test_2d_presets(application): # pylint: disable=unused-argument
import zivid

for model in zivid.CameraInfo.Model.valid_values():
if model in [
zivid.CameraInfo.Model.zividOnePlusLarge,
zivid.CameraInfo.Model.zividOnePlusMedium,
zivid.CameraInfo.Model.zividOnePlusSmall,
]:
with pytest.raises(
RuntimeError,
match=f"Internal error: The camera model '{model}' is not supported by this version of Zivid SDK.",
):
zivid.presets.categories2d(model)
else:
categories = zivid.presets.categories2d(model)
assert categories
for category in categories:
assert isinstance(category, zivid.presets.Category)
assert category.name != ""
assert category.presets
for preset in category.presets:
assert isinstance(preset, zivid.presets.Preset)
assert preset.name != ""
assert isinstance(preset.settings, zivid.Settings2D)
Loading