"""Frames acquired from the pipeline own their memory: nothing points into the SDK's buffers.""" import numpy as np import pytest pytest.importorskip("pyrealsense2") from src.backend.services.camera import RealSenseD435, _owned # noqa: E402 from tests.fake_realsense import FakeFrameset, FakePipeline, open_fake_camera # noqa: E402 def test_the_fake_hands_out_views_like_the_sdk(): """Control: without the copy, the driver's arrays WOULD share the SDK frame's memory.""" color = FakeFrameset(1).get_color_frame() assert np.shares_memory(np.asanyarray(color.get_data()), color.array) def test_captured_frame_does_not_share_memory_with_the_sdk_frame(): cam = RealSenseD435() pipe = FakePipeline() open_fake_camera(cam, pipe) try: frame = cam.capture() sdk = pipe.last_frameset assert frame is not None assert not np.shares_memory(frame.rgb, sdk.get_color_frame().array) assert not np.shares_memory(frame.depth, sdk.get_depth_frame().array) assert frame.rgb.base is None and frame.depth.base is None np.testing.assert_array_equal(frame.rgb, sdk.get_color_frame().array) # same pixels finally: cam.close() def test_owned_copies_views_only(): owned = np.zeros((4, 4), np.uint16) view = np.asanyarray(memoryview(owned)) assert _owned(owned) is owned copy = _owned(view) assert copy is not view and not np.shares_memory(copy, owned)