|
| 1 | +import numpy as np |
| 2 | +import pytest |
| 3 | + |
| 4 | +import supervision as sv |
| 5 | + |
| 6 | + |
| 7 | +def test_keypoints_from_rfdetr_detections() -> None: |
| 8 | + """Converts RF-DETR detections.data['keypoints'] into a KeyPoints object.""" |
| 9 | + detections = sv.Detections( |
| 10 | + xyxy=np.array([[0, 0, 10, 10], [10, 10, 20, 20]], dtype=np.float32), |
| 11 | + class_id=np.array([1, 3], dtype=int), |
| 12 | + data={ |
| 13 | + "keypoints": np.array( |
| 14 | + [ |
| 15 | + [[1.0, 2.0, 0.9], [3.0, 4.0, 0.8]], |
| 16 | + [[5.0, 6.0, 0.7], [7.0, 8.0, 0.6]], |
| 17 | + ], |
| 18 | + dtype=np.float32, |
| 19 | + ) |
| 20 | + }, |
| 21 | + ) |
| 22 | + |
| 23 | + key_points = sv.KeyPoints.from_rfdetr(detections) |
| 24 | + |
| 25 | + assert key_points.xy.shape == (2, 2, 2) |
| 26 | + assert key_points.confidence is not None |
| 27 | + assert key_points.confidence.shape == (2, 2) |
| 28 | + assert key_points.class_id is not None |
| 29 | + assert np.array_equal(key_points.class_id, np.array([1, 3], dtype=int)) |
| 30 | + |
| 31 | + |
| 32 | +def test_keypoints_from_rfdetr_missing_keypoints_raises_clear_error() -> None: |
| 33 | + """Missing detections.data['keypoints'] raises a clear conversion error.""" |
| 34 | + detections = sv.Detections( |
| 35 | + xyxy=np.array([[0, 0, 10, 10]], dtype=np.float32), |
| 36 | + class_id=np.array([0], dtype=int), |
| 37 | + ) |
| 38 | + |
| 39 | + with pytest.raises(ValueError, match="detections.data\\['keypoints'\\]"): |
| 40 | + sv.KeyPoints.from_rfdetr(detections) |
| 41 | + |
| 42 | + |
| 43 | +def test_keypoints_from_rfdetr_malformed_shape_raises_clear_error() -> None: |
| 44 | + """Malformed keypoints shape raises a clear conversion error.""" |
| 45 | + detections = sv.Detections( |
| 46 | + xyxy=np.array([[0, 0, 10, 10]], dtype=np.float32), |
| 47 | + class_id=np.array([0], dtype=int), |
| 48 | + data={"keypoints": np.array([[[1.0, 2.0]]], dtype=np.float32)}, |
| 49 | + ) |
| 50 | + |
| 51 | + with pytest.raises(ValueError, match="shape \\(N, K, 3\\)"): |
| 52 | + sv.KeyPoints.from_rfdetr(detections) |
| 53 | + |
| 54 | + |
| 55 | +def test_keypoint_annotator_uses_vertex_and_edge_rendering() -> None: |
| 56 | + """Converted RF-DETR keypoints are consumable by vertex and edge annotators.""" |
| 57 | + scene = np.zeros((32, 32, 3), dtype=np.uint8) |
| 58 | + detections = sv.Detections( |
| 59 | + xyxy=np.array([[0, 0, 10, 10]], dtype=np.float32), |
| 60 | + data={"keypoints": np.array([[[10.0, 10.0, 0.9], [20.0, 20.0, 0.8]]], dtype=np.float32)}, |
| 61 | + ) |
| 62 | + key_points = sv.KeyPoints.from_rfdetr(detections) |
| 63 | + |
| 64 | + scene = sv.VertexAnnotator().annotate(scene=scene, key_points=key_points) |
| 65 | + scene = sv.EdgeAnnotator(edges=[(1, 2)]).annotate(scene=scene, key_points=key_points) |
| 66 | + |
| 67 | + assert np.any(scene != 0) |
0 commit comments