Files
feedmill-recounter/src/zone_config.py
T
jetson 4bd9de58e1 feat: freeze ROI mode, deadband+sticky jitter fix, explicit fixed/frozen ROI flags
- zone_mode=freeze: TruckROITracker locks box after 3 consecutive truck
  detections; geometry stays fixed while presence stays live (truck
  departure -> confidence 0 -> batch transitions keep working)
- jitter: 10px deadband skips EMA on tiny bbox noise; sticky main-truck
  pick (IoU>=0.3) prevents ROI jumps between parked trucks
- TruckROI.fixed/frozen flags replace dashboard confidence>=1.0 heuristic
- gate truck_centroid on truck_present in both pipelines
- +22 tests (test_truck_roi.py, test_freeze_plumbing.py); 127 pass
2026-09-28 14:36:21 +07:00

82 lines
2.7 KiB
Python

"""Zone configuration — load/save counting zone presets from zones.json."""
from __future__ import annotations
import json
import os
from typing import Any
from src.truck_roi import TruckROI
def load_zone_config(path: str) -> dict[str, Any]:
"""Load zones.json and return full config dict."""
with open(path) as f:
return json.load(f)
def save_zone_config(config: dict[str, Any], path: str) -> None:
"""Save zone config dict to zones.json."""
with open(path, "w") as f:
json.dump(config, f, indent=2)
def build_static_roi(zone: dict[str, Any], video_w: int, video_h: int) -> TruckROI:
"""Build a TruckROI from a zone config dict, scaling from reference resolution.
The zone dict should have:
- truck_area: [[x1,y1], [x2,y2], [x3,y3], [x4,y4]] (4-point polygon)
- count_line: {"x_start": int, "x_end": int, "y": int}
- reference_resolution: [w, h] (optional, default [1920, 1080])
Coordinates are stored at reference_resolution and scaled to actual video_w/video_h.
"""
truck_pts = zone.get("truck_area", [])
count_line = zone.get("count_line", {})
ref_w, ref_h = zone.get("reference_resolution", [1920, 1080])
# Scale factors from reference resolution to actual video
sx = video_w / ref_w if ref_w else 1.0
sy = video_h / ref_h if ref_h else 1.0
# Compute bounding box of truck_area polygon (scaled)
if truck_pts and len(truck_pts) >= 4:
xs = [int(p[0] * sx) for p in truck_pts]
ys = [int(p[1] * sy) for p in truck_pts]
x1 = max(0, min(xs))
y1 = max(0, min(ys))
x2 = min(video_w, max(xs))
y2 = min(video_h, max(ys))
else:
x1, y1 = int(video_w * 0.3), int(video_h * 0.1)
x2, y2 = int(video_w * 0.75), int(video_h * 0.95)
line_y = int(count_line.get("y", (y1 + y2) // 2) * sy)
line_x_start = int(count_line.get("x_start", x1) * sx)
line_x_end = int(count_line.get("x_end", x2) * sx)
return TruckROI(
x1=x1, y1=y1, x2=x2, y2=y2,
line_y=line_y,
confidence=1.0,
fixed=True,
)
def list_zone_presets(path: str) -> list[dict[str, Any]]:
"""Return list of preset summaries: [{"name": str, "description": str, ...}]."""
config = load_zone_config(path)
presets = config.get("presets", {})
return [
{"name": name, "description": vals.get("description", ""), **vals}
for name, vals in presets.items()
]
def get_active_zone(path: str, preset_name: str | None = None) -> dict[str, Any] | None:
"""Get the active preset dict. Falls back to active_preset field if name not given."""
config = load_zone_config(path)
presets = config.get("presets", {})
name = preset_name or config.get("active_preset")
return presets.get(name)