All tests passing except test_control_robot.py
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
|
||||
|
||||
from pathlib import Path
|
||||
import numpy as np
|
||||
import pytest
|
||||
|
||||
@@ -7,17 +8,28 @@ from lerobot.common.robot_devices.cameras.opencv import OpenCVCamera
|
||||
from lerobot.common.robot_devices.utils import RobotDeviceNotConnectedError, RobotDeviceAlreadyConnectedError
|
||||
|
||||
|
||||
def test_camera():
|
||||
# Test instantiating with missing camera index raises an error
|
||||
with pytest.raises(ValueError):
|
||||
camera = OpenCVCamera()
|
||||
CAMERA_INDEX = 2
|
||||
# Maximum absolute difference between two consecutive images recored by a camera.
|
||||
# This value differs with respect to the camera.
|
||||
MAX_PIXEL_DIFFERENCE = 25
|
||||
|
||||
# Test instantiating with a wrong camera index raises an error
|
||||
with pytest.raises(ValueError):
|
||||
camera = OpenCVCamera(-1)
|
||||
def compute_max_pixel_difference(first_image, second_image):
|
||||
return np.abs(first_image.astype(float) - second_image.astype(float)).max()
|
||||
|
||||
|
||||
def test_camera():
|
||||
"""Test assumes that `camera.read()` returns the same image when called multiple times in a row.
|
||||
So the environment should not change (you shouldnt be in front of the camera) and the camera should not be moving.
|
||||
|
||||
Warning: The tests worked for a macbookpro camera, but I am getting assertion error (`np.allclose(color_image, async_color_image)`)
|
||||
for my iphone camera and my LG monitor camera.
|
||||
"""
|
||||
# TODO(rcadene): measure fps in nightly?
|
||||
# TODO(rcadene): test logs
|
||||
# TODO(rcadene): add compatibility with other camera APIs
|
||||
|
||||
# Test instantiating
|
||||
camera = OpenCVCamera(0)
|
||||
camera = OpenCVCamera(CAMERA_INDEX)
|
||||
|
||||
# Test reading, async reading, disconnecting before connecting raises an error
|
||||
with pytest.raises(RobotDeviceNotConnectedError):
|
||||
@@ -31,7 +43,7 @@ def test_camera():
|
||||
del camera
|
||||
|
||||
# Test connecting
|
||||
camera = OpenCVCamera(0)
|
||||
camera = OpenCVCamera(CAMERA_INDEX)
|
||||
camera.connect()
|
||||
assert camera.is_connected
|
||||
assert camera.fps is not None
|
||||
@@ -50,9 +62,14 @@ def test_camera():
|
||||
assert c == 3
|
||||
assert w > h
|
||||
|
||||
# Test reading asynchronously from the camera and image is similar
|
||||
# Test read and async_read outputs similar images
|
||||
# ...warming up as the first frames can be black
|
||||
for _ in range(30):
|
||||
camera.read()
|
||||
color_image = camera.read()
|
||||
async_color_image = camera.async_read()
|
||||
assert np.allclose(color_image, async_color_image)
|
||||
print("max_pixel_difference between read() and async_read()", compute_max_pixel_difference(color_image, async_color_image))
|
||||
assert np.allclose(color_image, async_color_image, rtol=1e-5, atol=MAX_PIXEL_DIFFERENCE)
|
||||
|
||||
# Test disconnecting
|
||||
camera.disconnect()
|
||||
@@ -60,27 +77,29 @@ def test_camera():
|
||||
assert camera.thread is None
|
||||
|
||||
# Test disconnecting with `__del__`
|
||||
camera = OpenCVCamera(0)
|
||||
camera = OpenCVCamera(CAMERA_INDEX)
|
||||
camera.connect()
|
||||
del camera
|
||||
|
||||
# Test acquiring a bgr image
|
||||
camera = OpenCVCamera(0, color="bgr")
|
||||
camera = OpenCVCamera(CAMERA_INDEX, color="bgr")
|
||||
camera.connect()
|
||||
assert camera.color == "bgr"
|
||||
bgr_color_image = camera.read()
|
||||
assert np.allclose(color_image, bgr_color_image[[2,1,0]])
|
||||
assert np.allclose(color_image, bgr_color_image[:, :, [2,1,0]], rtol=1e-5, atol=MAX_PIXEL_DIFFERENCE)
|
||||
del camera
|
||||
|
||||
# Test fps can be set
|
||||
camera = OpenCVCamera(0, fps=60)
|
||||
camera.connect()
|
||||
assert camera.fps == 60
|
||||
# TODO(rcadene): measure fps in nightly?
|
||||
# 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
|
||||
|
||||
# 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
|
||||
camera = OpenCVCamera(0, fps=30, width=1280, height=720)
|
||||
camera = OpenCVCamera(CAMERA_INDEX, fps=30, width=1280, height=720)
|
||||
camera.connect()
|
||||
assert camera.fps == 30
|
||||
assert camera.width == 1280
|
||||
@@ -92,7 +111,9 @@ def test_camera():
|
||||
assert c == 3
|
||||
del camera
|
||||
|
||||
|
||||
|
||||
|
||||
# Test not supported width and height raise an error
|
||||
camera = OpenCVCamera(CAMERA_INDEX, fps=30, width=0, height=0)
|
||||
with pytest.raises(OSError):
|
||||
camera.connect()
|
||||
del camera
|
||||
|
||||
|
||||
Reference in New Issue
Block a user