Skip to content

Commit

Permalink
feat: IntrinsicsPinhole.from_json()
Browse files Browse the repository at this point in the history
  • Loading branch information
tklockau committed Nov 4, 2024
1 parent 427f35e commit e385f0e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
12 changes: 12 additions & 0 deletions raillabel/format/intrinsics_pinhole.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

from dataclasses import dataclass

from raillabel.json_format import JSONIntrinsicsPinhole


@dataclass
class IntrinsicsPinhole:
Expand Down Expand Up @@ -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.
Expand Down
23 changes: 16 additions & 7 deletions tests/test_raillabel/format/test_intrinsics_pinhole.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 =========================

Expand All @@ -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(
Expand All @@ -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(
{
Expand Down Expand Up @@ -72,5 +82,4 @@ def test_asdict():


if __name__ == "__main__":
os.system("clear")
pytest.main([__file__, "--disable-pytest-warnings", "--cache-clear", "-v"])

0 comments on commit e385f0e

Please sign in to comment.