File size: 2,133 Bytes
e1c010e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Tests for the Vision Module — ObjectDetector.
"""

import numpy as np
import pytest


class TestObjectDetector:
    """Tests for ObjectDetector.detect() method."""

    def test_detect_returns_list(self):
        """Detection should return a list."""
        from src.vision.detector import ObjectDetector

        detector = ObjectDetector(model_name="yolov8n.pt", confidence=0.25)

        # Create a synthetic test image (solid blue, 480x640)
        image = np.zeros((480, 640, 3), dtype=np.uint8)
        image[:, :] = (200, 100, 50)

        result = detector.detect(image)
        assert isinstance(result, list), "detect() must return a list"

    def test_detect_result_schema(self):
        """Each detection should have label, confidence, and bbox keys."""
        from src.vision.detector import ObjectDetector

        detector = ObjectDetector(model_name="yolov8n.pt", confidence=0.25)

        # Use a real-ish image to maybe get detections
        image = np.random.randint(0, 255, (480, 640, 3), dtype=np.uint8)
        result = detector.detect(image)

        for det in result:
            assert "label" in det, "Detection must have 'label'"
            assert "confidence" in det, "Detection must have 'confidence'"
            assert "bbox" in det, "Detection must have 'bbox'"
            assert isinstance(det["label"], str)
            assert isinstance(det["confidence"], float)
            assert isinstance(det["bbox"], list)
            assert len(det["bbox"]) == 4

    def test_detect_invalid_image_raises(self):
        """Passing None should raise ValueError."""
        from src.vision.detector import ObjectDetector

        detector = ObjectDetector(model_name="yolov8n.pt", confidence=0.25)

        with pytest.raises(ValueError):
            detector.detect(None)

    def test_detect_1d_image_raises(self):
        """Passing a 1D array should raise ValueError."""
        from src.vision.detector import ObjectDetector

        detector = ObjectDetector(model_name="yolov8n.pt", confidence=0.25)

        with pytest.raises(ValueError):
            detector.detect(np.array([1, 2, 3]))