From d567ec6bcaa4c0ee1054429837c0ea89cb40ff6b Mon Sep 17 00:00:00 2001 From: javiber Date: Mon, 21 Nov 2022 11:44:51 -0300 Subject: [PATCH] Adressing changes requested - fixed a bug with the text color on draw_boxes.py and draw_points.py - renamed params on color.py - added mising typehints in color.py - typos in color.py - removed unused imports in absolute_grid.y --- norfair/drawing/absolute_grid.py | 1 - norfair/drawing/color.py | 38 ++++++++++++++++++-------------- norfair/drawing/draw_boxes.py | 5 ++++- norfair/drawing/draw_points.py | 6 ++++- 4 files changed, 31 insertions(+), 19 deletions(-) diff --git a/norfair/drawing/absolute_grid.py b/norfair/drawing/absolute_grid.py index 63d648be..50e2ecc5 100644 --- a/norfair/drawing/absolute_grid.py +++ b/norfair/drawing/absolute_grid.py @@ -1,5 +1,4 @@ from functools import lru_cache -from typing import Optional, Tuple import numpy as np diff --git a/norfair/drawing/color.py b/norfair/drawing/color.py index 18c97e7a..fba2478d 100644 --- a/norfair/drawing/color.py +++ b/norfair/drawing/color.py @@ -1,5 +1,5 @@ import re -from typing import Any, Iterable, Tuple, Union +from typing import Any, Hashable, Iterable, Tuple, Union # types @@ -7,8 +7,8 @@ ColorLike = Union[ColorType, str] -def hex_to_bgr(string: str) -> ColorType: - """Converts convencional 6 digits hex colors to BGR tuples +def hex_to_bgr(hex_value: str) -> ColorType: + """Converts conventional 6 digits hex colors to BGR tuples Parameters ---------- @@ -25,10 +25,16 @@ def hex_to_bgr(string: str) -> ColorType: ValueError if the string is invalid """ - # TODO: support 3-digits hex - if re.match("#[a-f0-9]{6}$", string): - return int(string[3:5], 16), int(string[5:7], 16), int(string[1:3], 16) - raise ValueError(f"'{string}' is not a valid color") + if re.match("#[a-f0-9]{6}$", hex_value): + return int(hex_value[3:5], 16), int(hex_value[5:7], 16), int(hex_value[1:3], 16) + + if re.match("#[a-f0-9]{3}$", hex_value): + return ( + int(hex_value[3] * 2, 16), + int(hex_value[2] * 2, 16), + int(hex_value[1] * 2, 16), + ) + raise ValueError(f"'{hex_value}' is not a valid color") class Color: @@ -223,12 +229,12 @@ class Color: cb10 = hex_to_bgr("#56b4e9") -def parse_color(value: ColorLike) -> ColorType: +def parse_color(color_like: ColorLike) -> ColorType: """Makes best effort to parse the given value to a Color Parameters ---------- - value : ColorLike + color_like : ColorLike Can be one of: 1. a string with the 6 digits hex value (`"#ff0000"`) @@ -240,13 +246,13 @@ def parse_color(value: ColorLike) -> ColorType: Color The BGR tuple. """ - if isinstance(value, str): - if value.startswith("#"): - return hex_to_bgr(value) + if isinstance(color_like, str): + if color_like.startswith("#"): + return hex_to_bgr(color_like) else: - return getattr(Color, value) - # TODO: validate value? - return tuple([int(v) for v in value]) + return getattr(Color, color_like) + # TODO: validate? + return tuple([int(v) for v in color_like]) PALETTES = { @@ -355,7 +361,7 @@ def set_default_color(cls, color: ColorLike): cls._default_color = parse_color(color) @classmethod - def choose_color(cls, hashable): + def choose_color(cls, hashable: Hashable) -> ColorType: if hashable is None: return cls._default_color return cls._colors[abs(hash(hashable)) % len(cls._colors)] diff --git a/norfair/drawing/draw_boxes.py b/norfair/drawing/draw_boxes.py index 275e26bf..6fd8c7a9 100644 --- a/norfair/drawing/draw_boxes.py +++ b/norfair/drawing/draw_boxes.py @@ -124,6 +124,9 @@ def draw_boxes( if drawables is None: return frame + if text_color is not None: + text_color = parse_color(text_color) + for obj in drawables: d = Drawable(obj) @@ -150,7 +153,7 @@ def draw_boxes( if text_color is None: obj_text_color = obj_color else: - obj_text_color = color + obj_text_color = text_color # the anchor will become the bottom-left of the text, # we select-top left of the bbox compensating for the thickness of the box text_anchor = ( diff --git a/norfair/drawing/draw_points.py b/norfair/drawing/draw_points.py index 9e83a629..cd5cce11 100644 --- a/norfair/drawing/draw_points.py +++ b/norfair/drawing/draw_points.py @@ -110,6 +110,10 @@ def draw_points( if drawables is None: return + + if text_color is not None: + text_color = parse_color(text_color) + if color is None: color = "by_id" if thickness is None: @@ -135,7 +139,7 @@ def draw_points( if text_color is None: obj_text_color = obj_color else: - obj_text_color = color + obj_text_color = text_color if draw_points: for point, live in zip(d.points, d.live_points):