mirror of
https://github.com/blakeblackshear/frigate.git
synced 2026-11-14 09:33:00 +03:00
Compare mask coordinates numerically (#24666)
CI / AMD64 Build (push) Canceled after 0s
CI / AMD64 Smoke Test (push) Canceled after 0s
CI / ARM Build (push) Canceled after 0s
CI / Jetson Jetpack 6 (push) Canceled after 0s
CI / AMD64 Extra Build (push) Canceled after 0s
CI / ARM Extra Build (push) Canceled after 0s
CI / Synaptics Build (push) Canceled after 0s
CI / Assemble and push default build (push) Canceled after 0s
CI / AMD64 Build (push) Canceled after 0s
CI / AMD64 Smoke Test (push) Canceled after 0s
CI / ARM Build (push) Canceled after 0s
CI / Jetson Jetpack 6 (push) Canceled after 0s
CI / AMD64 Extra Build (push) Canceled after 0s
CI / ARM Extra Build (push) Canceled after 0s
CI / Synaptics Build (push) Canceled after 0s
CI / Assemble and push default build (push) Canceled after 0s
* Compare mask coordinates numerically * Fix zone coordinate comparisons and normalize mask examples * Fix validation errors for malformed zone coordinates
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
"""Tests for mask coordinate normalization and rasterization."""
|
||||
|
||||
import unittest
|
||||
from itertools import product
|
||||
|
||||
import numpy as np
|
||||
|
||||
from frigate.config.config import RuntimeFilterConfig, RuntimeMotionConfig
|
||||
|
||||
|
||||
class TestMaskCoordinates(unittest.TestCase):
|
||||
def test_relative_coordinate_precision(self):
|
||||
cases = (
|
||||
("0.000,0.000,{edge},0.000,{edge},0.400,0.000,0.400", slice(0, 41)),
|
||||
("0.000,0.700,{edge},0.700,{edge},{edge},0.000,{edge}", slice(70, 100)),
|
||||
)
|
||||
for config_type, (template, rows), edge, as_list in product(
|
||||
(RuntimeMotionConfig, RuntimeFilterConfig),
|
||||
cases,
|
||||
("1", "1.0", "1.00", "1.000", "1.000000"),
|
||||
(False, True),
|
||||
):
|
||||
coordinates = template.format(edge=edge)
|
||||
if as_list:
|
||||
coordinates = [coordinates]
|
||||
with self.subTest(
|
||||
config_type=config_type.__name__, coordinates=coordinates
|
||||
):
|
||||
config = config_type(
|
||||
frame_shape=(100, 200),
|
||||
mask={"test_mask": {"coordinates": coordinates}},
|
||||
)
|
||||
expected = np.full((100, 200), 255, dtype=np.uint8)
|
||||
expected[rows] = 0
|
||||
|
||||
np.testing.assert_array_equal(config.rasterized_mask, expected)
|
||||
self.assertEqual(config.mask["test_mask"].coordinates, coordinates)
|
||||
|
||||
def test_pixel_coordinates(self):
|
||||
pixel_mask = "0,0,200,0,200,40,0,40"
|
||||
relative_mask = "0.000,0.700,1.000,0.700,1.000,1.000,0.000,1.000"
|
||||
for config_type, coordinates in product(
|
||||
(RuntimeMotionConfig, RuntimeFilterConfig),
|
||||
(pixel_mask, [pixel_mask], [pixel_mask, relative_mask]),
|
||||
):
|
||||
with self.subTest(
|
||||
config_type=config_type.__name__, coordinates=coordinates
|
||||
):
|
||||
config = config_type(
|
||||
frame_shape=(100, 200),
|
||||
mask={"test_mask": {"coordinates": coordinates}},
|
||||
)
|
||||
expected = np.full((100, 200), 255, dtype=np.uint8)
|
||||
expected[:41] = 0
|
||||
if isinstance(coordinates, list) and len(coordinates) == 2:
|
||||
expected[70:] = 0
|
||||
|
||||
np.testing.assert_array_equal(config.rasterized_mask, expected)
|
||||
@@ -0,0 +1,51 @@
|
||||
"""Tests for relative and legacy zone coordinates."""
|
||||
|
||||
import unittest
|
||||
from itertools import product
|
||||
|
||||
import numpy as np
|
||||
|
||||
from frigate.config.camera.zone import ZoneConfig
|
||||
|
||||
|
||||
class TestZoneCoordinates(unittest.TestCase):
|
||||
def test_relative_coordinate_precision(self):
|
||||
expected = np.array([[0, 0], [200, 0], [200, 40], [0, 40]])
|
||||
|
||||
for edge, as_list in product(
|
||||
("1", "1.0", "1.00", "1.000", "1.000000"), (False, True)
|
||||
):
|
||||
points = ["0,0", f"{edge},0", f"{edge},0.4", "0,0.4"]
|
||||
coordinates = points if as_list else ",".join(points)
|
||||
|
||||
with self.subTest(coordinates=coordinates):
|
||||
zone = ZoneConfig(coordinates=coordinates)
|
||||
zone.generate_contour((100, 200))
|
||||
|
||||
np.testing.assert_array_equal(zone.contour, expected)
|
||||
self.assertEqual(zone.coordinates, coordinates)
|
||||
|
||||
def test_invalid_coordinates(self):
|
||||
for points in (
|
||||
["invalid,0", "1,0", "1,0.4", "0,0.4"],
|
||||
["0,invalid", "1,0", "1,0.4", "0,0.4"],
|
||||
):
|
||||
for coordinates in (",".join(points), points):
|
||||
with self.subTest(coordinates=coordinates):
|
||||
zone = ZoneConfig(coordinates=coordinates)
|
||||
with self.assertRaisesRegex(
|
||||
ValueError, "Invalid coordinates found in configuration file"
|
||||
):
|
||||
zone.generate_contour((100, 200))
|
||||
|
||||
def test_pixel_coordinates(self):
|
||||
points = ["0,0", "200,0", "200,40", "0,40"]
|
||||
expected = np.array([[0, 0], [200, 0], [200, 40], [0, 40]])
|
||||
|
||||
for coordinates in (",".join(points), points):
|
||||
with self.subTest(coordinates=coordinates):
|
||||
zone = ZoneConfig(coordinates=coordinates)
|
||||
zone.generate_contour((100, 200))
|
||||
|
||||
np.testing.assert_array_equal(zone.contour, expected)
|
||||
self.assertEqual(zone.coordinates, "0.0,0.0,1.0,0.0,1.0,0.4,0.0,0.4")
|
||||
Reference in New Issue
Block a user