File size: 1,558 Bytes
05d5ffa
 
 
 
 
 
 
 
06f120e
05d5ffa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import importlib.util
from pathlib import Path
import numpy as np
import pytest
import yaml
from trafficvision.zones.zone_manager import ZoneManager

spec = importlib.util.spec_from_file_location(
    "zone_labeler", Path(__file__).parents[1] / "data/data-labelling/zone_labeler.py"
)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)


def test_labeler_rejects_invalid_polygons_and_incomplete_save(tmp_path):
    labeler = module.ZoneLabeler(np.zeros((540, 960, 3), np.uint8), "test", tmp_path / "zones.yaml")
    with pytest.raises(ValueError, match="at least one"):
        labeler.save()
    labeler.current = [[1, 1], [2, 2], [3, 3]]
    with pytest.raises(ValueError, match="nonzero area"):
        labeler.finish_zone("road")
    with pytest.raises(ValueError, match="Finish"):
        labeler.save()
    labeler.current = [[0, 0], [100, 0], [100, 100]]
    labeler.finish_zone("road")
    labeler.save()
    config = yaml.safe_load(labeler.output.read_text())
    manager = ZoneManager(config)
    manager.validate_frame(960, 540)
    assert manager.zones[0].contains([50, 25])
    labeler.current = [[0, 0], [10, 0], [10, 10]]
    with pytest.raises(ValueError, match="unique"):
        labeler.finish_zone("road")


def test_display_coordinates_scale_and_clip(tmp_path):
    labeler = module.ZoneLabeler(np.zeros((2160, 3840, 3), np.uint8), "test", tmp_path / "zones.yaml")
    labeler.display_frame()
    assert labeler.to_frame_point(700, 350) == [1920, 960]
    assert labeler.to_frame_point(-10, 5000) == [0, 2159]