|
| 1 | +""" |
| 2 | +Tests for DLCLive core functionality - frame processing, cropping, etc. |
| 3 | +""" |
| 4 | +import pytest |
| 5 | +import numpy as np |
| 6 | +from pathlib import Path |
| 7 | +from unittest.mock import Mock, MagicMock, patch |
| 8 | +from dlclive import DLCLive |
| 9 | +from dlclive.exceptions import DLCLiveError |
| 10 | + |
| 11 | + |
| 12 | +class TestDLCLive: |
| 13 | + """Test DLCLive class core functionality""" |
| 14 | + |
| 15 | + @pytest.fixture |
| 16 | + def mock_runner(self): |
| 17 | + """Create a mock runner for testing""" |
| 18 | + runner = Mock() |
| 19 | + runner.cfg = {"test": "config"} |
| 20 | + runner.precision = "FP32" |
| 21 | + runner.init_inference.return_value = np.zeros((17, 3)) |
| 22 | + runner.get_pose.return_value = np.zeros((17, 3)) |
| 23 | + runner.close.return_value = None |
| 24 | + runner.read_config.return_value = {"test": "config"} |
| 25 | + return runner |
| 26 | + |
| 27 | + @pytest.fixture |
| 28 | + def sample_frame(self): |
| 29 | + """Create a sample frame for testing""" |
| 30 | + return np.random.randint(0, 255, (480, 640, 3), dtype=np.uint8) |
| 31 | + |
| 32 | + @patch('dlclive.factory.build_runner') |
| 33 | + def test_dlclive_initialization(self, mock_build_runner, mock_runner, tmp_path): |
| 34 | + """Test DLCLive initialization""" |
| 35 | + model_path = tmp_path / "model.pt" |
| 36 | + model_path.write_text("test") |
| 37 | + mock_build_runner.return_value = mock_runner |
| 38 | + |
| 39 | + dlc = DLCLive(model_path, model_type="pytorch") |
| 40 | + |
| 41 | + assert dlc.path == model_path |
| 42 | + assert dlc.model_type == "pytorch" |
| 43 | + assert dlc.is_initialized == False |
| 44 | + assert dlc.cropping is None |
| 45 | + assert dlc.dynamic == (False, 0.5, 10) |
| 46 | + assert dlc.processor is None |
| 47 | + |
| 48 | + @patch('dlclive.factory.build_runner') |
| 49 | + def test_dlclive_cfg_property(self, mock_build_runner, mock_runner, tmp_path): |
| 50 | + """Test accessing cfg property""" |
| 51 | + model_path = tmp_path / "model.pt" |
| 52 | + model_path.write_text("test") |
| 53 | + mock_build_runner.return_value = mock_runner |
| 54 | + |
| 55 | + dlc = DLCLive(model_path) |
| 56 | + assert dlc.cfg == {"test": "config"} |
| 57 | + |
| 58 | + @patch('dlclive.factory.build_runner') |
| 59 | + def test_dlclive_precision_property(self, mock_build_runner, mock_runner, tmp_path): |
| 60 | + """Test accessing precision property""" |
| 61 | + model_path = tmp_path / "model.pt" |
| 62 | + model_path.write_text("test") |
| 63 | + mock_build_runner.return_value = mock_runner |
| 64 | + |
| 65 | + dlc = DLCLive(model_path) |
| 66 | + assert dlc.precision == "FP32" |
| 67 | + |
| 68 | + @patch('dlclive.factory.build_runner') |
| 69 | + def test_dlclive_read_config(self, mock_build_runner, mock_runner, tmp_path): |
| 70 | + """Test reading configuration""" |
| 71 | + model_path = tmp_path / "model.pt" |
| 72 | + model_path.write_text("test") |
| 73 | + mock_build_runner.return_value = mock_runner |
| 74 | + |
| 75 | + dlc = DLCLive(model_path) |
| 76 | + config = dlc.read_config() |
| 77 | + assert config == {"test": "config"} |
| 78 | + |
| 79 | + @patch('dlclive.factory.build_runner') |
| 80 | + def test_dlclive_parameterization(self, mock_build_runner, mock_runner, tmp_path): |
| 81 | + """Test parameterization property""" |
| 82 | + model_path = tmp_path / "model.pt" |
| 83 | + model_path.write_text("test") |
| 84 | + mock_build_runner.return_value = mock_runner |
| 85 | + |
| 86 | + dlc = DLCLive(model_path, cropping=[10, 100, 20, 200]) |
| 87 | + params = dlc.parameterization |
| 88 | + |
| 89 | + assert "path" in params |
| 90 | + assert "cfg" in params |
| 91 | + assert "model_type" in params |
| 92 | + assert params["cropping"] == [10, 100, 20, 200] |
| 93 | + |
| 94 | + @patch('dlclive.factory.build_runner') |
| 95 | + @patch('dlclive.utils.img_to_rgb') |
| 96 | + def test_process_frame_cropping(self, mock_img_to_rgb, mock_build_runner, |
| 97 | + mock_runner, sample_frame, tmp_path): |
| 98 | + """Test frame processing with cropping""" |
| 99 | + model_path = tmp_path / "model.pt" |
| 100 | + model_path.write_text("test") |
| 101 | + mock_build_runner.return_value = mock_runner |
| 102 | + mock_img_to_rgb.side_effect = lambda x: x |
| 103 | + |
| 104 | + dlc = DLCLive(model_path, cropping=[10, 100, 20, 200]) |
| 105 | + result = dlc.process_frame(sample_frame) |
| 106 | + |
| 107 | + # Check that cropping was applied (result should be smaller) |
| 108 | + assert result.shape[0] == 180 # 200 - 20 |
| 109 | + assert result.shape[1] == 90 # 100 - 10 |
| 110 | + |
| 111 | + @patch('dlclive.factory.build_runner') |
| 112 | + @patch('dlclive.utils.resize_frame') |
| 113 | + @patch('dlclive.utils.img_to_rgb') |
| 114 | + def test_process_frame_resize(self, mock_img_to_rgb, mock_resize, |
| 115 | + mock_build_runner, mock_runner, sample_frame, tmp_path): |
| 116 | + """Test frame processing with resize""" |
| 117 | + model_path = tmp_path / "model.pt" |
| 118 | + model_path.write_text("test") |
| 119 | + mock_build_runner.return_value = mock_runner |
| 120 | + mock_img_to_rgb.side_effect = lambda x: x |
| 121 | + mock_resize.side_effect = lambda x, resize: x # No actual resize in test |
| 122 | + |
| 123 | + dlc = DLCLive(model_path, resize=0.5) |
| 124 | + dlc.process_frame(sample_frame) |
| 125 | + |
| 126 | + mock_resize.assert_called_once() |
| 127 | + |
| 128 | + @patch('dlclive.factory.build_runner') |
| 129 | + def test_process_frame_dynamic_cropping(self, mock_build_runner, mock_runner, |
| 130 | + sample_frame, tmp_path): |
| 131 | + """Test dynamic cropping functionality""" |
| 132 | + model_path = tmp_path / "model.pt" |
| 133 | + model_path.write_text("test") |
| 134 | + mock_build_runner.return_value = mock_runner |
| 135 | + |
| 136 | + # Create pose with detected body parts |
| 137 | + pose = np.array([[100, 150, 0.8], [120, 160, 0.9], [80, 140, 0.7]]) |
| 138 | + |
| 139 | + dlc = DLCLive(model_path, dynamic=(True, 0.5, 10)) |
| 140 | + dlc.pose = pose |
| 141 | + |
| 142 | + result = dlc.process_frame(sample_frame) |
| 143 | + |
| 144 | + # Check that dynamic cropping was applied |
| 145 | + assert dlc.dynamic_cropping is not None |
| 146 | + assert len(dlc.dynamic_cropping) == 4 |
| 147 | + |
| 148 | + @patch('dlclive.factory.build_runner') |
| 149 | + def test_init_inference_no_frame(self, mock_build_runner, mock_runner, tmp_path): |
| 150 | + """Test that init_inference raises error with no frame""" |
| 151 | + model_path = tmp_path / "model.pt" |
| 152 | + model_path.write_text("test") |
| 153 | + mock_build_runner.return_value = mock_runner |
| 154 | + |
| 155 | + dlc = DLCLive(model_path) |
| 156 | + |
| 157 | + with pytest.raises(DLCLiveError, match="No frame provided"): |
| 158 | + dlc.init_inference() |
| 159 | + |
| 160 | + @patch('dlclive.factory.build_runner') |
| 161 | + def test_get_pose_no_frame(self, mock_build_runner, mock_runner, tmp_path): |
| 162 | + """Test that get_pose raises error with no frame""" |
| 163 | + model_path = tmp_path / "model.pt" |
| 164 | + model_path.write_text("test") |
| 165 | + mock_build_runner.return_value = mock_runner |
| 166 | + |
| 167 | + dlc = DLCLive(model_path) |
| 168 | + |
| 169 | + with pytest.raises(DLCLiveError, match="No frame provided"): |
| 170 | + dlc.get_pose() |
| 171 | + |
| 172 | + @patch('dlclive.factory.build_runner') |
| 173 | + def test_close(self, mock_build_runner, mock_runner, tmp_path): |
| 174 | + """Test closing DLCLive instance""" |
| 175 | + model_path = tmp_path / "model.pt" |
| 176 | + model_path.write_text("test") |
| 177 | + mock_build_runner.return_value = mock_runner |
| 178 | + |
| 179 | + dlc = DLCLive(model_path) |
| 180 | + dlc.is_initialized = True |
| 181 | + dlc.close() |
| 182 | + |
| 183 | + assert dlc.is_initialized == False |
| 184 | + mock_runner.close.assert_called_once() |
| 185 | + |
| 186 | + @patch('dlclive.factory.build_runner') |
| 187 | + def test_post_process_pose_with_processor(self, mock_build_runner, mock_runner, |
| 188 | + sample_frame, tmp_path): |
| 189 | + """Test pose post-processing with processor""" |
| 190 | + model_path = tmp_path / "model.pt" |
| 191 | + model_path.write_text("test") |
| 192 | + mock_build_runner.return_value = mock_runner |
| 193 | + |
| 194 | + # Create mock processor |
| 195 | + mock_processor = Mock() |
| 196 | + mock_processor.process.return_value = np.ones((17, 3)) |
| 197 | + |
| 198 | + dlc = DLCLive(model_path, processor=mock_processor) |
| 199 | + dlc.pose = np.zeros((17, 3)) |
| 200 | + |
| 201 | + # Manually call _post_process_pose |
| 202 | + result = dlc._post_process_pose(sample_frame) |
| 203 | + |
| 204 | + mock_processor.process.assert_called_once() |
| 205 | + np.testing.assert_array_equal(result, np.ones((17, 3))) |
| 206 | + |
| 207 | + |
0 commit comments