From 34b14ec7c26708896054bbd2d9a98e362e376299 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Agust=C3=ADn=20Castro?= Date: Wed, 28 Feb 2024 17:14:54 -0300 Subject: [PATCH] Add an initialization delay for the clusters --- demos/multi_camera/demo.py | 14 +++++++++++++ norfair/common_reference_ui.py | 36 +--------------------------------- norfair/multi_camera.py | 35 ++++++++++++++++++++++++++++++++- 3 files changed, 49 insertions(+), 36 deletions(-) diff --git a/demos/multi_camera/demo.py b/demos/multi_camera/demo.py index c1e45ecf..41476257 100644 --- a/demos/multi_camera/demo.py +++ b/demos/multi_camera/demo.py @@ -243,6 +243,18 @@ def run(): default=20, help="Min detections needed to start the tracked object", ) + parser.add_argument( + "--clusterizer-initialization-delay", + type=float, + default=6, + help="Minimum age of a cluster (or it's objects) to be returned", + ) + parser.add_argument( + "--filter-by-objects-age", + type=bool, + default=False, + help="Filter cluster by their objects age, instead of the clusters age.", + ) parser.add_argument( "--hit-counter-max", type=int, @@ -450,6 +462,8 @@ def normalized_foot_distance(tracker1, tracker2): max_votes_grow=args.max_votes_grow, max_votes_split=args.max_votes_grow, memory=args.memory, + initialization_delay=args.clusterizer_initialization_delay, + filter_by_objects_age=args.filter_by_objects_age, ) while True: diff --git a/norfair/common_reference_ui.py b/norfair/common_reference_ui.py index 1d8e3e2f..d0dbbebd 100644 --- a/norfair/common_reference_ui.py +++ b/norfair/common_reference_ui.py @@ -11,40 +11,6 @@ from norfair import Video from norfair.camera_motion import HomographyTransformationGetter, TransformationGetter -transformation = None - -window = None - -button_finish = None - -reference_point_canvas = None -footage_point_canvas = None - -canvas_reference = None -canvas_footage = None - -reference_original_size = None -reference_canvas_size = None -footage_original_size = None -footage_canvas_size = None - -footage_point = None -reference_point = None - -skipper = None - -points = None -points_sampled = None - -mode_annotate = None - -frame_options_annotations = None -handling_mark_functions = None -handle_mark_annotation = None - -global button_says_ignore -global button_ignore - def resize_image(image, desired_width=None, desired_height=None): aspect_ratio = image.height / image.width @@ -281,7 +247,7 @@ def handle_finish(): for info in skipper.values(): if info["video"] is not None: info["video"].video_capture.release() - cv2.destroyAllWindows() + cv2.destroyAllWindows() return transformation else: print("Can't leave without estimating the transformation.") diff --git a/norfair/multi_camera.py b/norfair/multi_camera.py index e09c31cc..ebc7e956 100644 --- a/norfair/multi_camera.py +++ b/norfair/multi_camera.py @@ -76,6 +76,8 @@ def __init__( max_votes_grow: int = 5, max_votes_split: int = 5, memory: int = 3, + initialization_delay: int = 4, + filter_by_objects_age: bool = False, ): """ Associate trackers from different cameras/videos. @@ -130,6 +132,12 @@ def __init__( self.max_votes_grow = max_votes_grow self.max_votes_split = max_votes_split + # I will give the trackers at least enough time to merge their cluster with another + self.initialization_delay = initialization_delay + max_votes_grow + self.filter_by_objects_age = filter_by_objects_age + + self.id_to_ages = {} + def update(self, trackers_by_camera): # trackers_by_camera = [list(tracked_objects_camera_0), list(tracked_objects_camera_1),...] @@ -649,4 +657,29 @@ def update(self, trackers_by_camera): cluster for cluster in self.clusters if len(cluster) > 0 ] - return self.clusters + if self.filter_by_objects_age: + return [ + cluster + for cluster in self.clusters + if any( + [ + obj.age - obj.initialization_delay > self.initialization_delay + for obj in cluster.tracked_objects.values() + ] + ) + ] + else: + id_to_ages = {} + for cluster in self.clusters: + if cluster.id in self.id_to_ages.keys(): + id_to_ages[cluster.id] = self.id_to_ages[cluster.id] + 1 + else: + id_to_ages[cluster.id] = 0 + + self.id_to_ages = id_to_ages + + return [ + cluster + for cluster in self.clusters + if id_to_ages[cluster.id] > self.initialization_delay + ]