use relative coordinates for masks & zones (#10912)

* Handle zones and masks as relative coords

* Ensure that zone coords are saved as relative

* Get motion mask working with relative coordinates

* Rewrite object mask to use relative coordinates as well

* Formatting

* Fix always trying to convert

* fix mask logic
This commit is contained in:
Nicolas Mowen
2024-04-09 16:51:38 -06:00
committed by GitHub
parent ef52a1d6f0
commit 15e4f5c771
5 changed files with 258 additions and 48 deletions

View File

@@ -727,9 +727,22 @@ def create_mask(frame_shape, mask):
return mask_img
def add_mask(mask, mask_img):
def add_mask(mask: str, mask_img: np.ndarray):
points = mask.split(",")
# 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 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])] for i in range(0, len(points), 2)]
[
[
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)
]
)
cv2.fillPoly(mask_img, pts=[contour], color=(0))