|
| 1 | +import numpy as np |
| 2 | +from PIL import Image |
| 3 | +from unittest.mock import MagicMock, patch |
| 4 | +import pytest |
| 5 | +import supervision as sv |
| 6 | +from perceptionmetrics.utils.image import draw_detections |
| 7 | + |
| 8 | + |
| 9 | +def test_draw_detections_calls_supervision(): |
| 10 | + # Setup |
| 11 | + img = Image.new("RGB", (100, 100)) |
| 12 | + boxes = np.array([[0, 0, 10, 10]]) |
| 13 | + class_ids = np.array([0]) |
| 14 | + class_names = ["cat"] |
| 15 | + scores = np.array([0.9]) |
| 16 | + |
| 17 | + mock_box_annotator = MagicMock() |
| 18 | + mock_box_annotator.annotate.return_value = np.zeros((100, 100, 3), dtype=np.uint8) |
| 19 | + |
| 20 | + with patch("perceptionmetrics.utils.image.sv.Detections") as mock_detections, patch( |
| 21 | + "perceptionmetrics.utils.image.sv.BoxAnnotator", return_value=mock_box_annotator |
| 22 | + ), patch("perceptionmetrics.utils.image.sv.Color"), patch( |
| 23 | + "perceptionmetrics.utils.image.sv.ColorPalette" |
| 24 | + ): |
| 25 | + |
| 26 | + draw_detections(img, boxes, class_ids, class_names, scores) |
| 27 | + |
| 28 | + assert mock_detections.called |
| 29 | + args, kwargs = mock_detections.call_args |
| 30 | + assert np.array_equal(kwargs["xyxy"], boxes) |
| 31 | + assert np.array_equal(kwargs["class_id"], class_ids) |
| 32 | + assert np.array_equal(kwargs["confidence"], scores) |
| 33 | + |
| 34 | + assert mock_box_annotator.annotate.called |
| 35 | + |
| 36 | + |
| 37 | +def test_draw_detections_label_construction(): |
| 38 | + # Setup |
| 39 | + img = Image.new("RGB", (100, 100)) |
| 40 | + boxes = np.array([[0, 0, 10, 10], [10, 10, 20, 20]]) |
| 41 | + class_ids = np.array([0, 1]) |
| 42 | + class_names = ["cat", "dog"] |
| 43 | + scores = np.array([0.9, 0.8]) |
| 44 | + |
| 45 | + # First BoxAnnotator (older style) will fail on annotate |
| 46 | + mock_box_annotator_old = MagicMock() |
| 47 | + mock_box_annotator_old.annotate.side_effect = TypeError( |
| 48 | + "Simulation of mismatching arguments" |
| 49 | + ) |
| 50 | + |
| 51 | + # Second BoxAnnotator (modern style) will succeed |
| 52 | + mock_box_annotator_new = MagicMock() |
| 53 | + mock_box_annotator_new.annotate.return_value = np.zeros( |
| 54 | + (100, 100, 3), dtype=np.uint8 |
| 55 | + ) |
| 56 | + |
| 57 | + mock_label_annotator = MagicMock() |
| 58 | + mock_label_annotator.annotate.return_value = np.zeros((100, 100, 3), dtype=np.uint8) |
| 59 | + |
| 60 | + with patch( |
| 61 | + "perceptionmetrics.utils.image.sv.BoxAnnotator", |
| 62 | + side_effect=[mock_box_annotator_old, mock_box_annotator_new], |
| 63 | + ), patch( |
| 64 | + "perceptionmetrics.utils.image.sv.LabelAnnotator", |
| 65 | + return_value=mock_label_annotator, |
| 66 | + ), patch( |
| 67 | + "perceptionmetrics.utils.image.sv.Color" |
| 68 | + ), patch( |
| 69 | + "perceptionmetrics.utils.image.sv.ColorPalette" |
| 70 | + ): |
| 71 | + |
| 72 | + draw_detections(img, boxes, class_ids, class_names, scores) |
| 73 | + |
| 74 | + # Check labels passed to LabelAnnotator |
| 75 | + assert mock_label_annotator.annotate.called |
| 76 | + args, kwargs = mock_label_annotator.annotate.call_args |
| 77 | + labels = kwargs.get("labels") |
| 78 | + assert labels == ["cat: 0.90", "dog: 0.80"] |
| 79 | + |
| 80 | + |
| 81 | +def test_draw_detections_missing_names(): |
| 82 | + img = Image.new("RGB", (100, 100)) |
| 83 | + boxes = np.array([[0, 0, 10, 10]]) |
| 84 | + class_ids = np.array([5]) |
| 85 | + class_names = [] # No name for ID 5 |
| 86 | + |
| 87 | + mock_box_annotator_old = MagicMock() |
| 88 | + mock_box_annotator_old.annotate.side_effect = TypeError() |
| 89 | + mock_box_annotator_new = MagicMock() |
| 90 | + mock_box_annotator_new.annotate.return_value = np.zeros( |
| 91 | + (100, 100, 3), dtype=np.uint8 |
| 92 | + ) |
| 93 | + |
| 94 | + mock_label_annotator = MagicMock() |
| 95 | + mock_label_annotator.annotate.return_value = np.zeros((100, 100, 3), dtype=np.uint8) |
| 96 | + |
| 97 | + with patch( |
| 98 | + "perceptionmetrics.utils.image.sv.BoxAnnotator", |
| 99 | + side_effect=[mock_box_annotator_old, mock_box_annotator_new], |
| 100 | + ), patch( |
| 101 | + "perceptionmetrics.utils.image.sv.LabelAnnotator", |
| 102 | + return_value=mock_label_annotator, |
| 103 | + ), patch( |
| 104 | + "perceptionmetrics.utils.image.sv.Color" |
| 105 | + ), patch( |
| 106 | + "perceptionmetrics.utils.image.sv.ColorPalette" |
| 107 | + ): |
| 108 | + |
| 109 | + draw_detections(img, boxes, class_ids, class_names) |
| 110 | + |
| 111 | + assert mock_label_annotator.annotate.called |
| 112 | + args, kwargs = mock_label_annotator.annotate.call_args |
| 113 | + labels = kwargs.get("labels") |
| 114 | + assert labels == ["5"] |
0 commit comments