diff --git a/supervision/detection/core.py b/supervision/detection/core.py index 1f04746d..7d142000 100644 --- a/supervision/detection/core.py +++ b/supervision/detection/core.py @@ -1135,10 +1135,7 @@ class Detections: return cls(xyxy=xyxy, mask=mask, data=data) - if ( - vlm == VLM.GOOGLE_GEMINI_2_0 - or vlm == VLM.GOOGLE_GEMINI_2_5 - ): + if vlm == VLM.GOOGLE_GEMINI_2_0 or vlm == VLM.GOOGLE_GEMINI_2_5: xyxy, class_id, class_name = from_google_gemini(result, **kwargs) data = {CLASS_NAME_DATA_FIELD: class_name} return cls(xyxy=xyxy, class_id=class_id, data=data) diff --git a/supervision/detection/vlm.py b/supervision/detection/vlm.py index 8271b778..022060f6 100644 --- a/supervision/detection/vlm.py +++ b/supervision/detection/vlm.py @@ -427,7 +427,6 @@ def from_google_gemini( return xyxy, class_id, class_name - def from_moondream( result: dict, resolution_wh: Tuple[int, int], diff --git a/test/detection/test_vlm.py b/test/detection/test_vlm.py index 3e6be1d0..c6529969 100644 --- a/test/detection/test_vlm.py +++ b/test/detection/test_vlm.py @@ -497,3 +497,103 @@ def test_from_google_gemini( np.testing.assert_array_equal(xyxy, expected_results[0]) np.testing.assert_array_equal(class_id, expected_results[1]) np.testing.assert_array_equal(class_name, expected_results[2]) + + +@pytest.mark.parametrize( + "exception, result, resolution_wh, expected_results", + [ + ( + does_not_raise(), + {}, + (640, 480), + np.empty((0, 4)), + ), # empty dict + ( + does_not_raise(), + {"objects": []}, + (640, 480), + np.empty((0, 4)), + ), # empty objects list + ( + does_not_raise(), + {"objects": "not a list"}, + (640, 480), + np.empty((0, 4)), + ), # objects is not a list + ( + does_not_raise(), + { + "objects": [ + {"x_min": 0.1, "y_min": 0.2, "x_max": 0.3, "y_max": 0.4}, + ] + }, + (640, 480), + np.array([[64.0, 96.0, 192.0, 192.0]]), + ), # single box + ( + does_not_raise(), + { + "objects": [ + {"x_min": 0.1, "y_min": 0.2, "x_max": 0.3, "y_max": 0.4}, + {"x_min": 0.5, "y_min": 0.6, "x_max": 0.7, "y_max": 0.8}, + ] + }, + (640, 480), + np.array([[64.0, 96.0, 192.0, 192.0], [320.0, 288.0, 448.0, 384.0]]), + ), # multiple boxes + ( + does_not_raise(), + { + "objects": [ + {"x_min": 0.1, "y_min": 0.2}, # missing x_max, y_max + {"x_min": 0.5, "y_min": 0.6, "x_max": 0.7, "y_max": 0.8}, + ] + }, + (640, 480), + np.array([[320.0, 288.0, 448.0, 384.0]]), + ), # partial valid boxes + ( + does_not_raise(), + { + "objects": [ + {"x_min": 0.0, "y_min": 0.0, "x_max": 1.0, "y_max": 1.0}, + ] + }, + (1000, 800), + np.array([[0.0, 0.0, 1000.0, 800.0]]), + ), # full image box + ( + pytest.raises(ValueError), + { + "objects": [ + {"x_min": 0.1, "y_min": 0.2, "x_max": 0.3, "y_max": 0.4}, + ] + }, + (0, 480), + None, + ), # zero width -> ValueError + ( + pytest.raises(ValueError), + { + "objects": [ + {"x_min": 0.1, "y_min": 0.2, "x_max": 0.3, "y_max": 0.4}, + ] + }, + (640, -100), + None, + ), # negative height -> ValueError + ], +) +def test_from_moondream( + exception, + result: dict, + resolution_wh: Tuple[int, int], + expected_results, +) -> None: + with exception: + xyxy = from_moondream( + result=result, + resolution_wh=resolution_wh, + ) + if expected_results is not None: + np.testing.assert_array_equal(xyxy, expected_results)