From e385f0eefad751608d5db59a05badfd0c1572b88 Mon Sep 17 00:00:00 2001 From: Tobias Klockau Date: Mon, 4 Nov 2024 12:35:55 +0100 Subject: [PATCH] feat: IntrinsicsPinhole.from_json() --- raillabel/format/intrinsics_pinhole.py | 12 ++++++++++ .../format/test_intrinsics_pinhole.py | 23 +++++++++++++------ 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/raillabel/format/intrinsics_pinhole.py b/raillabel/format/intrinsics_pinhole.py index 76e03aa..380df11 100644 --- a/raillabel/format/intrinsics_pinhole.py +++ b/raillabel/format/intrinsics_pinhole.py @@ -5,6 +5,8 @@ from dataclasses import dataclass +from raillabel.json_format import JSONIntrinsicsPinhole + @dataclass class IntrinsicsPinhole: @@ -33,6 +35,16 @@ class IntrinsicsPinhole: width_px: int height_px: int + @classmethod + def from_json(cls, json: JSONIntrinsicsPinhole) -> IntrinsicsPinhole: + """Construct an instant of this class from RailLabel JSON data.""" + return IntrinsicsPinhole( + camera_matrix=json.camera_matrix, + distortion=json.distortion_coeffs, + width_px=json.width_px, + height_px=json.height_px, + ) + @classmethod def fromdict(cls, data_dict: dict) -> IntrinsicsPinhole: """Generate a IntrinsicsPinhole object from a dict. diff --git a/tests/test_raillabel/format/test_intrinsics_pinhole.py b/tests/test_raillabel/format/test_intrinsics_pinhole.py index 2f07c4f..fb41360 100644 --- a/tests/test_raillabel/format/test_intrinsics_pinhole.py +++ b/tests/test_raillabel/format/test_intrinsics_pinhole.py @@ -3,15 +3,10 @@ from __future__ import annotations -import os -import sys -from pathlib import Path - import pytest -sys.path.insert(1, str(Path(__file__).parent.parent.parent.parent.parent)) - from raillabel.format import IntrinsicsPinhole +from raillabel.json_format import JSONIntrinsicsPinhole # == Fixtures ========================= @@ -26,6 +21,16 @@ def intrinsics_pinhole_dict() -> dict: } +@pytest.fixture +def intrinsics_pinhole_json() -> dict: + return JSONIntrinsicsPinhole( + camera_matrix=[0.48, 0, 0.81, 0, 0, 0.16, 0.83, 0, 0, 0, 1, 0], + distortion_coeffs=[0.49, 0.69, 0.31, 0.81, 0.99], + width_px=2464, + height_px=1600, + ) + + @pytest.fixture def intrinsics_pinhole() -> dict: return IntrinsicsPinhole( @@ -39,6 +44,11 @@ def intrinsics_pinhole() -> dict: # == Tests ============================ +def test_from_json(intrinsics_pinhole, intrinsics_pinhole_json): + actual = IntrinsicsPinhole.from_json(intrinsics_pinhole_json) + assert actual == intrinsics_pinhole + + def test_fromdict(): intrinsics_pinhole = IntrinsicsPinhole.fromdict( { @@ -72,5 +82,4 @@ def test_asdict(): if __name__ == "__main__": - os.system("clear") pytest.main([__file__, "--disable-pytest-warnings", "--cache-clear", "-v"])