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/privacy.py ADDED
@@ -0,0 +1,167 @@
1
+ """Privacy anonymization: Gaussian blur, pixelation, head/face blur, and stick-figure-only mode.
2
+
3
+ Best effort, not a guarantee: every box the detector produced (any score, including people not yet
4
+ confirmed as tracks) plus briefly-missed tracks is anonymized, with a margin. A person the detector
5
+ never finds in a frame is not. ``face-blur`` assumes the head is in the top 30% of the box.
6
+ ``skeleton`` draws a fixed stick figure inside each track box (no pose estimation) on a blank
7
+ canvas, so no video pixels are written at all.
8
+ """
9
+
10
+ from __future__ import annotations
11
+
12
+ from collections.abc import Sequence
13
+ from typing import Literal
14
+
15
+ import cv2
16
+ import numpy as np
17
+
18
+ from trackflow.types import Detection, Track
19
+
20
+ AnonymizeMode = Literal["none", "blur", "pixelate", "face-blur", "skeleton"]
21
+
22
+
23
+ def _track_color(track_id: int) -> tuple[int, int, int]:
24
+ """Deterministic vibrant BGR color per track ID."""
25
+ palette = [
26
+ (92, 214, 148),
27
+ (250, 173, 72),
28
+ (140, 130, 255),
29
+ (240, 200, 70),
30
+ (110, 235, 255),
31
+ (255, 130, 210),
32
+ (135, 255, 190),
33
+ (195, 155, 255),
34
+ ]
35
+ return palette[track_id % len(palette)]
36
+
37
+
38
+ class PrivacyAnonymizer:
39
+ """Applies privacy-preserving transformations to every detected person box."""
40
+
41
+ def __init__(
42
+ self, mode: AnonymizeMode = "none", *, strength: int = 31, margin: float = 0.08
43
+ ) -> None:
44
+ valid = ("none", "blur", "pixelate", "face-blur", "skeleton")
45
+ if mode not in valid:
46
+ raise ValueError(f"Unknown anonymize mode '{mode}'. Choose from: {valid}")
47
+ self.mode: AnonymizeMode = mode
48
+ # Ensure odd kernel size >= 9
49
+ self.kernel_size = max(9, int(strength) | 1)
50
+ self.margin = float(margin)
51
+
52
+ def apply(
53
+ self,
54
+ frame_bgr: np.ndarray,
55
+ tracks: Sequence[Track],
56
+ *,
57
+ detections: Sequence[Detection] = (),
58
+ coasting: Sequence[Track] = (),
59
+ ) -> np.ndarray:
60
+ """Return an anonymized copy of ``frame_bgr``.
61
+
62
+ Anonymizes the boxes of ``tracks``, of briefly-missed ``coasting`` tracks, and of all raw
63
+ ``detections`` (pass every detection, not only the ones that became tracks).
64
+ """
65
+ if self.mode == "none":
66
+ return frame_bgr
67
+
68
+ if self.mode == "skeleton":
69
+ return self._render_skeleton_canvas(frame_bgr, [*tracks, *coasting])
70
+
71
+ out = frame_bgr.copy()
72
+ h, w = out.shape[:2]
73
+
74
+ boxes = [t.bbox for t in tracks] + [t.bbox for t in coasting] + [d.bbox for d in detections]
75
+ for bx1, by1, bx2, by2 in boxes:
76
+ mx, my = (bx2 - bx1) * self.margin, (by2 - by1) * self.margin
77
+ x1 = max(0, min(w - 1, int(round(bx1 - mx))))
78
+ y1 = max(0, min(h - 1, int(round(by1 - my))))
79
+ x2 = max(x1 + 1, min(w, int(round(bx2 + mx))))
80
+ y2 = max(y1 + 1, min(h, int(round(by2 + my))))
81
+ if x2 - x1 < 4 or y2 - y1 < 4:
82
+ continue
83
+
84
+ if self.mode == "blur":
85
+ roi = out[y1:y2, x1:x2]
86
+ out[y1:y2, x1:x2] = cv2.GaussianBlur(roi, (self.kernel_size, self.kernel_size), 0)
87
+ elif self.mode == "pixelate":
88
+ roi = out[y1:y2, x1:x2]
89
+ bw, bh = max(4, (x2 - x1) // 10), max(4, (y2 - y1) // 10)
90
+ small = cv2.resize(roi, (bw, bh), interpolation=cv2.INTER_LINEAR)
91
+ out[y1:y2, x1:x2] = cv2.resize(
92
+ small, (x2 - x1, y2 - y1), interpolation=cv2.INTER_NEAREST
93
+ )
94
+ elif self.mode == "face-blur":
95
+ # Blur upper head/face ellipse (top 30% of person bounding box)
96
+ head_h = max(8, int((y2 - y1) * 0.30))
97
+ fy2 = min(h, y1 + head_h)
98
+ roi = out[y1:fy2, x1:x2]
99
+ blurred = cv2.GaussianBlur(roi, (self.kernel_size, self.kernel_size), 0)
100
+ mask = np.zeros((fy2 - y1, x2 - x1), dtype=np.uint8)
101
+ cv2.ellipse(
102
+ mask,
103
+ ((x2 - x1) // 2, (fy2 - y1) // 2),
104
+ (max(2, int((x2 - x1) * 0.42)), max(2, int((fy2 - y1) * 0.48))),
105
+ 0,
106
+ 0,
107
+ 360,
108
+ 255,
109
+ -1,
110
+ )
111
+ out[y1:fy2, x1:x2] = np.where(mask[:, :, None] > 0, blurred, roi)
112
+
113
+ return out
114
+
115
+ @staticmethod
116
+ def _render_skeleton_canvas(frame_bgr: np.ndarray, tracks: Sequence[Track]) -> np.ndarray:
117
+ """Render box-based stick figures (no pose estimation) on a blank grid canvas."""
118
+ h, w = frame_bgr.shape[:2]
119
+ canvas = np.full((h, w, 3), (18, 20, 26), dtype=np.uint8)
120
+
121
+ # Subtle analytics grid
122
+ step = max(32, min(w, h) // 12)
123
+ for gx in range(0, w, step):
124
+ cv2.line(canvas, (gx, 0), (gx, h), (28, 32, 42), 1)
125
+ for gy in range(0, h, step):
126
+ cv2.line(canvas, (0, gy), (w, gy), (28, 32, 42), 1)
127
+
128
+ for tr in tracks:
129
+ x1, y1, x2, y2 = tr.bbox
130
+ bw = max(8.0, x2 - x1)
131
+ bh = max(16.0, y2 - y1)
132
+ cx = int(round((x1 + x2) * 0.5))
133
+ color = _track_color(tr.track_id)
134
+
135
+ # Anatomical landmarks scaled to bounding box
136
+ head_y = int(round(y1 + bh * 0.12))
137
+ head_r = max(3, int(round(min(bw * 0.18, bh * 0.08))))
138
+ neck = (cx, int(round(y1 + bh * 0.22)))
139
+ hip = (cx, int(round(y1 + bh * 0.56)))
140
+ l_shoulder = (int(round(cx - bw * 0.24)), int(round(y1 + bh * 0.26)))
141
+ r_shoulder = (int(round(cx + bw * 0.24)), int(round(y1 + bh * 0.26)))
142
+ l_hand = (int(round(cx - bw * 0.34)), int(round(y1 + bh * 0.52)))
143
+ r_hand = (int(round(cx + bw * 0.34)), int(round(y1 + bh * 0.52)))
144
+ l_knee = (int(round(cx - bw * 0.15)), int(round(y1 + bh * 0.78)))
145
+ r_knee = (int(round(cx + bw * 0.15)), int(round(y1 + bh * 0.78)))
146
+ l_foot = (int(round(cx - bw * 0.18)), int(round(y2 - 2)))
147
+ r_foot = (int(round(cx + bw * 0.18)), int(round(y2 - 2)))
148
+
149
+ # Head & skeleton limbs
150
+ cv2.circle(canvas, (cx, head_y), head_r, color, 2, cv2.LINE_AA)
151
+ bones = [
152
+ (neck, hip),
153
+ (l_shoulder, r_shoulder),
154
+ (l_shoulder, l_hand),
155
+ (r_shoulder, r_hand),
156
+ (hip, l_knee),
157
+ (l_knee, l_foot),
158
+ (hip, r_knee),
159
+ (r_knee, r_foot),
160
+ ]
161
+ for p1, p2 in bones:
162
+ cv2.line(canvas, p1, p2, color, 2, cv2.LINE_AA)
163
+
164
+ for joint in (neck, hip, l_shoulder, r_shoulder, l_knee, r_knee):
165
+ cv2.circle(canvas, joint, 3, (240, 245, 255), -1, cv2.LINE_AA)
166
+
167
+ return canvas
trackflow/py.typed ADDED
@@ -0,0 +1 @@
1
+