How can I count classes in traffic_analysis example from roboflow with tracker_id (using yolov8)?

55 views Asked by At

I am currently modifying a project from the roboflow/supervision repository (https://github.com/roboflow/supervision/tree/develop/examples/traffic_analysis), but I am having a hard time counting the classes for each vehicle correctly. When both a bus and a car are detected simultaneously, the counter increases for both (I really don't know why).

My code:

class DetectionsManager:
    def __init__(self) -> None:
        self.tracker_id_to_zone_id: Dict[int, int] = {}
        self.counts: Dict[int, Dict[int, Set[int]]] = {}
        self.counts_car = 0
        self.counts_bus = 0

        self.unique_detect_ids: Dict[int, Set[int]] = {}


    def update(
        self,
        detections_all: sv.Detections,
        detections_in_zones: List[sv.Detections],
        detections_out_zones: List[sv.Detections],
    ) -> sv.Detections:
        for zone_in_id, detections_in_zone in enumerate(detections_in_zones):
            for tracker_id in detections_in_zone.tracker_id:
                self.tracker_id_to_zone_id.setdefault(tracker_id, zone_in_id)

        for zone_out_id, detections_out_zone in enumerate(detections_out_zones):
            for tracker_id in detections_out_zone.tracker_id:
                if tracker_id in self.tracker_id_to_zone_id:
                    zone_in_id = self.tracker_id_to_zone_id[tracker_id]
                    self.counts.setdefault(zone_out_id, {})
                    self.counts[zone_out_id].setdefault(zone_in_id, set())
                    self.counts[zone_out_id][zone_in_id].add(tracker_id)
                    if hasattr(detections_out_zone, 'tracker_id'):
                            for tracker_id in enumerate(detections_out_zone.tracker_id):
                                #maybe:
                                #  detect_id = detections_out_zone.class_id
                                #  if detect_id not in self.unique_detect_ids.get(tracker_id, set()):
                                #   self.unique_detect_ids.setdefault(tracker_id, set()).add(detect_id)
                                #   if detect_id == 1:
                                #       self.counts_car += 1
                                #   elif detect_id == 0:
                                #       self.counts_bus += 1
                                for detect_id in detections_out_zone.class_id:
                                    if detect_id not in self.unique_detect_ids.get(tracker_id, set()):
                                        self.unique_detect_ids.setdefault(tracker_id, set()).add(detect_id)
                                        if detect_id == 1:
                                            self.counts_car += 1
                                        elif detect_id == 0:
                                            self.counts_bus += 1
0

There are 0 answers