roboflow / supervision

We write your reusable computer vision tools. πŸ’œ
https://supervision.roboflow.com
MIT License
17.14k stars 1.31k forks source link

enhance numpy arrays static type checking #1021

Open onuralpszr opened 3 months ago

onuralpszr commented 3 months ago

Description

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

SkalskiP commented 3 months ago

@onuralpszr, thanks a lot for creating this issue πŸ™πŸ» I really appreciate that you're so helpful.

onuralpszr commented 3 months ago

@onuralpszr, thanks a lot for creating this issue πŸ™πŸ» I really appreciate that you're so helpful.

You are welcome :)

jeslinpjames commented 3 months ago

Hello, can i try to do this?

onuralpszr commented 3 months ago

Hello, can i try to do this?

I am already doing most of them. :)

jeslinpjames commented 3 months ago

Ohh okay

miladhatami1393 commented 1 month ago

I really appreciate that you're so helpful

SkalskiP commented 1 month ago

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

onuralpszr commented 1 month ago

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

SkalskiP commented 1 month ago

@onuralpszr sounds great! πŸ”₯