forked from andrew/feedmill-recounter
123 lines
4.5 KiB
Python
123 lines
4.5 KiB
Python
import json
|
|
import os
|
|
import pytest
|
|
from shapely.geometry import Point
|
|
from src.zone_config import (
|
|
build_counting_polygon,
|
|
build_static_roi,
|
|
list_zone_presets,
|
|
load_zone_config,
|
|
)
|
|
|
|
# FCP's detection_poly_pts at reference resolution (predict.py:692-697).
|
|
FCP_COUNTING_AREA = [[574, 50], [586, 1077], [1418, 1076], [1397, 50]]
|
|
|
|
|
|
def test_load_zone_config_reads_json(tmp_path):
|
|
cfg = {"presets": {"test": {"count_line": {"x_start": 100, "x_end": 200, "y": 150}}}, "active_preset": "test"}
|
|
p = tmp_path / "zones.json"
|
|
p.write_text(json.dumps(cfg))
|
|
result = load_zone_config(str(p))
|
|
assert result["active_preset"] == "test"
|
|
assert result["presets"]["test"]["count_line"]["y"] == 150
|
|
|
|
|
|
def test_build_static_roi_from_zone_config():
|
|
zone = {
|
|
"truck_area": [[100, 50], [110, 300], [400, 310], [390, 40]],
|
|
"count_line": {"x_start": 110, "x_end": 400, "y": 150},
|
|
"reference_resolution": [640, 480],
|
|
}
|
|
roi = build_static_roi(zone, video_w=640, video_h=480)
|
|
assert roi.line_y == 150
|
|
assert roi.confidence == 1.0
|
|
assert roi.x1 < roi.x2
|
|
assert roi.y1 < roi.y2
|
|
|
|
|
|
def test_build_static_roi_scales_coords():
|
|
zone = {
|
|
"truck_area": [[600, 385], [609, 1076], [1404, 1078], [1381, 343]],
|
|
"count_line": {"x_start": 577, "x_end": 1401, "y": 330},
|
|
}
|
|
roi = build_static_roi(zone, video_w=1920, video_h=1080)
|
|
assert roi.line_y == 330
|
|
assert roi.x1 == 600
|
|
assert roi.x2 == 1404
|
|
|
|
|
|
def test_build_static_roi_scales_from_1080p_to_720p():
|
|
zone = {
|
|
"truck_area": [[600, 385], [609, 1076], [1404, 1078], [1381, 343]],
|
|
"count_line": {"x_start": 577, "x_end": 1401, "y": 330},
|
|
"reference_resolution": [1920, 1080],
|
|
}
|
|
roi = build_static_roi(zone, video_w=1280, video_h=720)
|
|
assert roi.line_y == int(330 * 720 / 1080)
|
|
assert roi.x1 == int(600 * 1280 / 1920)
|
|
assert roi.x2 == int(1404 * 1280 / 1920)
|
|
|
|
|
|
# ── Counting polygon (FCP detection_polygon parity) ──────────────────────
|
|
|
|
|
|
def _polygon_coords(poly):
|
|
"""Polygon ring as a list of (x, y) int tuples."""
|
|
return [(int(x), int(y)) for x, y in poly.exterior.coords]
|
|
|
|
|
|
def test_build_counting_polygon_matches_fcp_reference_polygon():
|
|
"""Scaled to 1280x720 the polygon equals FCP's hardcoded detection_polygon.
|
|
|
|
FCP predict.py:688-698 scales 1920x1080 points by 1280/1920 and
|
|
720/1080 and truncates with int().
|
|
"""
|
|
zone = {"counting_area": FCP_COUNTING_AREA, "reference_resolution": [1920, 1080]}
|
|
poly = build_counting_polygon(zone, 1280, 720)
|
|
assert _polygon_coords(poly) == [
|
|
(382, 33), (390, 718), (945, 717), (931, 33), (382, 33),
|
|
]
|
|
|
|
|
|
def test_build_counting_polygon_defaults_to_1920x1080_reference():
|
|
"""A zone without reference_resolution uses FCP's 1920x1080 default."""
|
|
poly = build_counting_polygon({"counting_area": FCP_COUNTING_AREA}, 1280, 720)
|
|
assert _polygon_coords(poly) == [
|
|
(382, 33), (390, 718), (945, 717), (931, 33), (382, 33),
|
|
]
|
|
|
|
|
|
def test_build_counting_polygon_at_reference_resolution_is_unscaled():
|
|
"""At the reference resolution the points are unchanged."""
|
|
zone = {"counting_area": FCP_COUNTING_AREA, "reference_resolution": [1920, 1080]}
|
|
poly = build_counting_polygon(zone, 1920, 1080)
|
|
assert _polygon_coords(poly) == [
|
|
(574, 50), (586, 1077), (1418, 1076), (1397, 50), (574, 50),
|
|
]
|
|
|
|
|
|
def test_build_counting_polygon_none_without_counting_area():
|
|
"""No counting_area -> no polygon (the filter stays off)."""
|
|
assert build_counting_polygon({}, 1280, 720) is None
|
|
assert build_counting_polygon({"truck_area": [[0, 0]]}, 1280, 720) is None
|
|
assert build_counting_polygon({"counting_area": []}, 1280, 720) is None
|
|
|
|
|
|
def test_build_counting_polygon_contains_centroid_semantics():
|
|
"""contains() on the bbox centre mirrors FCP's detection_polygon check."""
|
|
poly = build_counting_polygon({"counting_area": FCP_COUNTING_AREA}, 1280, 720)
|
|
assert poly.contains(Point((620.0, 320.0))) # inside
|
|
assert not poly.contains(Point((70.0, 320.0))) # left of the polygon
|
|
assert not poly.contains(Point((1200.0, 320.0))) # right of the polygon
|
|
|
|
|
|
def test_list_zone_presets(tmp_path):
|
|
cfg = {"presets": {"p1": {"description": "Zone 1"}, "p2": {"description": "Zone 2"}}}
|
|
p = tmp_path / "zones.json"
|
|
p.write_text(json.dumps(cfg))
|
|
presets = list_zone_presets(str(p))
|
|
assert len(presets) == 2
|
|
names = [p["name"] for p in presets]
|
|
assert "p1" in names
|
|
assert "p2" in names
|