test: captured frames do not share memory with the SDK frame
The fake frames now hand out a buffer (memoryview) like pyrealsense2's BufData, so np.asanyarray() of it is a view, as on the robot; a control test proves it. Then: the captured rgb/depth share no memory with the fake SDK frame and own their data (without the fix both shared it), and _owned() copies views only. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
55e0a991b4
commit
a8e380ed69
2 files changed
+47
-1
No files matched your search
@@ -0,0 +1,39 @@
|
||||
"""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)
|
||||
Reference in new issue
Block a user