From 440be5d416465720ea95d1d1474cba2bcb15d460 Mon Sep 17 00:00:00 2001 From: SkalskiP Date: Wed, 4 Oct 2023 18:36:30 +0200 Subject: [PATCH] Add color mapping strategy to InferenceSlicer Added a new argument, 'color_map' in InferenceSlicer to provide more options for color coding while annotating. This includes mapping colors by 'index', 'class' or 'track'. Also refactored the way colors are resolved by separating the responsibility into a new function 'resolve_color_idx' and 'resolve_color'. --- supervision/annotators/core.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/supervision/annotators/core.py b/supervision/annotators/core.py index 350ae06b..7f11609b 100644 --- a/supervision/annotators/core.py +++ b/supervision/annotators/core.py @@ -582,6 +582,7 @@ class TraceAnnotator: position: Optional[Position] = Position.CENTER, trace_length: int = 30, thickness: int = 2, + color_map: str = "class" ): """ Args: @@ -592,11 +593,14 @@ class TraceAnnotator: trace_length (int): The maximum length of the trace in terms of historical points. Defaults to `30`. thickness (int): The thickness of the trace lines. Defaults to `2`. + color_map (str): Strategy for mapping colors to annotations. + Options are `index`, `class`, or `track`. """ self.color: Union[Color, ColorPalette] = color self.position = position self.trace = Trace(max_size=trace_length) self.thickness = thickness + self.color_map: ColorMap = ColorMap(color_map) def annotate(self, scene: np.ndarray, detections: Detections) -> np.ndarray: """ @@ -629,15 +633,14 @@ class TraceAnnotator: """ self.trace.put(detections) - for i, (xyxy, mask, confidence, class_id, tracker_id) in enumerate(detections): - class_id = detections.class_id[i] if class_id is not None else None - idx = class_id if class_id is not None else i - color = ( - self.color.by_idx(idx) - if isinstance(self.color, ColorPalette) - else self.color + for detection_idx in range(len(detections)): + tracker_id = int(detections.tracker_id[detection_idx]) + idx = resolve_color_idx( + detections=detections, + detection_idx=detection_idx, + color_map=self.color_map, ) - + color = resolve_color(color=self.color, idx=idx) xy = self.trace.get(tracker_id=tracker_id) if len(xy) > 1: scene = cv2.polylines(