From b451d0a4f10db1f13540a6fca94bb2fa98595cf4 Mon Sep 17 00:00:00 2001 From: Nicolas Mowen Date: Mon, 13 May 2024 09:00:34 -0600 Subject: [PATCH] Improve mask error (#11361) * Don't apply masks / zones that are outside of the detect resolution * formatting --- frigate/util/config.py | 45 ++++++++++++++++++++++++++++++------------ 1 file changed, 32 insertions(+), 13 deletions(-) diff --git a/frigate/util/config.py b/frigate/util/config.py index e882f0bfd..f395ee125 100644 --- a/frigate/util/config.py +++ b/frigate/util/config.py @@ -157,26 +157,45 @@ def get_relative_coordinates( points = m.split(",") if any(x > "1.0" for x in points): - relative_masks.append( - ",".join( - [ - f"{round(int(points[i]) / frame_shape[1], 3)},{round(int(points[i + 1]) / frame_shape[0], 3)}" - for i in range(0, len(points), 2) - ] + rel_points = [] + for i in range(0, len(points), 2): + x = int(points[i]) + y = int(points[i + 1]) + + if x > frame_shape[1] or y > frame_shape[0]: + logger.error( + f"Not applying mask due to invalid coordinates. {x},{y} is outside of the detection resolution {frame_shape[1]}x{frame_shape[0]}. Use the editor in the UI to correct the mask." + ) + continue + + rel_points.append( + f"{round(x / frame_shape[1], 3)},{round(y / frame_shape[0], 3)}" ) - ) + + relative_masks.append(",".join(rel_points)) else: relative_masks.append(m) mask = relative_masks elif isinstance(mask, str) and any(x > "1.0" for x in mask.split(",")): points = mask.split(",") - mask = ",".join( - [ - f"{round(int(points[i]) / frame_shape[1], 3)},{round(int(points[i + 1]) / frame_shape[0], 3)}" - for i in range(0, len(points), 2) - ] - ) + rel_points = [] + + for i in range(0, len(points), 2): + x = int(points[i]) + y = int(points[i + 1]) + + if x > frame_shape[1] or y > frame_shape[0]: + logger.error( + f"Not applying mask due to invalid coordinates. {x},{y} is outside of the detection resolution {frame_shape[1]}x{frame_shape[0]}. Use the editor in the UI to correct the mask." + ) + return [] + + rel_points.append( + f"{round(x / frame_shape[1], 3)},{round(y / frame_shape[0], 3)}" + ) + + mask = ",".join(rel_points) return mask