blakeblackshear.frigate/frigate/test/test_config.py

342 lines
12 KiB
Python
Raw Normal View History

2020-11-01 13:17:44 +01:00
import json
from unittest import TestCase, main
import voluptuous as vol
2020-11-03 15:15:58 +01:00
from frigate.config import FRIGATE_CONFIG_SCHEMA, FrigateConfig
2020-11-01 13:17:44 +01:00
2021-02-17 14:23:32 +01:00
2020-11-01 13:17:44 +01:00
class TestConfig(TestCase):
2020-11-03 15:15:58 +01:00
def setUp(self):
self.minimal = {
2021-02-17 14:23:32 +01:00
"mqtt": {"host": "mqtt"},
"cameras": {
"back": {
"ffmpeg": {
"inputs": [
{"path": "rtsp://10.0.0.1:554/video", "roles": ["detect"]}
2020-11-29 22:55:53 +01:00
]
2020-11-03 15:15:58 +01:00
},
2021-02-17 14:23:32 +01:00
"height": 1080,
"width": 1920,
2020-11-03 15:15:58 +01:00
}
2021-02-17 14:23:32 +01:00
},
2020-11-03 15:15:58 +01:00
}
2021-02-17 14:23:32 +01:00
2020-11-01 13:17:44 +01:00
def test_empty(self):
FRIGATE_CONFIG_SCHEMA({})
def test_minimal(self):
2020-11-03 15:15:58 +01:00
FRIGATE_CONFIG_SCHEMA(self.minimal)
2021-02-17 14:23:32 +01:00
2020-11-03 15:15:58 +01:00
def test_config_class(self):
FrigateConfig(config=self.minimal)
2021-02-17 14:23:32 +01:00
2020-11-03 15:15:58 +01:00
def test_inherit_tracked_objects(self):
config = {
2021-02-17 14:23:32 +01:00
"mqtt": {"host": "mqtt"},
"objects": {"track": ["person", "dog"]},
"cameras": {
"back": {
"ffmpeg": {
"inputs": [
{"path": "rtsp://10.0.0.1:554/video", "roles": ["detect"]}
2020-11-29 22:55:53 +01:00
]
2020-11-03 15:15:58 +01:00
},
2021-02-17 14:23:32 +01:00
"height": 1080,
"width": 1920,
2020-11-03 15:15:58 +01:00
}
2021-02-17 14:23:32 +01:00
},
2020-11-03 15:15:58 +01:00
}
frigate_config = FrigateConfig(config=config)
2021-02-17 14:23:32 +01:00
assert "dog" in frigate_config.cameras["back"].objects.track
2020-11-03 15:15:58 +01:00
def test_override_tracked_objects(self):
config = {
2021-02-17 14:23:32 +01:00
"mqtt": {"host": "mqtt"},
"objects": {"track": ["person", "dog"]},
"cameras": {
"back": {
"ffmpeg": {
"inputs": [
{"path": "rtsp://10.0.0.1:554/video", "roles": ["detect"]}
2020-11-29 22:55:53 +01:00
]
2020-11-03 15:15:58 +01:00
},
2021-02-17 14:23:32 +01:00
"height": 1080,
"width": 1920,
"objects": {"track": ["cat"]},
2020-11-03 15:15:58 +01:00
}
2021-02-17 14:23:32 +01:00
},
2020-11-03 15:15:58 +01:00
}
frigate_config = FrigateConfig(config=config)
2021-02-17 14:23:32 +01:00
assert "cat" in frigate_config.cameras["back"].objects.track
2020-11-03 15:15:58 +01:00
def test_default_object_filters(self):
config = {
2021-02-17 14:23:32 +01:00
"mqtt": {"host": "mqtt"},
"objects": {"track": ["person", "dog"]},
"cameras": {
"back": {
"ffmpeg": {
"inputs": [
{"path": "rtsp://10.0.0.1:554/video", "roles": ["detect"]}
2020-11-29 22:55:53 +01:00
]
2020-11-03 15:15:58 +01:00
},
2021-02-17 14:23:32 +01:00
"height": 1080,
"width": 1920,
2020-11-03 15:15:58 +01:00
}
2021-02-17 14:23:32 +01:00
},
2020-11-03 15:15:58 +01:00
}
frigate_config = FrigateConfig(config=config)
2021-02-17 14:23:32 +01:00
assert "dog" in frigate_config.cameras["back"].objects.filters
2020-11-03 15:15:58 +01:00
def test_inherit_object_filters(self):
config = {
2021-02-17 14:23:32 +01:00
"mqtt": {"host": "mqtt"},
"objects": {
"track": ["person", "dog"],
"filters": {"dog": {"threshold": 0.7}},
2020-11-03 15:15:58 +01:00
},
2021-02-17 14:23:32 +01:00
"cameras": {
"back": {
"ffmpeg": {
"inputs": [
{"path": "rtsp://10.0.0.1:554/video", "roles": ["detect"]}
2020-11-29 22:55:53 +01:00
]
2020-11-03 15:15:58 +01:00
},
2021-02-17 14:23:32 +01:00
"height": 1080,
"width": 1920,
2020-11-03 15:15:58 +01:00
}
2021-02-17 14:23:32 +01:00
},
2020-11-03 15:15:58 +01:00
}
frigate_config = FrigateConfig(config=config)
2021-02-17 14:23:32 +01:00
assert "dog" in frigate_config.cameras["back"].objects.filters
assert frigate_config.cameras["back"].objects.filters["dog"].threshold == 0.7
2020-11-03 15:15:58 +01:00
def test_override_object_filters(self):
config = {
2021-02-17 14:23:32 +01:00
"mqtt": {"host": "mqtt"},
"cameras": {
"back": {
"ffmpeg": {
"inputs": [
{"path": "rtsp://10.0.0.1:554/video", "roles": ["detect"]}
2020-11-29 22:55:53 +01:00
]
2020-11-03 15:15:58 +01:00
},
2021-02-17 14:23:32 +01:00
"height": 1080,
"width": 1920,
"objects": {
"track": ["person", "dog"],
"filters": {"dog": {"threshold": 0.7}},
},
2020-11-03 15:15:58 +01:00
}
2021-02-17 14:23:32 +01:00
},
2020-11-03 15:15:58 +01:00
}
frigate_config = FrigateConfig(config=config)
2021-02-17 14:23:32 +01:00
assert "dog" in frigate_config.cameras["back"].objects.filters
assert frigate_config.cameras["back"].objects.filters["dog"].threshold == 0.7
2021-02-06 13:30:26 +01:00
def test_global_object_mask(self):
config = {
2021-02-17 14:23:32 +01:00
"mqtt": {"host": "mqtt"},
"objects": {"track": ["person", "dog"]},
"cameras": {
"back": {
"ffmpeg": {
"inputs": [
{"path": "rtsp://10.0.0.1:554/video", "roles": ["detect"]}
2021-02-06 13:30:26 +01:00
]
},
2021-02-17 14:23:32 +01:00
"height": 1080,
"width": 1920,
"objects": {
"mask": "0,0,1,1,0,1",
"filters": {"dog": {"mask": "1,1,1,1,1,1"}},
},
2021-02-06 13:30:26 +01:00
}
2021-02-17 14:23:32 +01:00
},
2021-02-06 13:30:26 +01:00
}
frigate_config = FrigateConfig(config=config)
2021-02-17 14:23:32 +01:00
assert "dog" in frigate_config.cameras["back"].objects.filters
assert len(frigate_config.cameras["back"].objects.filters["dog"]._raw_mask) == 2
assert (
len(frigate_config.cameras["back"].objects.filters["person"]._raw_mask) == 1
)
2021-02-05 14:22:33 +01:00
def test_ffmpeg_params_global(self):
2020-11-03 15:15:58 +01:00
config = {
2021-02-17 14:23:32 +01:00
"ffmpeg": {"input_args": ["-re"]},
"mqtt": {"host": "mqtt"},
"cameras": {
"back": {
"ffmpeg": {
"inputs": [
{"path": "rtsp://10.0.0.1:554/video", "roles": ["detect"]}
2020-11-29 22:55:53 +01:00
]
2020-11-03 15:15:58 +01:00
},
2021-02-17 14:23:32 +01:00
"height": 1080,
"width": 1920,
"objects": {
"track": ["person", "dog"],
"filters": {"dog": {"threshold": 0.7}},
},
2020-11-01 13:17:44 +01:00
}
2021-02-17 14:23:32 +01:00
},
2020-11-01 13:17:44 +01:00
}
2020-11-03 15:15:58 +01:00
frigate_config = FrigateConfig(config=config)
2021-02-17 14:23:32 +01:00
assert "-re" in frigate_config.cameras["back"].ffmpeg_cmds[0]["cmd"]
2021-02-05 14:22:33 +01:00
def test_ffmpeg_params_camera(self):
config = {
2021-02-17 14:23:32 +01:00
"mqtt": {"host": "mqtt"},
"cameras": {
"back": {
"ffmpeg": {
"inputs": [
{"path": "rtsp://10.0.0.1:554/video", "roles": ["detect"]}
2021-02-05 14:22:33 +01:00
],
2021-02-17 14:23:32 +01:00
"input_args": ["-re"],
},
"height": 1080,
"width": 1920,
"objects": {
"track": ["person", "dog"],
"filters": {"dog": {"threshold": 0.7}},
2021-02-05 14:22:33 +01:00
},
}
2021-02-17 14:23:32 +01:00
},
2021-02-05 14:22:33 +01:00
}
frigate_config = FrigateConfig(config=config)
2021-02-17 14:23:32 +01:00
assert "-re" in frigate_config.cameras["back"].ffmpeg_cmds[0]["cmd"]
2021-02-05 14:22:33 +01:00
def test_ffmpeg_params_input(self):
config = {
2021-02-17 14:23:32 +01:00
"mqtt": {"host": "mqtt"},
"cameras": {
"back": {
"ffmpeg": {
"inputs": [
{
"path": "rtsp://10.0.0.1:554/video",
"roles": ["detect"],
"input_args": ["-re"],
}
2021-02-05 14:22:33 +01:00
]
},
2021-02-17 14:23:32 +01:00
"height": 1080,
"width": 1920,
"objects": {
"track": ["person", "dog"],
"filters": {"dog": {"threshold": 0.7}},
},
2021-02-05 14:22:33 +01:00
}
2021-02-17 14:23:32 +01:00
},
2021-02-05 14:22:33 +01:00
}
frigate_config = FrigateConfig(config=config)
2021-02-17 14:23:32 +01:00
assert "-re" in frigate_config.cameras["back"].ffmpeg_cmds[0]["cmd"]
2020-12-23 14:16:37 +01:00
def test_inherit_clips_retention(self):
2020-11-23 15:25:46 +01:00
config = {
2021-02-17 14:23:32 +01:00
"mqtt": {"host": "mqtt"},
"clips": {"retain": {"default": 20, "objects": {"person": 30}}},
"cameras": {
"back": {
"ffmpeg": {
"inputs": [
{"path": "rtsp://10.0.0.1:554/video", "roles": ["detect"]}
2020-11-29 22:55:53 +01:00
]
2020-11-23 15:25:46 +01:00
},
2021-02-17 14:23:32 +01:00
"height": 1080,
"width": 1920,
2020-11-23 15:25:46 +01:00
}
2021-02-17 14:23:32 +01:00
},
2020-11-23 15:25:46 +01:00
}
frigate_config = FrigateConfig(config=config)
2021-02-17 14:23:32 +01:00
assert frigate_config.cameras["back"].clips.retain.objects["person"] == 30
2020-11-29 22:55:53 +01:00
def test_roles_listed_twice_throws_error(self):
config = {
2021-02-17 14:23:32 +01:00
"mqtt": {"host": "mqtt"},
"clips": {"retain": {"default": 20, "objects": {"person": 30}}},
"cameras": {
"back": {
"ffmpeg": {
"inputs": [
{"path": "rtsp://10.0.0.1:554/video", "roles": ["detect"]},
{"path": "rtsp://10.0.0.1:554/video2", "roles": ["detect"]},
2020-11-29 22:55:53 +01:00
]
},
2021-02-17 14:23:32 +01:00
"height": 1080,
"width": 1920,
2020-11-29 22:55:53 +01:00
}
2021-02-17 14:23:32 +01:00
},
2020-11-29 22:55:53 +01:00
}
self.assertRaises(vol.MultipleInvalid, lambda: FrigateConfig(config=config))
2021-02-17 14:23:32 +01:00
def test_zone_matching_camera_name_throws_error(self):
config = {
2021-02-17 14:23:32 +01:00
"mqtt": {"host": "mqtt"},
"clips": {"retain": {"default": 20, "objects": {"person": 30}}},
"cameras": {
"back": {
"ffmpeg": {
"inputs": [
{"path": "rtsp://10.0.0.1:554/video", "roles": ["detect"]}
]
},
2021-02-17 14:23:32 +01:00
"height": 1080,
"width": 1920,
"zones": {"back": {"coordinates": "1,1,1,1,1,1"}},
}
2021-02-17 14:23:32 +01:00
},
}
self.assertRaises(vol.MultipleInvalid, lambda: FrigateConfig(config=config))
2021-02-17 14:23:32 +01:00
2020-12-23 14:16:37 +01:00
def test_clips_should_default_to_global_objects(self):
2020-12-04 13:59:03 +01:00
config = {
2021-02-17 14:23:32 +01:00
"mqtt": {"host": "mqtt"},
"clips": {"retain": {"default": 20, "objects": {"person": 30}}},
"objects": {"track": ["person", "dog"]},
"cameras": {
"back": {
"ffmpeg": {
"inputs": [
{"path": "rtsp://10.0.0.1:554/video", "roles": ["detect"]}
2020-12-04 13:59:03 +01:00
]
},
2021-02-17 14:23:32 +01:00
"height": 1080,
"width": 1920,
"clips": {"enabled": True},
2020-12-04 13:59:03 +01:00
}
2021-02-17 14:23:32 +01:00
},
2020-12-04 13:59:03 +01:00
}
config = FrigateConfig(config=config)
2021-02-17 14:23:32 +01:00
assert config.cameras["back"].clips.objects is None
def test_role_assigned_but_not_enabled(self):
json_config = {
2021-02-17 14:23:32 +01:00
"mqtt": {"host": "mqtt"},
"cameras": {
"back": {
"ffmpeg": {
"inputs": [
{
"path": "rtsp://10.0.0.1:554/video",
"roles": ["detect", "rtmp"],
},
{"path": "rtsp://10.0.0.1:554/record", "roles": ["record"]},
]
},
2021-02-17 14:23:32 +01:00
"height": 1080,
"width": 1920,
}
2021-02-17 14:23:32 +01:00
},
}
config = FrigateConfig(config=json_config)
2021-02-17 14:23:32 +01:00
ffmpeg_cmds = config.cameras["back"].ffmpeg_cmds
assert len(ffmpeg_cmds) == 1
assert not "clips" in ffmpeg_cmds[0]["roles"]
2020-11-01 13:17:44 +01:00
2021-02-17 14:23:32 +01:00
if __name__ == "__main__":
2020-11-01 13:17:44 +01:00
main(verbosity=2)