From e571853f11ec6d7488ad0f79ea81050db02ce80e Mon Sep 17 00:00:00 2001 From: Nicolas Mowen Date: Tue, 9 Apr 2024 11:23:20 -0600 Subject: [PATCH] Get motion mask working with relative coordinates --- frigate/config.py | 28 ++++++++++++++++++++++++++++ frigate/util/image.py | 16 +++++++--------- 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/frigate/config.py b/frigate/config.py index 4907c1919..cc8d95afa 100644 --- a/frigate/config.py +++ b/frigate/config.py @@ -354,6 +354,34 @@ class RuntimeMotionConfig(MotionConfig): frame_shape = config.get("frame_shape", (1, 1)) mask = config.get("mask", "") + + # masks and zones are saved as relative coordinates + # we know if any points are > 1 then it is using the + # old native resolution coordinates + if mask: + if isinstance(mask, list): + relative_masks = [] + for m in mask: + points = m.split(",") + relative_masks.append( + ",".join( + [ + f"{round(int(points[i]) / frame_shape[1], 2)},{round(int(points[i + 1]) / frame_shape[0], 2)}" + for i in range(0, len(points), 2) + ] + ) + ) + + mask = relative_masks + elif isinstance(mask, str): + points = mask.split(",") + mask = ",".join( + [ + f"{round(int(points[i]) / frame_shape[1], 2)},{round(int(points[i + 1]) / frame_shape[0], 2)}" + for i in range(0, len(points), 2) + ] + ) + config["raw_mask"] = mask if mask: diff --git a/frigate/util/image.py b/frigate/util/image.py index 45cdfec79..a81ea30b5 100644 --- a/frigate/util/image.py +++ b/frigate/util/image.py @@ -739,18 +739,16 @@ def add_mask(mask: str, mask_img: np.ndarray): # masks and zones are saved as relative coordinates # we know if any points are > 1 then it is using the # old native resolution coordinates - explicit = any(x > "1.0" for x in points) + logger.error(f"received points as {points}") + if any(x > "1.0" for x in points): + raise Exception("add mask expects relative coordinates only") contour = np.array( [ - ( - [int(points[i]), int(points[i + 1])] - if explicit - else [ - int(float(points[i]) * mask_img.shape[1]), - int(float(points[i + 1]) * mask_img.shape[0]), - ] - ) + [ + int(float(points[i]) * mask_img.shape[1]), + int(float(points[i + 1]) * mask_img.shape[0]), + ] for i in range(0, len(points), 2) ] )