File size: 4,611 Bytes
65da30d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
from reachy_mini.media.camera_constants import ReachyMiniCamSpecs, ArduCamResolution, MujocoCameraSpecs
from reachy_mini.media.media_manager import MediaManager, MediaBackend
import numpy as np
import pytest
import time
# import tempfile
# import cv2


@pytest.mark.video
def test_get_frame_exists() -> None:
    """Test that a frame can be retrieved from the camera and is not None."""
    media = MediaManager(backend=MediaBackend.DEFAULT)
    frame = media.get_frame()
    assert frame is not None, "No frame was retrieved from the camera."
    assert isinstance(frame, np.ndarray), "Frame is not a numpy array."
    assert frame.size > 0, "Frame is empty."
    assert frame.shape[0] == media.camera.resolution[1] and frame.shape[1] == media.camera.resolution[0], f"Frame has incorrect dimensions: {frame.shape}"

    # with tempfile.NamedTemporaryFile(suffix='.png', delete=False) as tmp_file:
    #    cv2.imwrite(tmp_file.name, frame)
    #    print(f"Frame saved for inspection: {tmp_file.name}")    

@pytest.mark.video
def test_get_frame_exists_all_resolutions() -> None:
    """Test that a frame can be retrieved from the camera for all supported resolutions."""
    media = MediaManager(backend=MediaBackend.DEFAULT)
    for resolution in media.camera.camera_specs.available_resolutions:
        media.camera.set_resolution(resolution)
        frame = media.get_frame()
        assert frame is not None, f"No frame was retrieved from the camera at resolution {resolution}."
        assert isinstance(frame, np.ndarray), f"Frame is not a numpy array at resolution {resolution}."
        assert frame.size > 0, f"Frame is empty at resolution {resolution}."
        assert frame.shape[0] == resolution.value[1] and frame.shape[1] == resolution.value[0], f"Frame has incorrect dimensions at resolution {resolution}: {frame.shape}" 

@pytest.mark.video
def test_change_resolution_errors() -> None:
    """Test that changing resolution raises a runtime error if not allowed."""
    media = MediaManager(backend=MediaBackend.DEFAULT)
    media.camera.camera_specs = None
    with pytest.raises(RuntimeError):
        media.camera.set_resolution(ArduCamResolution.R1280x720)

    media.camera.camera_specs = MujocoCameraSpecs()
    with pytest.raises(RuntimeError):
        media.camera.set_resolution(ArduCamResolution.R1280x720)

    media.camera.camera_specs = ReachyMiniCamSpecs()
    with pytest.raises(ValueError):
        media.camera.set_resolution(ArduCamResolution.R1280x720)


@pytest.mark.video_gstreamer
def test_get_frame_exists_gstreamer() -> None:
    """Test that a frame can be retrieved from the camera and is not None."""
    media = MediaManager(backend=MediaBackend.GSTREAMER)
    time.sleep(2)  # Give some time for the camera to initialize
    frame = media.get_frame()
    assert frame is not None, "No frame was retrieved from the camera."
    assert isinstance(frame, np.ndarray), "Frame is not a numpy array."
    assert frame.size > 0, "Frame is empty."
    assert frame.shape[0] == media.camera.resolution[1] and frame.shape[1] == media.camera.resolution[0], f"Frame has incorrect dimensions: {frame.shape}"

@pytest.mark.video_gstreamer
def test_get_frame_exists_all_resolutions_gstreamer() -> None:
    """Test that a frame can be retrieved from the camera for all supported resolutions."""
    media = MediaManager(backend=MediaBackend.GSTREAMER)
    time.sleep(2)  # Give some time for the camera to initialize

    for resolution in media.camera.camera_specs.available_resolutions:
        media.camera.close()
        media.camera.set_resolution(resolution)
        media.camera.open()
        time.sleep(2)  # Give some time for the camera to adjust to new resolution
        frame = media.get_frame()
        assert frame is not None, f"No frame was retrieved from the camera at resolution {resolution}."
        assert isinstance(frame, np.ndarray), f"Frame is not a numpy array at resolution {resolution}."
        assert frame.size > 0, f"Frame is empty at resolution {resolution}."
        assert frame.shape[0] == resolution.value[1] and frame.shape[1] == resolution.value[0], f"Frame has incorrect dimensions at resolution {resolution}: {frame.shape}"

@pytest.mark.video_gstreamer
def test_change_resolution_errors_gstreamer() -> None:
    """Test that changing resolution raises a runtime error if not allowed."""
    media = MediaManager(backend=MediaBackend.GSTREAMER)
    time.sleep(1)  # Give some time for the camera to initialize
    with pytest.raises(RuntimeError):
        media.camera.set_resolution(media.camera.camera_specs.available_resolutions[0])