trackflow-python-tensorrt 0.6.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- trackflow/__init__.py +62 -0
- trackflow/benchmark.py +266 -0
- trackflow/boosttrack.py +440 -0
- trackflow/cli.py +631 -0
- trackflow/detector.py +850 -0
- trackflow/homography.py +240 -0
- trackflow/inspector.py +479 -0
- trackflow/inspector_ui.html +538 -0
- trackflow/lines.py +120 -0
- trackflow/models.py +421 -0
- trackflow/ocsort.py +368 -0
- trackflow/pipeline.py +910 -0
- trackflow/privacy.py +167 -0
- trackflow/py.typed +1 -0
- trackflow/reid.py +819 -0
- trackflow/sinks.py +440 -0
- trackflow/tracker.py +1219 -0
- trackflow/types.py +235 -0
- trackflow_python_tensorrt-0.6.1.dist-info/METADATA +367 -0
- trackflow_python_tensorrt-0.6.1.dist-info/RECORD +23 -0
- trackflow_python_tensorrt-0.6.1.dist-info/WHEEL +4 -0
- trackflow_python_tensorrt-0.6.1.dist-info/entry_points.txt +3 -0
- trackflow_python_tensorrt-0.6.1.dist-info/licenses/LICENSE +202 -0
trackflow/homography.py
ADDED
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
"""4-point floorplan homography calibration, bird's-eye projection, heatmaps, and dwell zones."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
from dataclasses import dataclass, field
|
|
7
|
+
from itertools import combinations
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
from typing import Any
|
|
10
|
+
|
|
11
|
+
import cv2
|
|
12
|
+
import numpy as np
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
@dataclass(slots=True)
|
|
16
|
+
class DwellZone:
|
|
17
|
+
"""Named polygon in top-down floorplan coordinates."""
|
|
18
|
+
|
|
19
|
+
name: str
|
|
20
|
+
polygon: list[tuple[float, float]]
|
|
21
|
+
color: str = "#38bdf8"
|
|
22
|
+
|
|
23
|
+
def contains(self, x: float, y: float) -> bool:
|
|
24
|
+
pts = np.array(self.polygon, dtype=np.float32).reshape(-1, 1, 2)
|
|
25
|
+
return cv2.pointPolygonTest(pts, (float(x), float(y)), False) >= 0
|
|
26
|
+
|
|
27
|
+
def to_dict(self) -> dict[str, Any]:
|
|
28
|
+
return {
|
|
29
|
+
"name": self.name,
|
|
30
|
+
"polygon": [[round(float(x), 3), round(float(y), 3)] for x, y in self.polygon],
|
|
31
|
+
"color": self.color,
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
@dataclass
|
|
36
|
+
class FloorplanHomography:
|
|
37
|
+
"""Maps 2D camera ground points onto a top-down bird's-eye floorplan via 4-point homography."""
|
|
38
|
+
|
|
39
|
+
image_points: list[tuple[float, float]]
|
|
40
|
+
world_points: list[tuple[float, float]]
|
|
41
|
+
width_m: float = 10.0
|
|
42
|
+
height_m: float = 10.0
|
|
43
|
+
zones: list[DwellZone] = field(default_factory=list)
|
|
44
|
+
_matrix: np.ndarray = field(init=False, repr=False)
|
|
45
|
+
_heatmap: np.ndarray = field(init=False, repr=False)
|
|
46
|
+
_trajectories: dict[int, list[tuple[float, float]]] = field(default_factory=dict, repr=False)
|
|
47
|
+
zone_dwell_totals_s: dict[str, float] = field(default_factory=dict)
|
|
48
|
+
|
|
49
|
+
def __post_init__(self) -> None:
|
|
50
|
+
if len(self.image_points) != 4 or len(self.world_points) != 4:
|
|
51
|
+
raise ValueError(
|
|
52
|
+
"FloorplanHomography requires exactly 4 image_points and 4 world_points."
|
|
53
|
+
)
|
|
54
|
+
src = np.array(self.image_points, dtype=np.float32)
|
|
55
|
+
dst = np.array(self.world_points, dtype=np.float32)
|
|
56
|
+
if not all(np.isfinite(v) and v > 0 for v in (self.width_m, self.height_m)):
|
|
57
|
+
raise ValueError("Floorplan width and height must be positive and finite.")
|
|
58
|
+
for points in (src, dst):
|
|
59
|
+
if points.shape != (4, 2) or not np.isfinite(points).all():
|
|
60
|
+
raise ValueError("Calibration points must be finite x, y pairs.")
|
|
61
|
+
if any(
|
|
62
|
+
cv2.contourArea(points[list(triple)]) == 0 for triple in combinations(range(4), 3)
|
|
63
|
+
):
|
|
64
|
+
raise ValueError(
|
|
65
|
+
"Calibration needs four distinct points with no three on the same line."
|
|
66
|
+
)
|
|
67
|
+
self._matrix = cv2.getPerspectiveTransform(src, dst)
|
|
68
|
+
self._heatmap = np.zeros((100, 100), dtype=np.float32)
|
|
69
|
+
for z in self.zones:
|
|
70
|
+
self.zone_dwell_totals_s.setdefault(z.name, 0.0)
|
|
71
|
+
|
|
72
|
+
def fresh(self) -> FloorplanHomography:
|
|
73
|
+
"""Reuse this calibration for an independent stream with empty trajectories and dwell totals."""
|
|
74
|
+
return FloorplanHomography(
|
|
75
|
+
self.image_points, self.world_points, self.width_m, self.height_m, self.zones
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
@classmethod
|
|
79
|
+
def from_json(cls, path: str | Path) -> FloorplanHomography:
|
|
80
|
+
data = json.loads(Path(path).read_text(encoding="utf-8"))
|
|
81
|
+
img_pts = [(float(p[0]), float(p[1])) for p in data["image_points"]]
|
|
82
|
+
wld_pts = [(float(p[0]), float(p[1])) for p in data["world_points"]]
|
|
83
|
+
zones = [
|
|
84
|
+
DwellZone(
|
|
85
|
+
name=str(z["name"]),
|
|
86
|
+
polygon=[(float(p[0]), float(p[1])) for p in z["polygon"]],
|
|
87
|
+
color=str(z.get("color", "#38bdf8")),
|
|
88
|
+
)
|
|
89
|
+
for z in data.get("zones", [])
|
|
90
|
+
]
|
|
91
|
+
width_m = float(data.get("width_m", max(p[0] for p in wld_pts) or 10.0))
|
|
92
|
+
height_m = float(data.get("height_m", max(p[1] for p in wld_pts) or 10.0))
|
|
93
|
+
return cls(
|
|
94
|
+
image_points=img_pts,
|
|
95
|
+
world_points=wld_pts,
|
|
96
|
+
width_m=width_m,
|
|
97
|
+
height_m=height_m,
|
|
98
|
+
zones=zones,
|
|
99
|
+
)
|
|
100
|
+
|
|
101
|
+
def to_dict(self) -> dict[str, Any]:
|
|
102
|
+
return {
|
|
103
|
+
"image_points": [
|
|
104
|
+
[round(float(x), 2), round(float(y), 2)] for x, y in self.image_points
|
|
105
|
+
],
|
|
106
|
+
"world_points": [
|
|
107
|
+
[round(float(x), 3), round(float(y), 3)] for x, y in self.world_points
|
|
108
|
+
],
|
|
109
|
+
"width_m": round(float(self.width_m), 3),
|
|
110
|
+
"height_m": round(float(self.height_m), 3),
|
|
111
|
+
"zones": [z.to_dict() for z in self.zones],
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
def save_json(self, path: str | Path) -> None:
|
|
115
|
+
Path(path).write_text(json.dumps(self.to_dict(), indent=2), encoding="utf-8")
|
|
116
|
+
|
|
117
|
+
def project(self, image_xy: tuple[float, float]) -> tuple[float, float]:
|
|
118
|
+
"""Project a single 2D image pixel point (e.g. foot point) into floorplan (x, y)."""
|
|
119
|
+
pt = np.array([[[float(image_xy[0]), float(image_xy[1])]]], dtype=np.float32)
|
|
120
|
+
out = cv2.perspectiveTransform(pt, self._matrix)[0, 0]
|
|
121
|
+
return (float(out[0]), float(out[1]))
|
|
122
|
+
|
|
123
|
+
def locate_zone(self, world_xy: tuple[float, float]) -> str | None:
|
|
124
|
+
for zone in self.zones:
|
|
125
|
+
if zone.contains(world_xy[0], world_xy[1]):
|
|
126
|
+
return zone.name
|
|
127
|
+
return None
|
|
128
|
+
|
|
129
|
+
def observe(
|
|
130
|
+
self,
|
|
131
|
+
track_id: int,
|
|
132
|
+
foot_xy: tuple[float, float],
|
|
133
|
+
dt_s: float = 0.0,
|
|
134
|
+
) -> tuple[tuple[float, float], str | None]:
|
|
135
|
+
"""Project a track observation, accumulate heatmap & dwell metrics, and return (world_xy, zone)."""
|
|
136
|
+
wx, wy = self.project(foot_xy)
|
|
137
|
+
self._trajectories.setdefault(track_id, []).append((wx, wy))
|
|
138
|
+
|
|
139
|
+
gx = int(
|
|
140
|
+
np.clip(
|
|
141
|
+
(wx / max(self.width_m, 1e-3)) * (self._heatmap.shape[1] - 1),
|
|
142
|
+
0,
|
|
143
|
+
self._heatmap.shape[1] - 1,
|
|
144
|
+
)
|
|
145
|
+
)
|
|
146
|
+
gy = int(
|
|
147
|
+
np.clip(
|
|
148
|
+
(wy / max(self.height_m, 1e-3)) * (self._heatmap.shape[0] - 1),
|
|
149
|
+
0,
|
|
150
|
+
self._heatmap.shape[0] - 1,
|
|
151
|
+
)
|
|
152
|
+
)
|
|
153
|
+
self._heatmap[gy, gx] += 1.0
|
|
154
|
+
|
|
155
|
+
zone = self.locate_zone((wx, wy))
|
|
156
|
+
if zone is not None:
|
|
157
|
+
self.zone_dwell_totals_s[zone] = self.zone_dwell_totals_s.get(zone, 0.0) + max(
|
|
158
|
+
0.0, dt_s
|
|
159
|
+
)
|
|
160
|
+
return (wx, wy), zone
|
|
161
|
+
|
|
162
|
+
def render_floorplan(
|
|
163
|
+
self, out_path: str | Path, canvas_size: tuple[int, int] = (800, 600)
|
|
164
|
+
) -> Path:
|
|
165
|
+
"""Render a high-contrast bird's-eye floorplan PNG with heatmap, dwell zones, and trajectories."""
|
|
166
|
+
cw, ch = canvas_size
|
|
167
|
+
canvas = np.full((ch, cw, 3), (15, 20, 30), dtype=np.uint8)
|
|
168
|
+
|
|
169
|
+
# Draw grid lines (1-meter spacing)
|
|
170
|
+
for mx in range(int(self.width_m) + 1):
|
|
171
|
+
px = int((mx / max(self.width_m, 1e-3)) * (cw - 1))
|
|
172
|
+
cv2.line(canvas, (px, 0), (px, ch), (30, 38, 54), 1)
|
|
173
|
+
for my in range(int(self.height_m) + 1):
|
|
174
|
+
py = int((my / max(self.height_m, 1e-3)) * (ch - 1))
|
|
175
|
+
cv2.line(canvas, (0, py), (cw, py), (30, 38, 54), 1)
|
|
176
|
+
|
|
177
|
+
# Overlay smoothed density heatmap
|
|
178
|
+
if float(self._heatmap.max()) > 0:
|
|
179
|
+
heat = cv2.GaussianBlur(self._heatmap, (11, 11), 0)
|
|
180
|
+
norm = np.clip((heat / max(float(heat.max()), 1e-6)) * 255.0, 0, 255).astype(np.uint8)
|
|
181
|
+
resized = cv2.resize(norm, (cw, ch), interpolation=cv2.INTER_CUBIC)
|
|
182
|
+
colored = cv2.applyColorMap(resized, cv2.COLORMAP_INFERNO)
|
|
183
|
+
mask = (resized > 15).astype(np.float32)[:, :, None] * 0.55
|
|
184
|
+
canvas = np.clip(canvas * (1.0 - mask) + colored * mask, 0, 255).astype(np.uint8)
|
|
185
|
+
|
|
186
|
+
def to_px(wx: float, wy: float) -> tuple[int, int]:
|
|
187
|
+
x = int(np.clip((wx / max(self.width_m, 1e-3)) * (cw - 1), 0, cw - 1))
|
|
188
|
+
y = int(np.clip((wy / max(self.height_m, 1e-3)) * (ch - 1), 0, ch - 1))
|
|
189
|
+
return (x, y)
|
|
190
|
+
|
|
191
|
+
# Draw dwell zones
|
|
192
|
+
for zone in self.zones:
|
|
193
|
+
pts = np.array([to_px(x, y) for x, y in zone.polygon], dtype=np.int32)
|
|
194
|
+
if len(pts) >= 3:
|
|
195
|
+
overlay = canvas.copy()
|
|
196
|
+
cv2.fillPoly(overlay, [pts], (60, 120, 180))
|
|
197
|
+
canvas = cv2.addWeighted(overlay, 0.22, canvas, 0.78, 0)
|
|
198
|
+
cv2.polylines(canvas, [pts], True, (110, 210, 255), 2, cv2.LINE_AA)
|
|
199
|
+
cx, cy = int(pts[:, 0].mean()), int(pts[:, 1].mean())
|
|
200
|
+
dwell = self.zone_dwell_totals_s.get(zone.name, 0.0)
|
|
201
|
+
cv2.putText(
|
|
202
|
+
canvas,
|
|
203
|
+
f"{zone.name} ({dwell:.1f}s)",
|
|
204
|
+
(max(6, cx - 40), max(20, cy)),
|
|
205
|
+
cv2.FONT_HERSHEY_SIMPLEX,
|
|
206
|
+
0.5,
|
|
207
|
+
(230, 245, 255),
|
|
208
|
+
1,
|
|
209
|
+
cv2.LINE_AA,
|
|
210
|
+
)
|
|
211
|
+
|
|
212
|
+
# Draw trajectories
|
|
213
|
+
palette = [
|
|
214
|
+
(92, 214, 148),
|
|
215
|
+
(250, 173, 72),
|
|
216
|
+
(140, 130, 255),
|
|
217
|
+
(110, 235, 255),
|
|
218
|
+
(255, 130, 210),
|
|
219
|
+
]
|
|
220
|
+
for tid, pts in self._trajectories.items():
|
|
221
|
+
if len(pts) < 2:
|
|
222
|
+
continue
|
|
223
|
+
color = palette[tid % len(palette)]
|
|
224
|
+
px_pts = np.array([to_px(x, y) for x, y in pts], dtype=np.int32)
|
|
225
|
+
cv2.polylines(canvas, [px_pts], False, color, 2, cv2.LINE_AA)
|
|
226
|
+
cv2.circle(canvas, tuple(px_pts[-1]), 4, (255, 255, 255), -1, cv2.LINE_AA)
|
|
227
|
+
|
|
228
|
+
cv2.putText(
|
|
229
|
+
canvas,
|
|
230
|
+
f"Trackflow Bird's-Eye Floorplan ({self.width_m:.1f}m x {self.height_m:.1f}m)",
|
|
231
|
+
(14, 26),
|
|
232
|
+
cv2.FONT_HERSHEY_SIMPLEX,
|
|
233
|
+
0.55,
|
|
234
|
+
(225, 235, 245),
|
|
235
|
+
1,
|
|
236
|
+
cv2.LINE_AA,
|
|
237
|
+
)
|
|
238
|
+
out = Path(out_path)
|
|
239
|
+
cv2.imwrite(str(out), canvas)
|
|
240
|
+
return out
|