From 6d7b1ce384ead057cae07928281c70fe389a0375 Mon Sep 17 00:00:00 2001 From: ryzendigo <48058157+ryzendigo@users.noreply.github.com> Date: Mon, 16 Mar 2026 20:47:24 +0800 Subject: [PATCH] fix: inverted condition causes division by zero in velocity direction check (#22470) The cosine similarity calculation is guarded by: if not np.any(np.linalg.norm(velocities, axis=1)) This enters the block when ALL velocity norms are zero, then divides by those zero norms. The condition should check that all norms are non-zero before computing cosine similarity: if np.all(np.linalg.norm(velocities, axis=1)) Also fixes debug log that shows average_velocity[0] for both x and y velocity components (second should be average_velocity[1]). --- frigate/ptz/autotrack.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/frigate/ptz/autotrack.py b/frigate/ptz/autotrack.py index eb2d16940..1a45f619c 100644 --- a/frigate/ptz/autotrack.py +++ b/frigate/ptz/autotrack.py @@ -901,7 +901,7 @@ class PtzAutoTracker: # Check direction difference velocities = np.round(velocities) invalid_dirs = False - if not np.any(np.linalg.norm(velocities, axis=1)): + if np.all(np.linalg.norm(velocities, axis=1)): cosine_sim = np.dot(velocities[0], velocities[1]) / ( np.linalg.norm(velocities[0]) * np.linalg.norm(velocities[1]) ) @@ -1067,7 +1067,7 @@ class PtzAutoTracker: f"{camera}: Zoom test: below dimension threshold: {below_dimension_threshold} width: {bb_right - bb_left}, max width: {camera_width * (self.zoom_factor[camera] + 0.1)}, height: {bb_bottom - bb_top}, max height: {camera_height * (self.zoom_factor[camera] + 0.1)}" ) logger.debug( - f"{camera}: Zoom test: below velocity threshold: {below_velocity_threshold} velocity x: {abs(average_velocity[0])}, x threshold: {velocity_threshold_x}, velocity y: {abs(average_velocity[0])}, y threshold: {velocity_threshold_y}" + f"{camera}: Zoom test: below velocity threshold: {below_velocity_threshold} velocity x: {abs(average_velocity[0])}, x threshold: {velocity_threshold_x}, velocity y: {abs(average_velocity[1])}, y threshold: {velocity_threshold_y}" ) logger.debug(f"{camera}: Zoom test: at max zoom: {at_max_zoom}") logger.debug(f"{camera}: Zoom test: at min zoom: {at_min_zoom}")