Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace any print() + exit() combos with raise #259

Merged
merged 1 commit into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions norfair/tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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]
Expand Down
24 changes: 9 additions & 15 deletions norfair/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions norfair/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Expand Down
Loading