From 846b33715502c00dc04ef5fa97ec2d5e13020b20 Mon Sep 17 00:00:00 2001 From: Christoforos Aristeidou Date: Fri, 12 Jan 2024 01:10:13 +0200 Subject: [PATCH] changed if else statements to dictionary mapping --- supervision/annotators/core.py | 40 +++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/supervision/annotators/core.py b/supervision/annotators/core.py index 8ae9a241..9479e09e 100644 --- a/supervision/annotators/core.py +++ b/supervision/annotators/core.py @@ -1570,28 +1570,32 @@ class PercentageBarAnnotator(BaseAnnotator): cx, cy = detections.get_anchors_coordinates( anchor=self.position )[detection_idx].astype(int) - + conf = detections.confidence[detection_idx] border_thickness = int(0.15 * self.height) - if self.position == "TOP_LEFT": - - elif self.position == "TOP_CENTER": - - elif self.position == "TOP_RIGHT": - - elif self.position == "CENTER_LEFT": - - elif self.position == "CENTER": - - elif self.position == "CENTER_RIGHT": - - elif self.position == "BOTTOM_LEFT": - - elif self.position == "BOTTOM_CENTER": - - elif self.position == "BOTTOM_RIGHT": + border_mapping = { + Position.TOP_LEFT: ((cx - self.width, cy - self.height), + (cx, cy)), + Position.TOP_CENTER: ((cx - self.width//2, cy), + (cx + self.width//2, cy - self.height)), + Position.TOP_RIGHT: ((cx, cy), + (cx + self.width, cy - self.height)), + Position.CENTER_LEFT: ((cx - self.width, cy - self.height//2), + (cx, cy + self.height//2)), + Position.CENTER: ((cx - self.width//2, cy - self.height//2), + (cx + self.width//2, cy + self.height//2)), + Position.CENTER_RIGHT: ((cx, cy - self.height//2), + (cx + self.width, cy + self.height//2)), + Position.BOTTOM_LEFT: ((cx - self.width, cy), + (cx, cy + self.height)), + Position.BOTTOM_CENTER: ((cx - self.width//2, cy), + (cx + self.width//2, cy + self.height)), + Position.BOTTOM_RIGHT: ((cx, cy), + (cx + self.width, cy + self.height)), + } + border_coords = border_mapping[self.position] color = resolve_color( color=self.color,