44 lines
1.8 KiB
Python
44 lines
1.8 KiB
Python
from __future__ import annotations
|
|
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
from Brain.yovision_brain.domain import Box
|
|
from Brain.yovision_brain.source import CentroidTracker, read_stream_url
|
|
|
|
|
|
class StreamURLTests(unittest.TestCase):
|
|
def test_requires_absolute_single_rtsp_url_file(self) -> None:
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
path = Path(directory) / "stream.url"
|
|
path.write_text("rtsp://user:secret@127.0.0.1:8554/camera\n", encoding="utf-8")
|
|
self.assertEqual(read_stream_url(str(path)), "rtsp://user:secret@127.0.0.1:8554/camera")
|
|
path.write_text("rtsp://127.0.0.1/a\nrtsp://127.0.0.1/b\n", encoding="utf-8")
|
|
with self.assertRaisesRegex(ValueError, "exactly one") as caught:
|
|
read_stream_url(str(path))
|
|
self.assertNotIn("127.0.0.1", str(caught.exception))
|
|
|
|
def test_rejects_relative_path_without_echoing_input(self) -> None:
|
|
with self.assertRaisesRegex(ValueError, "absolute"):
|
|
read_stream_url("camera-secret.url")
|
|
|
|
def test_rejects_url_files_inside_repository(self) -> None:
|
|
repository_file = Path(__file__).resolve()
|
|
with self.assertRaisesRegex(ValueError, "outside the repository"):
|
|
read_stream_url(str(repository_file))
|
|
|
|
|
|
class TrackerTests(unittest.TestCase):
|
|
def test_nearby_boxes_retain_track_and_distant_box_gets_new_track(self) -> None:
|
|
tracker = CentroidTracker(max_distance=0.2)
|
|
first = tracker.update([(Box(0.1, 0.1, 0.2, 0.4), 0.8)], 1)
|
|
nearby = tracker.update([(Box(0.12, 0.1, 0.22, 0.4), 0.7)], 2)
|
|
distant = tracker.update([(Box(0.7, 0.1, 0.8, 0.4), 0.9)], 3)
|
|
self.assertEqual(first[0].track_id, nearby[0].track_id)
|
|
self.assertNotEqual(nearby[0].track_id, distant[0].track_id)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|