Mock OpenCVCamera
This commit is contained in:
@@ -337,6 +337,8 @@ class OpenCVCamera:
|
|||||||
return color_image
|
return color_image
|
||||||
|
|
||||||
def read_loop(self):
|
def read_loop(self):
|
||||||
|
# TODO(rcadene): implement safe exit for the threads,
|
||||||
|
# to avoid Segfault when main process finishes
|
||||||
while self.stop_event is None or not self.stop_event.is_set():
|
while self.stop_event is None or not self.stop_event.is_set():
|
||||||
self.color_image = self.read()
|
self.color_image = self.read()
|
||||||
|
|
||||||
|
|||||||
@@ -10,10 +10,9 @@ pytest -sx tests/test_cameras.py::test_camera
|
|||||||
import numpy as np
|
import numpy as np
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from lerobot import available_robots
|
|
||||||
from lerobot.common.robot_devices.cameras.opencv import OpenCVCamera, save_images_from_cameras
|
from lerobot.common.robot_devices.cameras.opencv import OpenCVCamera, save_images_from_cameras
|
||||||
from lerobot.common.robot_devices.utils import RobotDeviceAlreadyConnectedError, RobotDeviceNotConnectedError
|
from lerobot.common.robot_devices.utils import RobotDeviceAlreadyConnectedError, RobotDeviceNotConnectedError
|
||||||
from tests.utils import require_robot
|
from tests.utils import TEST_ROBOT_TYPES, require_robot
|
||||||
|
|
||||||
CAMERA_INDEX = 2
|
CAMERA_INDEX = 2
|
||||||
# Maximum absolute difference between two consecutive images recored by a camera.
|
# Maximum absolute difference between two consecutive images recored by a camera.
|
||||||
@@ -25,7 +24,7 @@ def compute_max_pixel_difference(first_image, second_image):
|
|||||||
return np.abs(first_image.astype(float) - second_image.astype(float)).max()
|
return np.abs(first_image.astype(float) - second_image.astype(float)).max()
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("robot_type", available_robots)
|
@pytest.mark.parametrize("robot_type", TEST_ROBOT_TYPES)
|
||||||
@require_robot
|
@require_robot
|
||||||
def test_camera(request, robot_type):
|
def test_camera(request, robot_type):
|
||||||
"""Test assumes that `camera.read()` returns the same image when called multiple times in a row.
|
"""Test assumes that `camera.read()` returns the same image when called multiple times in a row.
|
||||||
@@ -78,11 +77,11 @@ def test_camera(request, robot_type):
|
|||||||
camera.read()
|
camera.read()
|
||||||
color_image = camera.read()
|
color_image = camera.read()
|
||||||
async_color_image = camera.async_read()
|
async_color_image = camera.async_read()
|
||||||
print(
|
error_msg = (
|
||||||
"max_pixel_difference between read() and async_read()",
|
"max_pixel_difference between read() and async_read()",
|
||||||
compute_max_pixel_difference(color_image, async_color_image),
|
compute_max_pixel_difference(color_image, async_color_image),
|
||||||
)
|
)
|
||||||
assert np.allclose(color_image, async_color_image, rtol=1e-5, atol=MAX_PIXEL_DIFFERENCE)
|
assert np.allclose(color_image, async_color_image, rtol=1e-5, atol=MAX_PIXEL_DIFFERENCE), error_msg
|
||||||
|
|
||||||
# Test disconnecting
|
# Test disconnecting
|
||||||
camera.disconnect()
|
camera.disconnect()
|
||||||
@@ -105,12 +104,6 @@ def test_camera(request, robot_type):
|
|||||||
# TODO(rcadene): Add a test for a camera that doesnt support fps=60 and raises an OSError
|
# TODO(rcadene): Add a test for a camera that doesnt support fps=60 and raises an OSError
|
||||||
# TODO(rcadene): Add a test for a camera that supports fps=60
|
# TODO(rcadene): Add a test for a camera that supports fps=60
|
||||||
|
|
||||||
# Test fps=10 raises an OSError
|
|
||||||
camera = OpenCVCamera(CAMERA_INDEX, fps=10)
|
|
||||||
with pytest.raises(OSError):
|
|
||||||
camera.connect()
|
|
||||||
del camera
|
|
||||||
|
|
||||||
# Test width and height can be set
|
# Test width and height can be set
|
||||||
camera = OpenCVCamera(CAMERA_INDEX, fps=30, width=1280, height=720)
|
camera = OpenCVCamera(CAMERA_INDEX, fps=30, width=1280, height=720)
|
||||||
camera.connect()
|
camera.connect()
|
||||||
@@ -131,7 +124,7 @@ def test_camera(request, robot_type):
|
|||||||
del camera
|
del camera
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("robot_type", available_robots)
|
@pytest.mark.parametrize("robot_type", TEST_ROBOT_TYPES)
|
||||||
@require_robot
|
@require_robot
|
||||||
def test_save_images_from_cameras(tmpdir, request, robot_type):
|
def test_save_images_from_cameras(tmpdir, request, robot_type):
|
||||||
save_images_from_cameras(tmpdir, record_time_s=1)
|
save_images_from_cameras(tmpdir, record_time_s=1)
|
||||||
|
|||||||
@@ -16,9 +16,12 @@
|
|||||||
import platform
|
import platform
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
|
|
||||||
|
import cv2
|
||||||
|
import numpy as np
|
||||||
import pytest
|
import pytest
|
||||||
import torch
|
import torch
|
||||||
|
|
||||||
|
from lerobot import available_robots
|
||||||
from lerobot.common.utils.import_utils import is_package_available
|
from lerobot.common.utils.import_utils import is_package_available
|
||||||
|
|
||||||
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
||||||
@@ -28,6 +31,8 @@ DEFAULT_CONFIG_PATH = "lerobot/configs/default.yaml"
|
|||||||
|
|
||||||
ROBOT_CONFIG_PATH_TEMPLATE = "lerobot/configs/robot/{robot}.yaml"
|
ROBOT_CONFIG_PATH_TEMPLATE = "lerobot/configs/robot/{robot}.yaml"
|
||||||
|
|
||||||
|
TEST_ROBOT_TYPES = available_robots + [f"mocked_{robot_type}" for robot_type in available_robots]
|
||||||
|
|
||||||
|
|
||||||
def require_x86_64_kernel(func):
|
def require_x86_64_kernel(func):
|
||||||
"""
|
"""
|
||||||
@@ -175,11 +180,58 @@ def require_robot(func):
|
|||||||
robot_type = kwargs.get("robot_type")
|
robot_type = kwargs.get("robot_type")
|
||||||
|
|
||||||
if request is None:
|
if request is None:
|
||||||
raise ValueError("The 'request' fixture must be passed to the test function as a parameter.")
|
raise ValueError("The 'request' fixture must be an argument of the test function.")
|
||||||
|
|
||||||
|
# Run test with a monkeypatched version of the robot devices.
|
||||||
|
if robot_type.startswith("mocked_"):
|
||||||
|
kwargs["robot_type"] = robot_type.replace("mocked_", "")
|
||||||
|
|
||||||
|
monkeypatch = request.getfixturevalue("monkeypatch")
|
||||||
|
monkeypatch.setattr(cv2, "VideoCapture", MockVideoCapture)
|
||||||
|
|
||||||
|
# Run test with a real robot. Skip test if robot connection fails.
|
||||||
|
else:
|
||||||
|
# `is_robot_available` is defined in `tests/conftest.py`
|
||||||
|
if not request.getfixturevalue("is_robot_available"):
|
||||||
|
pytest.skip(f"A {robot_type} robot is not available.")
|
||||||
|
|
||||||
# The function `is_robot_available` is defined in `tests/conftest.py`
|
|
||||||
if not request.getfixturevalue("is_robot_available"):
|
|
||||||
pytest.skip(f"A {robot_type} robot is not available.")
|
|
||||||
return func(*args, **kwargs)
|
return func(*args, **kwargs)
|
||||||
|
|
||||||
return wrapper
|
return wrapper
|
||||||
|
|
||||||
|
|
||||||
|
class MockVideoCapture(cv2.VideoCapture):
|
||||||
|
image = {
|
||||||
|
"480x640": np.random.randint(0, 256, size=(480, 640, 3), dtype=np.uint8),
|
||||||
|
"720x1280": np.random.randint(0, 256, size=(720, 1280, 3), dtype=np.uint8),
|
||||||
|
}
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
super().__init__(*args, **kwargs)
|
||||||
|
self._mock_dict = {
|
||||||
|
cv2.CAP_PROP_FPS: 30,
|
||||||
|
cv2.CAP_PROP_FRAME_WIDTH: 640,
|
||||||
|
cv2.CAP_PROP_FRAME_HEIGHT: 480,
|
||||||
|
}
|
||||||
|
|
||||||
|
def isOpened(self): # noqa: N802
|
||||||
|
return True
|
||||||
|
|
||||||
|
def set(self, propId: int, value: float) -> bool: # noqa: N803
|
||||||
|
self._mock_dict[propId] = value
|
||||||
|
return True
|
||||||
|
|
||||||
|
def get(self, propId: int) -> float: # noqa: N803
|
||||||
|
value = self._mock_dict[propId]
|
||||||
|
if value == 0:
|
||||||
|
if propId == cv2.CAP_PROP_FRAME_HEIGHT:
|
||||||
|
value = 480
|
||||||
|
elif propId == cv2.CAP_PROP_FRAME_WIDTH:
|
||||||
|
value = 640
|
||||||
|
return value
|
||||||
|
|
||||||
|
def read(self):
|
||||||
|
h = self.get(cv2.CAP_PROP_FRAME_HEIGHT)
|
||||||
|
w = self.get(cv2.CAP_PROP_FRAME_WIDTH)
|
||||||
|
ret = True
|
||||||
|
return ret, self.image[f"{h}x{w}"]
|
||||||
|
|||||||
Reference in New Issue
Block a user