From 7eb918282cf81c0c30e80ddad279265fe35528a9 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 22 May 2024 12:27:53 +0000 Subject: [PATCH] =?UTF-8?q?fix(pre=5Fcommit):=20=F0=9F=8E=A8=20auto=20form?= =?UTF-8?q?at=20pre-commit=20hooks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- supervision/detection/core.py | 2 +- supervision/detection/lmm.py | 24 +++++++--------- test/detection/test_lmm.py | 54 +++++++++++++++++------------------ 3 files changed, 38 insertions(+), 42 deletions(-) diff --git a/supervision/detection/core.py b/supervision/detection/core.py index d6a04efb..e8599817 100644 --- a/supervision/detection/core.py +++ b/supervision/detection/core.py @@ -7,7 +7,7 @@ from typing import Any, Dict, Iterator, List, Optional, Tuple, Union import numpy as np from supervision.config import CLASS_NAME_DATA_FIELD, ORIENTED_BOX_COORDINATES -from supervision.detection.lmm import LMM, validate_lmm_and_kwargs, from_paligemma +from supervision.detection.lmm import LMM, from_paligemma, validate_lmm_and_kwargs from supervision.detection.utils import ( box_non_max_suppression, calculate_masks_centroids, diff --git a/supervision/detection/lmm.py b/supervision/detection/lmm.py index 1c4b90dc..67921328 100644 --- a/supervision/detection/lmm.py +++ b/supervision/detection/lmm.py @@ -1,20 +1,17 @@ import re -import numpy as np from enum import Enum -from typing import Dict, List, Tuple, Optional, Union, Any +from typing import Any, Dict, List, Optional, Tuple, Union + +import numpy as np class LMM(Enum): - PALIGEMMA = 'paligemma' + PALIGEMMA = "paligemma" -REQUIRED_ARGUMENTS: Dict[LMM, List[str]] = { - LMM.PALIGEMMA: ['resolution_wh'] -} +REQUIRED_ARGUMENTS: Dict[LMM, List[str]] = {LMM.PALIGEMMA: ["resolution_wh"]} -ALLOWED_ARGUMENTS: Dict[LMM, List[str]] = { - LMM.PALIGEMMA: ['resolution_wh', 'classes'] -} +ALLOWED_ARGUMENTS: Dict[LMM, List[str]] = {LMM.PALIGEMMA: ["resolution_wh", "classes"]} def validate_lmm_and_kwargs(lmm: Union[LMM, str], kwargs: Dict[str, Any]) -> LMM: @@ -40,13 +37,12 @@ def validate_lmm_and_kwargs(lmm: Union[LMM, str], kwargs: Dict[str, Any]) -> LMM def from_paligemma( - result: str, - resolution_wh: Tuple[int, int], - classes: Optional[List[str]] = None + result: str, resolution_wh: Tuple[int, int], classes: Optional[List[str]] = None ) -> Tuple[np.ndarray, Optional[np.ndarray], np.ndarray]: w, h = resolution_wh pattern = re.compile( - r'(?) (\w+)') + r"(?) (\w+)" + ) matches = pattern.findall(result) matches = np.array(matches) if matches else np.empty((0, 5)) @@ -59,4 +55,4 @@ def from_paligemma( xyxy, class_name = xyxy[mask], class_name[mask] class_id = np.array([classes.index(name) for name in class_name]) - return xyxy, class_id, class_name.astype(np.dtype('U')) + return xyxy, class_id, class_name.astype(np.dtype("U")) diff --git a/test/detection/test_lmm.py b/test/detection/test_lmm.py index f8ea91ef..5066a7a3 100644 --- a/test/detection/test_lmm.py +++ b/test/detection/test_lmm.py @@ -1,6 +1,6 @@ -import numpy as np -from typing import Tuple, Optional, List +from typing import List, Optional, Tuple +import numpy as np import pytest from supervision.detection.lmm import from_paligemma @@ -13,91 +13,91 @@ from supervision.detection.lmm import from_paligemma "", (1000, 1000), None, - (np.empty((0, 4)), None, np.empty(0).astype(np.dtype('U'))) + (np.empty((0, 4)), None, np.empty(0).astype(np.dtype("U"))), ), # empty response ( "\n", (1000, 1000), None, - (np.empty((0, 4)), None, np.empty(0).astype(np.dtype('U'))) + (np.empty((0, 4)), None, np.empty(0).astype(np.dtype("U"))), ), # new line response ( "the quick brown fox jumps over the lazy dog.", (1000, 1000), None, - (np.empty((0, 4)), None, np.empty(0).astype(np.dtype('U'))) + (np.empty((0, 4)), None, np.empty(0).astype(np.dtype("U"))), ), # response with no location ( " cat", (1000, 1000), None, - (np.empty((0, 4)), None, np.empty(0).astype(np.dtype('U'))) + (np.empty((0, 4)), None, np.empty(0).astype(np.dtype("U"))), ), # response with missing location ( " cat", (1000, 1000), None, - (np.empty((0, 4)), None, np.empty(0).astype(np.dtype('U'))) + (np.empty((0, 4)), None, np.empty(0).astype(np.dtype("U"))), ), # response with extra location ( "", (1000, 1000), None, - (np.empty((0, 4)), None, np.empty(0).astype(np.dtype('U'))) + (np.empty((0, 4)), None, np.empty(0).astype(np.dtype("U"))), ), # response with no class ( " catt", (1000, 1000), - ['cat', 'dog'], - (np.empty((0, 4)), np.empty(0), np.empty(0).astype(np.dtype('U'))) + ["cat", "dog"], + (np.empty((0, 4)), np.empty(0), np.empty(0).astype(np.dtype("U"))), ), # response with invalid class ( " cat", (1000, 1000), None, ( - np.array([[250., 250., 750., 750.]]), + np.array([[250.0, 250.0, 750.0, 750.0]]), None, - np.array(['cat']).astype(np.dtype('U')) - ) + np.array(["cat"]).astype(np.dtype("U")), + ), ), # correct response; no classes ( " cat ;", (1000, 1000), - ['cat', 'dog'], + ["cat", "dog"], ( - np.array([[250., 250., 750., 750.]]), + np.array([[250.0, 250.0, 750.0, 750.0]]), np.array([0]), - np.array(['cat']).astype(np.dtype('U')) - ) + np.array(["cat"]).astype(np.dtype("U")), + ), ), # correct response; with classes ( " cat ; cat", (1000, 1000), - ['cat', 'dog'], + ["cat", "dog"], ( - np.array([[250., 250., 750., 750.]]), + np.array([[250.0, 250.0, 750.0, 750.0]]), np.array([0]), - np.array(['cat']).astype(np.dtype('U')) - ) + np.array(["cat"]).astype(np.dtype("U")), + ), ), # partially correct response; with classes ( " cat ; cat", (1000, 1000), - ['cat', 'dog'], + ["cat", "dog"], ( - np.array([[250., 250., 750., 750.]]), + np.array([[250.0, 250.0, 750.0, 750.0]]), np.array([0]), - np.array(['cat']).astype(np.dtype('U')) - ) + np.array(["cat"]).astype(np.dtype("U")), + ), ), # partially correct response; with classes - ] + ], ) def test_from_paligemma( result: str, resolution_wh: Tuple[int, int], classes: Optional[List[str]], - expected_results: Tuple[np.ndarray, Optional[np.ndarray], np.ndarray] + expected_results: Tuple[np.ndarray, Optional[np.ndarray], np.ndarray], ) -> None: result = from_paligemma(result=result, resolution_wh=resolution_wh, classes=classes) np.testing.assert_array_equal(result[0], expected_results[0])