Get motion mask working with relative coordinates

This commit is contained in:
Nicolas Mowen 2024-04-09 11:23:20 -06:00
parent a705e2b6f7
commit e571853f11
2 changed files with 35 additions and 9 deletions

View File

@ -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:

View File

@ -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]),
]
)
for i in range(0, len(points), 2)
]
)