Skip to content
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
5 changes: 3 additions & 2 deletions src/machinevisiontoolbox/Camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -3568,8 +3568,9 @@ def estpose(
:type method: str, optional
:param frame: estimate pose with respect to frame "world" [default] or "camera"
:type frame: str, optional
:return: pose of target frame relative to the world frame
:rtype: :class:`~spatialmath.pose3d.SE3`
:return: pose of target frame relative to the world frame, or ``None`` if
the algorithm finds no solution
:rtype: :class:`~spatialmath.pose3d.SE3` or None

Using a set of points defining some object with respect to its own frame {B}, and
a set of image-plane projections, estimate the pose of {B} with respect to the world
Expand Down
13 changes: 13 additions & 0 deletions src/machinevisiontoolbox/blocks/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,18 @@ def __init__(
:return: a ESTPOSE_P block
:rtype: EstPose_p instance
:raises ValueError: if ``camera`` is None or ``frame`` is not ``"world"`` or ``"camera"``
:raises RuntimeError: when the diagram is evaluated, if the pose estimation finds
no solution for the points. bdsim prints the block, the time and the input,
and stops the run with a "Fatal failure" error caused by this one.

.. versionchanged:: 2.5.0
``frame`` is now passed to :meth:`~machinevisiontoolbox.Camera.CentralCamera.estpose`.
Previously it was accepted but ignored, so the pose was always given with
respect to the world frame.

.. versionchanged:: 2.5.0
A pose estimate that fails raises :class:`RuntimeError`. It used to
output ``None``, which the next block received as its input.
"""
if camera is None:
raise ValueError("camera is not defined")
Expand All @@ -189,6 +195,13 @@ def __init__(
def output(self, t: float, inports: list[Any], x: np.ndarray) -> list[Any]:
p = inports[0]
T = self.camera.estpose(self.P, p, method=self.method, frame=self.frame)
if T is None:
# bdsim prints the block, the time and the input, then stops the run
# with a "Fatal failure" error caused by this one
raise RuntimeError(
f"pose estimation failed: method {self.method!r} found no pose "
f"for {p.shape[1]} points"
)
return [T]


Expand Down
53 changes: 53 additions & 0 deletions tests/test_blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,3 +287,56 @@ def test_frames_differ(self):
def test_bad_frame_raises(self):
with pytest.raises(ValueError):
EstPose_p(self.cam, self.P, frame="robot")


class TestEstPoseFailure:
"""EstPose_p raises when the pose estimate fails, and does not output None."""

def setup_method(self):
self.cam = CentralCamera.Default()
self.P = mkcube(0.2)
# every image point at one position: IPPE finds no pose for this
p = self.cam.project_point(self.P, objpose=SE3.Trans(0.1, 0.2, 0.5))
self.p_degenerate = np.tile(p[:, :1], (1, self.P.shape[1]))
self.p = p

def test_the_camera_gives_none_for_the_degenerate_points(self):
# confirms that the points used below are a failure of the estimate
assert self.cam.estpose(self.P, self.p_degenerate, method="ippe") is None

def test_failure_raises_not_none(self):
block = EstPose_p(self.cam, self.P, method="ippe")
with pytest.raises(RuntimeError, match="pose estimation failed"):
block.test_output(self.p_degenerate)

def test_message_names_the_method_and_the_point_count(self):
block = EstPose_p(self.cam, self.P, method="ippe")
with pytest.raises(RuntimeError, match=r"'ippe'.*8 points"):
block.test_output(self.p_degenerate)

def test_success_is_unchanged(self):
T = EstPose_p(self.cam, self.P).test_output(self.p)[0]
assert T is not None

def test_in_a_simulation_bdsim_reports_the_block(self, capsys):
sim = bdsim.BDSim(
animation=False,
graphics=False,
hold=False,
progress=False,
verbose=False,
sysargs=False,
)
bd = sim.blockdiagram()
points = bd.CONSTANT(self.p_degenerate)
estpose = bd.ESTPOSE_P(self.cam, self.P, method="ippe")
bd.connect(points, estpose)
# bdsim evaluates the diagram once while it compiles, so the failure is
# raised there, before any simulation steps
with pytest.raises(RuntimeError) as raised:
bd.compile()
# bdsim reports "Fatal failure", caused by our error, and prints which
# block and time it came from
assert "pose estimation failed" in str(raised.value.__cause__)
shown = capsys.readouterr()
assert "estpose_p.0" in shown.out + shown.err
Loading