From 2bf448f285ec87f242def597fafb58bdd859b21e Mon Sep 17 00:00:00 2001 From: Onuralp SEZER Date: Mon, 14 Jul 2025 13:09:58 +0300 Subject: [PATCH] =?UTF-8?q?refactor:=20=F0=9F=A7=B9=20remove=20test=20file?= =?UTF-8?q?=20for=20from=5Fflorence=5F2=20function=20and=20merge=20into=20?= =?UTF-8?q?main=20test=5Fvlm=20file?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Onuralp SEZER --- test/detection/test_vlm.py | 286 +++++++++++++++++++++++++ test/detection/test_vlm_florence_2.py | 291 -------------------------- 2 files changed, 286 insertions(+), 291 deletions(-) delete mode 100644 test/detection/test_vlm_florence_2.py diff --git a/test/detection/test_vlm.py b/test/detection/test_vlm.py index c6529969..7b9acb0c 100644 --- a/test/detection/test_vlm.py +++ b/test/detection/test_vlm.py @@ -1,3 +1,4 @@ +from contextlib import ExitStack as DoesNotRaise from contextlib import nullcontext as does_not_raise from typing import List, Optional, Tuple @@ -5,6 +6,7 @@ import numpy as np import pytest from supervision.detection.vlm import ( + from_florence_2, from_google_gemini, from_moondream, from_paligemma, @@ -597,3 +599,287 @@ def test_from_moondream( ) if expected_results is not None: np.testing.assert_array_equal(xyxy, expected_results) + + +@pytest.mark.parametrize( + "florence_result, resolution_wh, expected_results, exception", + [ + ( # Object detection: empty + {"": {"bboxes": [], "labels": []}}, + (10, 10), + (np.array([], dtype=np.float32), np.array([]), None, None), + DoesNotRaise(), + ), + ( # Object detection: two detections + { + "": { + "bboxes": [[4, 4, 6, 6], [5, 5, 7, 7]], + "labels": ["car", "door"], + } + }, + (10, 10), + ( + np.array([[4, 4, 6, 6], [5, 5, 7, 7]], dtype=np.float32), + np.array(["car", "door"]), + None, + None, + ), + DoesNotRaise(), + ), + ( # Caption: unsupported + {"": "A green car parked in front of a yellow building."}, + (10, 10), + None, + pytest.raises(ValueError), + ), + ( # Detailed Caption: unsupported + { + "": "The image shows a blue Volkswagen Beetle parked " + "in front of a yellow building with two brown doors, surrounded by " + "trees and a clear blue sky." + }, + (10, 10), + None, + pytest.raises(ValueError), + ), + ( # More Detailed Caption: unsupported + { + "": "The image shows a vintage Volkswagen " + "Beetle car parked on a " + "cobblestone street in front of a yellow building with two wooden " + "doors. The car is painted in a bright turquoise color and has a " + "white stripe running along the side. It has two doors on either side " + "of the car, one on top of the other, and a small window on the " + "front. The building appears to be old and dilapidated, with peeling " + "paint and crumbling walls. The sky is blue and there are trees in " + "the background." + }, + (10, 10), + None, + pytest.raises(ValueError), + ), + ( # Caption to Phrase Grounding: empty + {"": {"bboxes": [], "labels": []}}, + (10, 10), + (np.array([], dtype=np.float32), np.array([]), None, None), + DoesNotRaise(), + ), + ( # Caption to Phrase Grounding: two detections + { + "": { + "bboxes": [[4, 4, 6, 6], [5, 5, 7, 7]], + "labels": ["a green car", "a yellow building"], + } + }, + (10, 10), + ( + np.array([[4, 4, 6, 6], [5, 5, 7, 7]], dtype=np.float32), + np.array(["a green car", "a yellow building"]), + None, + None, + ), + DoesNotRaise(), + ), + ( # Dense Region caption: empty + {"": {"bboxes": [], "labels": []}}, + (10, 10), + (np.array([], dtype=np.float32), np.array([]), None, None), + DoesNotRaise(), + ), + ( # Caption to Phrase Grounding: two detections + { + "": { + "bboxes": [[4, 4, 6, 6], [5, 5, 7, 7]], + "labels": ["a green car", "a yellow building"], + } + }, + (10, 10), + ( + np.array([[4, 4, 6, 6], [5, 5, 7, 7]], dtype=np.float32), + np.array(["a green car", "a yellow building"]), + None, + None, + ), + DoesNotRaise(), + ), + ( # Region proposal + { + "": { + "bboxes": [[4, 4, 6, 6], [5, 5, 7, 7]], + "labels": ["", ""], + } + }, + (10, 10), + ( + np.array([[4, 4, 6, 6], [5, 5, 7, 7]], dtype=np.float32), + None, + None, + None, + ), + DoesNotRaise(), + ), + ( # Referring Expression Segmentation + { + "": { + "polygons": [[[1, 1, 2, 1, 2, 2, 1, 2]]], + "labels": [""], + } + }, + (10, 10), + ( + np.array([[1.0, 1.0, 2.0, 2.0]], dtype=np.float32), + None, + np.array( + [ + [ + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 1, 1, 0, 0, 0, 0, 0, 0, 0], + [0, 1, 1, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + ] + ], + dtype=bool, + ), + None, + ), + DoesNotRaise(), + ), + ( # Referring Expression Segmentation + { + "": { + "polygons": [[[1, 1, 2, 1, 2, 2, 1, 2]]], + "labels": [""], + } + }, + (10, 10), + ( + np.array([[1.0, 1.0, 2.0, 2.0]], dtype=np.float32), + None, + np.array( + [ + [ + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 1, 1, 0, 0, 0, 0, 0, 0, 0], + [0, 1, 1, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + ] + ], + dtype=bool, + ), + None, + ), + DoesNotRaise(), + ), + ( # OCR: unsupported + {"": "A"}, + (10, 10), + None, + pytest.raises(ValueError), + ), + ( # OCR with Region: obb boxes + { + "": { + "quad_boxes": [[2, 2, 6, 4, 5, 6, 1, 5], [4, 4, 5, 5, 4, 6, 3, 5]], + "labels": ["some text", "other text"], + } + }, + (10, 10), + ( + np.array([[1, 2, 6, 6], [3, 4, 5, 6]], dtype=np.float32), + np.array(["some text", "other text"]), + None, + np.array( + [[[2, 2], [6, 4], [5, 6], [1, 5]], [[4, 4], [5, 5], [4, 6], [3, 5]]] + ), + ), + DoesNotRaise(), + ), + ( # Open Vocabulary Detection + { + "": { + "bboxes": [[4, 4, 6, 6], [5, 5, 7, 7]], + "bboxes_labels": ["cat", "cat"], + "polygon": [], + "polygons_labels": [], + } + }, + (10, 10), + ( + np.array([[4, 4, 6, 6], [5, 5, 7, 7]], dtype=np.float32), + np.array(["cat", "cat"]), + None, + None, + ), + DoesNotRaise(), + ), + ( # Region to Category: empty + {"": "No object detected."}, + (10, 10), + (np.empty((0, 4), dtype=np.float32), np.array([]), None, None), + DoesNotRaise(), + ), + ( # Region to Category: detected + {"": "some object"}, + (10, 10), + ( + np.array([[3, 4, 5, 6]], dtype=np.float32), + np.array(["some object"]), + None, + None, + ), + DoesNotRaise(), + ), + ( # Region to Description: empty + {"": "No object detected."}, + (10, 10), + (np.empty((0, 4), dtype=np.float32), np.array([]), None, None), + DoesNotRaise(), + ), + ( # Region to Description: detected + {"": "descr"}, + (10, 10), + ( + np.array([[3, 4, 5, 6]], dtype=np.float32), + np.array(["descr"]), + None, + None, + ), + DoesNotRaise(), + ), + ], +) +def test_florence_2( + florence_result: dict, + resolution_wh: Tuple[int, int], + expected_results: Tuple[ + np.ndarray, Optional[np.ndarray], Optional[np.ndarray], Optional[np.ndarray] + ], + exception: Exception, +) -> None: + with exception: + result = from_florence_2(florence_result, resolution_wh) + np.testing.assert_array_equal(result[0], expected_results[0]) + if expected_results[1] is None: + assert result[1] is None + else: + np.testing.assert_array_equal(result[1], expected_results[1]) + if expected_results[2] is None: + assert result[2] is None + else: + np.testing.assert_array_equal(result[2], expected_results[2]) + if expected_results[3] is None: + assert result[3] is None + else: + np.testing.assert_array_equal(result[3], expected_results[3]) diff --git a/test/detection/test_vlm_florence_2.py b/test/detection/test_vlm_florence_2.py deleted file mode 100644 index 0fbe647b..00000000 --- a/test/detection/test_vlm_florence_2.py +++ /dev/null @@ -1,291 +0,0 @@ -from contextlib import ExitStack as DoesNotRaise -from typing import Optional, Tuple - -import numpy as np -import pytest - -from supervision.detection.vlm import from_florence_2 - - -@pytest.mark.parametrize( - "florence_result, resolution_wh, expected_results, exception", - [ - ( # Object detection: empty - {"": {"bboxes": [], "labels": []}}, - (10, 10), - (np.array([], dtype=np.float32), np.array([]), None, None), - DoesNotRaise(), - ), - ( # Object detection: two detections - { - "": { - "bboxes": [[4, 4, 6, 6], [5, 5, 7, 7]], - "labels": ["car", "door"], - } - }, - (10, 10), - ( - np.array([[4, 4, 6, 6], [5, 5, 7, 7]], dtype=np.float32), - np.array(["car", "door"]), - None, - None, - ), - DoesNotRaise(), - ), - ( # Caption: unsupported - {"": "A green car parked in front of a yellow building."}, - (10, 10), - None, - pytest.raises(ValueError), - ), - ( # Detailed Caption: unsupported - { - "": "The image shows a blue Volkswagen Beetle parked " - "in front of a yellow building with two brown doors, surrounded by " - "trees and a clear blue sky." - }, - (10, 10), - None, - pytest.raises(ValueError), - ), - ( # More Detailed Caption: unsupported - { - "": "The image shows a vintage Volkswagen " - "Beetle car parked on a " - "cobblestone street in front of a yellow building with two wooden " - "doors. The car is painted in a bright turquoise color and has a " - "white stripe running along the side. It has two doors on either side " - "of the car, one on top of the other, and a small window on the " - "front. The building appears to be old and dilapidated, with peeling " - "paint and crumbling walls. The sky is blue and there are trees in " - "the background." - }, - (10, 10), - None, - pytest.raises(ValueError), - ), - ( # Caption to Phrase Grounding: empty - {"": {"bboxes": [], "labels": []}}, - (10, 10), - (np.array([], dtype=np.float32), np.array([]), None, None), - DoesNotRaise(), - ), - ( # Caption to Phrase Grounding: two detections - { - "": { - "bboxes": [[4, 4, 6, 6], [5, 5, 7, 7]], - "labels": ["a green car", "a yellow building"], - } - }, - (10, 10), - ( - np.array([[4, 4, 6, 6], [5, 5, 7, 7]], dtype=np.float32), - np.array(["a green car", "a yellow building"]), - None, - None, - ), - DoesNotRaise(), - ), - ( # Dense Region caption: empty - {"": {"bboxes": [], "labels": []}}, - (10, 10), - (np.array([], dtype=np.float32), np.array([]), None, None), - DoesNotRaise(), - ), - ( # Caption to Phrase Grounding: two detections - { - "": { - "bboxes": [[4, 4, 6, 6], [5, 5, 7, 7]], - "labels": ["a green car", "a yellow building"], - } - }, - (10, 10), - ( - np.array([[4, 4, 6, 6], [5, 5, 7, 7]], dtype=np.float32), - np.array(["a green car", "a yellow building"]), - None, - None, - ), - DoesNotRaise(), - ), - ( # Region proposal - { - "": { - "bboxes": [[4, 4, 6, 6], [5, 5, 7, 7]], - "labels": ["", ""], - } - }, - (10, 10), - ( - np.array([[4, 4, 6, 6], [5, 5, 7, 7]], dtype=np.float32), - None, - None, - None, - ), - DoesNotRaise(), - ), - ( # Referring Expression Segmentation - { - "": { - "polygons": [[[1, 1, 2, 1, 2, 2, 1, 2]]], - "labels": [""], - } - }, - (10, 10), - ( - np.array([[1.0, 1.0, 2.0, 2.0]], dtype=np.float32), - None, - np.array( - [ - [ - [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], - [0, 1, 1, 0, 0, 0, 0, 0, 0, 0], - [0, 1, 1, 0, 0, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], - ] - ], - dtype=bool, - ), - None, - ), - DoesNotRaise(), - ), - ( # Referring Expression Segmentation - { - "": { - "polygons": [[[1, 1, 2, 1, 2, 2, 1, 2]]], - "labels": [""], - } - }, - (10, 10), - ( - np.array([[1.0, 1.0, 2.0, 2.0]], dtype=np.float32), - None, - np.array( - [ - [ - [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], - [0, 1, 1, 0, 0, 0, 0, 0, 0, 0], - [0, 1, 1, 0, 0, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], - ] - ], - dtype=bool, - ), - None, - ), - DoesNotRaise(), - ), - ( # OCR: unsupported - {"": "A"}, - (10, 10), - None, - pytest.raises(ValueError), - ), - ( # OCR with Region: obb boxes - { - "": { - "quad_boxes": [[2, 2, 6, 4, 5, 6, 1, 5], [4, 4, 5, 5, 4, 6, 3, 5]], - "labels": ["some text", "other text"], - } - }, - (10, 10), - ( - np.array([[1, 2, 6, 6], [3, 4, 5, 6]], dtype=np.float32), - np.array(["some text", "other text"]), - None, - np.array( - [[[2, 2], [6, 4], [5, 6], [1, 5]], [[4, 4], [5, 5], [4, 6], [3, 5]]] - ), - ), - DoesNotRaise(), - ), - ( # Open Vocabulary Detection - { - "": { - "bboxes": [[4, 4, 6, 6], [5, 5, 7, 7]], - "bboxes_labels": ["cat", "cat"], - "polygon": [], - "polygons_labels": [], - } - }, - (10, 10), - ( - np.array([[4, 4, 6, 6], [5, 5, 7, 7]], dtype=np.float32), - np.array(["cat", "cat"]), - None, - None, - ), - DoesNotRaise(), - ), - ( # Region to Category: empty - {"": "No object detected."}, - (10, 10), - (np.empty((0, 4), dtype=np.float32), np.array([]), None, None), - DoesNotRaise(), - ), - ( # Region to Category: detected - {"": "some object"}, - (10, 10), - ( - np.array([[3, 4, 5, 6]], dtype=np.float32), - np.array(["some object"]), - None, - None, - ), - DoesNotRaise(), - ), - ( # Region to Description: empty - {"": "No object detected."}, - (10, 10), - (np.empty((0, 4), dtype=np.float32), np.array([]), None, None), - DoesNotRaise(), - ), - ( # Region to Description: detected - {"": "descr"}, - (10, 10), - ( - np.array([[3, 4, 5, 6]], dtype=np.float32), - np.array(["descr"]), - None, - None, - ), - DoesNotRaise(), - ), - ], -) -def test_florence_2( - florence_result: dict, - resolution_wh: Tuple[int, int], - expected_results: Tuple[ - np.ndarray, Optional[np.ndarray], Optional[np.ndarray], Optional[np.ndarray] - ], - exception: Exception, -) -> None: - with exception: - result = from_florence_2(florence_result, resolution_wh) - np.testing.assert_array_equal(result[0], expected_results[0]) - if expected_results[1] is None: - assert result[1] is None - else: - np.testing.assert_array_equal(result[1], expected_results[1]) - if expected_results[2] is None: - assert result[2] is None - else: - np.testing.assert_array_equal(result[2], expected_results[2]) - if expected_results[3] is None: - assert result[3] is None - else: - np.testing.assert_array_equal(result[3], expected_results[3])