roboflow / supervision

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

[LineZone] - allow per class counting #790

Open SkalskiP opened 7 months ago

SkalskiP commented 7 months ago

Description

Currently, sv.LineZone provides only aggregated counts - all classes are thrown into one bucket. In the past, many users have asked us to provide more granular - per class count. This can be achieved by adding class_in_count and class_out_count dictionaries that will store per-class counts.

API

class LineZone:

    def __init__(
        self,
        start: Point,
        end: Point,
        triggering_anchors: Iterable[Position] = (
            Position.TOP_LEFT,
            Position.TOP_RIGHT,
            Position.BOTTOM_LEFT,
            Position.BOTTOM_RIGHT,
        ),
    ):
        # Existing initialization code...

        self.class_in_count: Dict[int, int] = {}
        self.class_out_count: Dict[int, int] = {}

    def trigger(self, detections: Detections) -> Tuple[np.ndarray, np.ndarray]:
        crossed_in = np.full(len(detections), False)
        crossed_out = np.full(len(detections), False)

        # Required logic changes...

Additional

RaghavvGupta commented 7 months ago

Hey @SkalskiP, I was wondering to work on this. I have this logic in mind, pls confirm if this approach is what you guys want?

Dictionaries to keep track of counts for each class

self.class_in_count: Dict[str, int] = {} self.class_out_count: Dict[str, int] = {}

Updating per class-in and class-out count

class_label = detections.class_labels[i]
                if class_label not in self.class_in_count:
                    self.class_in_count[class_label] = 0
                self.class_in_count[class_label] += 1
            else:
                self.out_count += 1
                crossed_out[i] = True

                # Updating per-class out count
                class_label = detections.class_labels[i]
                if class_label not in self.class_out_count:
                    self.class_out_count[class_label] = 0
                self.class_out_count[class_label] += 1
RaghavvGupta commented 6 months ago

Hey @SkalskiP πŸ˜€πŸ‘‹ https://colab.research.google.com/drive/1HxRcfzqnS6axAlYPiPpe8WLblfdn6fC_?usp=sharing

This is what I was talking about. Please tell me whether this is what you want.

SkalskiP commented 6 months ago

Hi, @RaghavvGupta, Would that be the implementation of a current count or the total count?

RaghavvGupta commented 6 months ago

I have tried implementing per-class counting. It includes dictionaries class_in_count and class_out_count to store counts for each class separately. The counts within these dictionaries are updated during each call to the trigger method, representing the current count of objects for each class based on their movement across the line.

SkalskiP commented 6 months ago

@RaghavvGupta, submit PR so we can move forward with the review process.

caiquejjx commented 2 months ago

Hello, is this being worked on?

LinasKo commented 2 months ago

Hi @caiquejjx πŸ‘‹

Yes, it is. We have an outstanding PR that we need to review.