supervision icon indicating copy to clipboard operation
supervision copied to clipboard

enhance numpy arrays static type checking

Open onuralpszr opened this issue 1 year ago • 11 comments

Description

  • This issue aims to improve the static type checking in our codebase and expand the functionality of the Supervision library. The enhancements will make the static type checking and improve code readilibty and help developers to understand which type they need to use
  • Ensure type checking for all possible the functions and classes in the Supervision library.

Old Style code snippet


import numpy as np

@dataclass
class Detection:
    xyxy: np.ndarray
    mask: Optional[np.ndarray] = None
    confidence: Optional[np.ndarray] = None
    class_id: Optional[np.ndarray] = None
    tracker_id: Optional[np.ndarray] = None
    data: Dict[str, Union[np.ndarray, List]] = field(default_factory=dict)

    def __len__(self):
        return len(self.xyxy)

    def __eq__(self, other: Detections):
        return all(
            [
                np.array_equal(self.xyxy, other.xyxy),
                np.array_equal(self.mask, other.mask),
                np.array_equal(self.class_id, other.class_id),
                np.array_equal(self.confidence, other.confidence),
                np.array_equal(self.tracker_id, other.tracker_id),
                is_data_equal(self.data, other.data),
            ]
        )

New Style code snippet


import numpy as np
import numpy.typing as npt

@dataclass
class Detection:
    xyxy: npt.NDArray[np.float32]
    mask: Optional[npt.NDArray[np.float32]] = None
    confidence: Optional[npt.NDArray[np.float32]] = None
    class_id: Optional[npt.NDArray[np.float32]] = None
    tracker_id: Optional[npt.NDArray[np.float32]] = None
    data: Dict[str, Union[npt.NDArray[Any], List[Any]]] = field(default_factory=dict)

    def __len__(self) -> int:
        return len(self.xyxy)

    def __eq__(self, other: Detections) -> bool:
        return all(
            [
                np.array_equal(self.xyxy, other.xyxy),
                np.array_equal(self.mask, other.mask),
                np.array_equal(self.class_id, other.class_id),
                np.array_equal(self.confidence, other.confidence),
                np.array_equal(self.tracker_id, other.tracker_id),
                is_data_equal(self.data, other.data),
            ]
        )

Additional

  • Note: Please share a Google Colab with minimal code to test the new feature. We know it's additional work, but it will definitely speed up the review process. Each change must be tested by the reviewer. Setting up a local environment to do this is time-consuming. Please ensure that Google Colab can be accessed without any issues (make it public). Thank you! 🙏🏻

onuralpszr avatar Mar 19 '24 02:03 onuralpszr

@onuralpszr, thanks a lot for creating this issue 🙏🏻 I really appreciate that you're so helpful.

SkalskiP avatar Mar 19 '24 07:03 SkalskiP

@onuralpszr, thanks a lot for creating this issue 🙏🏻 I really appreciate that you're so helpful.

You are welcome :)

onuralpszr avatar Mar 19 '24 07:03 onuralpszr

Hello, can i try to do this?

jeslinpjames avatar Mar 21 '24 18:03 jeslinpjames

Hello, can i try to do this?

I am already doing most of them. :)

onuralpszr avatar Mar 21 '24 18:03 onuralpszr

Ohh okay

jeslinpjames avatar Mar 21 '24 18:03 jeslinpjames

I really appreciate that you're so helpful

miladhatami1393 avatar May 13 '24 05:05 miladhatami1393

Hi @onuralpszr are you still thinking about finishing this work, or can I make it unassigned?

SkalskiP avatar May 13 '24 09:05 SkalskiP

Hi @onuralpszr are you still thinking about finishing this work, or can I make it unassigned?

Sure, Let me finish and send PR, I already had changes in my local let me re-check, update with latest changes and open PR

onuralpszr avatar May 13 '24 09:05 onuralpszr

@onuralpszr sounds great! 🔥

SkalskiP avatar May 13 '24 10:05 SkalskiP