From 3444b9df03848561a0d732cce305abc285a38982 Mon Sep 17 00:00:00 2001 From: quantumdot Date: Tue, 7 Mar 2023 13:58:09 -0500 Subject: [PATCH] replace any print() + exit() combos with raise --- norfair/tracker.py | 6 ++---- norfair/utils.py | 24 +++++++++--------------- norfair/video.py | 3 +-- 3 files changed, 12 insertions(+), 21 deletions(-) diff --git a/norfair/tracker.py b/norfair/tracker.py index 63f5e4a6..275eceee 100644 --- a/norfair/tracker.py +++ b/norfair/tracker.py @@ -294,10 +294,9 @@ def _update_objects_in_place( if candidates is not None and len(candidates) > 0: distance_matrix = distance_function.get_distances(objects, candidates) if np.isnan(distance_matrix).any(): - print( + raise ValueError( "\nReceived nan values from distance function, please check your distance function for errors!" ) - exit() # Used just for debugging distance function if distance_matrix.any(): @@ -485,10 +484,9 @@ def __init__( coord_transformations: Optional[CoordinatesTransformation] = None, ): if not isinstance(initial_detection, Detection): - print( + raise ValueError( f"\n[red]ERROR[/red]: The detection list fed into `tracker.update()` should be composed of {Detection} objects not {type(initial_detection)}.\n" ) - exit() self._obj_factory = obj_factory self.dim_points = initial_detection.absolute_points.shape[1] self.num_points = initial_detection.absolute_points.shape[0] diff --git a/norfair/utils.py b/norfair/utils.py index d04b6227..98e0d903 100644 --- a/norfair/utils.py +++ b/norfair/utils.py @@ -14,20 +14,16 @@ def validate_points(points: np.ndarray) -> np.array: if len(points.shape) == 1: points = points[np.newaxis, ...] elif len(points.shape) > 2: - print_detection_error_message_and_exit(points) + raise_detection_error_message(points) return points -def print_detection_error_message_and_exit(points): - print("\n[red]INPUT ERROR:[/red]") - print( - f"Each `Detection` object should have a property `points` of shape (num_of_points_to_track, 2), not {points.shape}. Check your `Detection` list creation code." - ) - print("You can read the documentation for the `Detection` class here:") - print( - "https://tryolabs.github.io/norfair/reference/tracker/#norfair.tracker.Detection\n" - ) - exit() +def raise_detection_error_message(points): + message = "\n[red]INPUT ERROR:[/red]\n" + message += f"Each `Detection` object should have a property `points` of shape (num_of_points_to_track, 2), not {points.shape}. Check your `Detection` list creation code.\n" + message += "You can read the documentation for the `Detection` class here:\n" + message += "https://tryolabs.github.io/norfair/reference/tracker/#norfair.tracker.Detection\n" + raise ValueError(message) def print_objects_as_table(tracked_objects: Sequence): @@ -73,22 +69,20 @@ def get_cutout(points, image): class DummyOpenCVImport: def __getattribute__(self, name): - print( + raise ImportError( r"""[bold red]Missing dependency:[/bold red] You are trying to use Norfair's video features. However, OpenCV is not installed. Please, make sure there is an existing installation of OpenCV or install Norfair with `pip install norfair\[video]`.""" ) - exit() class DummyMOTMetricsImport: def __getattribute__(self, name): - print( + raise ImportError( r"""[bold red]Missing dependency:[/bold red] You are trying to use Norfair's metrics features without the required dependencies. Please, install Norfair with `pip install norfair\[metrics]`, or `pip install norfair\[metrics,video]` if you also want video features.""" ) - exit() # lru_cache will prevent re-run the function if the message is the same diff --git a/norfair/video.py b/norfair/video.py index 0f3cd3c3..83abbd4a 100644 --- a/norfair/video.py +++ b/norfair/video.py @@ -176,8 +176,7 @@ def __iter__(self): cv2.destroyAllWindows() def _fail(self, msg: str): - print(msg) - exit() + raise RuntimeError(msg) def write(self, frame: np.ndarray) -> int: """